Statistics
| Branch: | Tag: | Revision:

root / cloudcmsfaq / models.py @ 2a32f17b

History | View | Annotate | Download (7 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34

    
35
from datetime import datetime
36

    
37
from django.db import models
38
from django.conf import settings
39
from django.contrib.auth.models import User
40
from django.utils.translation import ugettext_lazy as _, ugettext, ungettext
41
from django.template.loader import render_to_string
42
from django.core import urlresolvers
43

    
44
from feincms import translations
45
from feincms.models import Base
46
from feincms.module.page.models import Page
47
from feincms.content.richtext.models import RichTextContent
48
from feincms.content.section.models import SectionContent
49
from feincms.content.application.models import reverse
50
from feincms.module.medialibrary.fields import MediaFileForeignKey
51
from feincms.module.medialibrary.models import MediaFile
52
from feincms.module.page.extensions.navigation import NavigationExtension
53
from feincms.module.page.extensions.navigation import PagePretender
54
from feincms.content.application.models import ApplicationContent
55

    
56
from cloudcms.models import Application
57
from cloudcms.cms_utils import get_app_page
58

    
59
# monkeypatch django reverse (feincms 1.5+ solves this issue)
60
urlresolvers.reverse = reverse
61

    
62
class Category(models.Model, translations.TranslatedObjectMixin):
63
    """
64
    Question category.
65
    """
66

    
67
    ordering = models.SmallIntegerField(_('ordering'), default=0)
68
    display_on_menu = models.BooleanField(default=False)
69

    
70
    class Meta:
71
        verbose_name = _('category')
72
        verbose_name_plural = _('categories')
73
        ordering = ['-ordering',]
74

    
75
    objects = translations.TranslatedObjectManager()
76

    
77
    def __unicode__(self):
78
        trans = translations.TranslatedObjectMixin.__unicode__(self)
79
        return trans or _('Unnamed category')
80

    
81

    
82
class CategoryTranslation(translations.Translation(Category)):
83
    """
84
    Category translation
85
    """
86
    title = models.CharField(_('category title'), max_length=100)
87
    slug = models.SlugField(_('slug'), unique=True)
88
    description = models.CharField(_('description'), max_length=250, blank=True)
89

    
90
    class Meta:
91
        verbose_name = _('category translation')
92
        verbose_name_plural = _('category translations')
93
        ordering = ['title']
94

    
95
    def __unicode__(self):
96
        return self.title
97

    
98
    def save(self, *args, **kwargs):
99
        if not self.slug:
100
            self.slug = slugify(self.title)
101

    
102
        super(CategoryTranslation, self).save(*args, **kwargs)
103

    
104

    
105
class QuestionManager(models.Manager):
106

    
107
    def active(self):
108
        return self.filter(is_active=True)
109

    
110
    def latest(self, limit=3):
111
        return self.filter()[:limit]
112

    
113

    
114
def get_faq_page():
115
    """
116
    Returns Page model that has been associated with faq application
117
    """
118
    return get_app_page(Page, "cloudcmsfaq")
119

    
120
class Question(Base):
121
    """
122
    Question/answer entry
123
    """
124
    is_active = models.BooleanField(_('is active'), default=True)
125
    is_featured = models.BooleanField(_('is featured'), default=False)
126

    
127
    title = models.CharField(_('title'), max_length=100)
128
    slug = models.SlugField(_('slug'), max_length=100, unique_for_date='published_on')
129
    author = models.ForeignKey(User, related_name='faqs', verbose_name=_('author'))
130
    language = models.CharField(max_length=255, choices=settings.LANGUAGES)
131

    
132
    application = models.ManyToManyField(Application,
133
            related_name="faqs",
134
            verbose_name=_('application'))
135

    
136
    published_on = models.DateTimeField(_('published on'), blank=True, null=True, default=datetime.now,
137
        help_text=_('Will be filled in automatically when question gets published.'))
138
    last_changed = models.DateTimeField(_('last change'), auto_now=True, editable=False)
139

    
140
    service = models.ForeignKey('cloudcms.Service', verbose_name=_('service'),
141
        related_name='faqs', null=True, blank=False)
142

    
143
    category = models.ForeignKey(Category, verbose_name=_('category'),
144
        related_name='faqs', null=False, blank=False)
145

    
146
    objects = QuestionManager()
147

    
148
    class Meta:
149
        get_latest_by = 'published_on'
150
        ordering = ['service', 'category', '-published_on']
151
        verbose_name = _('faq')
152
        verbose_name_plural = _('faqs')
153

    
154
    def __unicode__(self):
155
        return self.title
156

    
157
    def get_absolute_url(self):
158
        try:
159
            r = reverse('cloudcmsfaq_question_detail', 'cloudcmsfaq.urls', (),
160
                    {
161
                     'service': self.service.translation.slug,
162
                     'slug': self.slug,
163
                    })
164
        except Exception, e:
165
            pass
166

    
167
        # ugly hack to fix proper application reverse url
168
        FAQ_URL = ""
169
        try:
170
            FAQ_URL = get_faq_page().get_navigation_url()
171
        except Exception, e:
172
            pass
173

    
174
        if r.startswith(FAQ_URL):
175
            return r
176
        else:
177
            return FAQ_URL + r.lstrip('/')
178

    
179
    def back_url(self):
180
        return get_faq_page().get_navigation_url()
181

    
182

    
183
# Feincms navigation extension
184
class FaqServicesNavigationExtension(NavigationExtension):
185
    """
186
    Navigation extension for FeinCMS which lists all categories that user
187
    wants to include in global site navigation.
188
    """
189

    
190
    name = _('faq categories')
191

    
192
    def children(self, page, **kwargs):
193
        from cloudcms.models import Service
194

    
195
        for service in Service.objects.filter(display_on_menu=True):
196
            url='%sservice/%s/' % (page.get_absolute_url(), service.translation.slug)
197
            yield PagePretender(
198
                title=service.translation.title,
199
                tree_id=page.tree_id,
200
                url=url,
201
                lft=0,
202
                rght=0,
203
                slug=category.translation.slug,
204
            )
205