root / cloudcmsfaq / models.py @ 072ce13f
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 |
def faqs_count(self): |
73 |
return self.faqs.count() |
74 |
|
75 |
class Meta: |
76 |
verbose_name = _('category')
|
77 |
verbose_name_plural = _('categories')
|
78 |
ordering = ['ordering',]
|
79 |
|
80 |
objects = translations.TranslatedObjectManager() |
81 |
|
82 |
|
83 |
def __unicode__(self): |
84 |
trans = translations.TranslatedObjectMixin.__unicode__(self)
|
85 |
return trans or _('Unnamed category') |
86 |
|
87 |
|
88 |
class CategoryTranslation(translations.Translation(Category)): |
89 |
"""
|
90 |
Category translation
|
91 |
"""
|
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) |
95 |
|
96 |
class Meta: |
97 |
verbose_name = _('category translation')
|
98 |
verbose_name_plural = _('category translations')
|
99 |
ordering = ['title']
|
100 |
|
101 |
def __unicode__(self): |
102 |
return self.title |
103 |
|
104 |
def save(self, *args, **kwargs): |
105 |
if not self.slug: |
106 |
self.slug = slugify(self.title) |
107 |
|
108 |
super(CategoryTranslation, self).save(*args, **kwargs) |
109 |
|
110 |
|
111 |
class QuestionManager(models.Manager): |
112 |
|
113 |
def active(self): |
114 |
return self.filter(is_active=True) |
115 |
|
116 |
def latest(self, limit=3): |
117 |
return self.filter()[:limit] |
118 |
|
119 |
|
120 |
def get_faq_page(): |
121 |
"""
|
122 |
Returns Page model that has been associated with faq application
|
123 |
"""
|
124 |
return get_app_page(Page, "cloudcmsfaq") |
125 |
|
126 |
class Question(Base): |
127 |
"""
|
128 |
Question/answer entry
|
129 |
"""
|
130 |
is_active = models.BooleanField(_('is active'), default=True) |
131 |
is_featured = models.BooleanField(_('is featured'), default=False) |
132 |
|
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)
|
137 |
|
138 |
application = models.ManyToManyField(Application, |
139 |
related_name="faqs",
|
140 |
verbose_name=_('application'))
|
141 |
|
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) |
145 |
|
146 |
service = models.ForeignKey('cloudcms.Service', verbose_name=_('service'), |
147 |
related_name='faqs', null=True, blank=False) |
148 |
|
149 |
category = models.ForeignKey(Category, verbose_name=_('category'),
|
150 |
related_name='faqs', null=False, blank=False) |
151 |
|
152 |
objects = QuestionManager() |
153 |
|
154 |
|
155 |
class Meta: |
156 |
get_latest_by = 'published_on'
|
157 |
ordering = ['service', 'category__ordering', 'published_on'] |
158 |
verbose_name = _('faq')
|
159 |
verbose_name_plural = _('faqs')
|
160 |
|
161 |
def __unicode__(self): |
162 |
return self.title |
163 |
|
164 |
def get_next(self): |
165 |
return self.get_next_by_published_on(category=self.category, service=self.service) |
166 |
|
167 |
def get_previous(self): |
168 |
return self.get_previous_by_published_on(category=self.category, service=self.service) |
169 |
|
170 |
def get_absolute_url(self): |
171 |
try:
|
172 |
r = reverse('cloudcmsfaq_question_detail', 'cloudcmsfaq.urls', (), |
173 |
{ |
174 |
'service': self.service.translation.slug, |
175 |
'slug': self.slug, |
176 |
}) |
177 |
except Exception, e: |
178 |
pass
|
179 |
|
180 |
# ugly hack to fix proper application reverse url
|
181 |
FAQ_URL = ""
|
182 |
try:
|
183 |
FAQ_URL = get_faq_page().get_navigation_url() |
184 |
except Exception, e: |
185 |
pass
|
186 |
|
187 |
if r.startswith(FAQ_URL):
|
188 |
return r
|
189 |
else:
|
190 |
return FAQ_URL + r.lstrip('/') |
191 |
|
192 |
def back_url(self): |
193 |
return get_faq_page().get_navigation_url()
|
194 |
|
195 |
|
196 |
# Feincms navigation extension
|
197 |
class FaqServicesNavigationExtension(NavigationExtension): |
198 |
"""
|
199 |
Navigation extension for FeinCMS which lists all categories that user
|
200 |
wants to include in global site navigation.
|
201 |
"""
|
202 |
|
203 |
name = _('faq categories')
|
204 |
|
205 |
def children(self, page, **kwargs): |
206 |
from cloudcms.models import Service |
207 |
|
208 |
for service in Service.objects.filter(display_on_menu=True): |
209 |
url='%sservice/%s/' % (page.get_absolute_url(), service.translation.slug)
|
210 |
yield PagePretender(
|
211 |
title=service.translation.title, |
212 |
tree_id=page.tree_id, |
213 |
url=url, |
214 |
lft=0,
|
215 |
rght=0,
|
216 |
slug=category.translation.slug, |
217 |
) |
218 |
|