1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
35 from datetime import datetime
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
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
56 from cloudcms.models import Application
57 from cloudcms.cms_utils import get_app_page
59 # monkeypatch django reverse (feincms 1.5+ solves this issue)
60 urlresolvers.reverse = reverse
62 class Category(models.Model, translations.TranslatedObjectMixin):
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)
73 return self.faqs.count()
76 verbose_name = _('category')
77 verbose_name_plural = _('categories')
78 ordering = ['ordering',]
80 objects = translations.TranslatedObjectManager()
83 def __unicode__(self):
84 trans = translations.TranslatedObjectMixin.__unicode__(self)
85 return trans or _('Unnamed category')
88 class CategoryTranslation(translations.Translation(Category)):
92 title = models.CharField(_('category title'), max_length=100)
93 slug = models.SlugField(_('slug'), unique=True)
94 description = models.CharField(_('description'), max_length=250, blank=True)
97 verbose_name = _('category translation')
98 verbose_name_plural = _('category translations')
101 def __unicode__(self):
104 def save(self, *args, **kwargs):
106 self.slug = slugify(self.title)
108 super(CategoryTranslation, self).save(*args, **kwargs)
111 class QuestionManager(models.Manager):
114 return self.filter(is_active=True)
116 def latest(self, limit=3):
117 return self.filter()[:limit]
122 Returns Page model that has been associated with faq application
124 return get_app_page(Page, "cloudcmsfaq")
126 class Question(Base):
128 Question/answer entry
130 is_active = models.BooleanField(_('is active'), default=True)
131 is_featured = models.BooleanField(_('is featured'), default=False)
133 title = models.CharField(_('title'), max_length=255)
134 slug = models.SlugField(_('slug'), max_length=100, unique_for_date='published_on')
135 author = models.ForeignKey(User, related_name='faqs', verbose_name=_('author'))
136 language = models.CharField(max_length=255, choices=settings.LANGUAGES)
138 application = models.ManyToManyField(Application,
140 verbose_name=_('application'))
142 published_on = models.DateTimeField(_('published on'), blank=True, null=False, default=datetime.now,
143 help_text=_('Will be filled in automatically when question gets published.'))
144 last_changed = models.DateTimeField(_('last change'), auto_now=True, editable=False)
146 service = models.ForeignKey('cloudcms.Service', verbose_name=_('service'),
147 related_name='faqs', null=True, blank=False)
149 category = models.ForeignKey(Category, verbose_name=_('category'),
150 related_name='faqs', null=False, blank=False)
152 objects = QuestionManager()
156 get_latest_by = 'published_on'
157 ordering = ['service', 'category__ordering', 'published_on']
158 verbose_name = _('faq')
159 verbose_name_plural = _('faqs')
161 def __unicode__(self):
165 return self.get_next_by_published_on(category=self.category, service=self.service)
167 def get_previous(self):
168 return self.get_previous_by_published_on(category=self.category, service=self.service)
170 def get_absolute_url(self):
172 r = reverse('cloudcmsfaq_question_detail', 'cloudcmsfaq.urls', (),
174 'service': self.service.translation.slug,
180 # ugly hack to fix proper application reverse url
183 FAQ_URL = get_faq_page().get_navigation_url()
187 if r.startswith(FAQ_URL):
190 return FAQ_URL + r.lstrip('/')
193 return get_faq_page().get_navigation_url()
196 # Feincms navigation extension
197 class FaqServicesNavigationExtension(NavigationExtension):
199 Navigation extension for FeinCMS which lists all categories that user
200 wants to include in global site navigation.
203 name = _('faq categories')
205 def children(self, page, **kwargs):
206 from cloudcms.models import Service
208 for service in Service.objects.filter(display_on_menu=True):
209 url='%sservice/%s/' % (page.get_absolute_url(), service.translation.slug)
211 title=service.translation.title,
212 tree_id=page.tree_id,
217 slug=category.translation.slug,