Statistics
| Branch: | Tag: | Revision:

root / cloudcmsfaq / models.py @ bd9ce789

History | View | Annotate | Download (7.4 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
    image = MediaFileForeignKey(MediaFile, blank=True, null=True)
70
    class_name = models.CharField(_('class_name'), max_length=200, blank=True, null=True)
71

    
72
    class Meta:
73
        verbose_name = _('category')
74
        verbose_name_plural = _('categories')
75
        ordering = ['ordering',]
76

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

    
84

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

    
93
    class Meta:
94
        verbose_name = _('category translation')
95
        verbose_name_plural = _('category translations')
96
        ordering = ['title']
97

    
98
    def __unicode__(self):
99
        return self.title
100

    
101
    def save(self, *args, **kwargs):
102
        if not self.slug:
103
            self.slug = slugify(self.title)
104

    
105
        super(CategoryTranslation, self).save(*args, **kwargs)
106

    
107

    
108
class QuestionManager(models.Manager):
109

    
110
    def active(self):
111
        return self.filter(is_active=True)
112

    
113
    def latest(self, limit=3):
114
        return self.filter()[:limit]
115

    
116

    
117
def get_faq_page():
118
    """
119
    Returns Page model that has been associated with faq application
120
    """
121
    return get_app_page(Page, "cloudcmsfaq")
122

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

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

    
135
    application = models.ManyToManyField(Application,
136
            related_name="faqs",
137
            verbose_name=_('application'))
138

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

    
143
    service = models.ForeignKey('cloudcms.Service', verbose_name=_('service'),
144
        related_name='faqs', null=True, blank=False)
145

    
146
    category = models.ForeignKey(Category, verbose_name=_('category'),
147
        related_name='faqs', null=False, blank=False)
148

    
149
    objects = QuestionManager()
150

    
151

    
152
    class Meta:
153
        get_latest_by = 'published_on'
154
        ordering = ['service', 'category__ordering', 'published_on']
155
        verbose_name = _('faq')
156
        verbose_name_plural = _('faqs')
157

    
158
    def __unicode__(self):
159
        return self.title
160

    
161
    def get_next(self):
162
        return self.get_next_by_published_on(category=self.category, service=self.service)
163

    
164
    def get_previous(self):
165
        return self.get_previous_by_published_on(category=self.category, service=self.service)
166
    
167
    def get_absolute_url(self):
168
        try:
169
            r = reverse('cloudcmsfaq_question_detail', 'cloudcmsfaq.urls', (),
170
                    {
171
                     'service': self.service.translation.slug,
172
                     'slug': self.slug,
173
                    })
174
        except Exception, e:
175
            pass
176

    
177
        # ugly hack to fix proper application reverse url
178
        FAQ_URL = ""
179
        try:
180
            FAQ_URL = get_faq_page().get_navigation_url()
181
        except Exception, e:
182
            pass
183

    
184
        if r.startswith(FAQ_URL):
185
            return r
186
        else:
187
            return FAQ_URL + r.lstrip('/')
188

    
189
    def back_url(self):
190
        return get_faq_page().get_navigation_url()
191

    
192

    
193
# Feincms navigation extension
194
class FaqServicesNavigationExtension(NavigationExtension):
195
    """
196
    Navigation extension for FeinCMS which lists all categories that user
197
    wants to include in global site navigation.
198
    """
199

    
200
    name = _('faq categories')
201

    
202
    def children(self, page, **kwargs):
203
        from cloudcms.models import Service
204

    
205
        for service in Service.objects.filter(display_on_menu=True):
206
            url='%sservice/%s/' % (page.get_absolute_url(), service.translation.slug)
207
            yield PagePretender(
208
                title=service.translation.title,
209
                tree_id=page.tree_id,
210
                url=url,
211
                lft=0,
212
                rght=0,
213
                slug=category.translation.slug,
214
            )
215