Initial commit of userguide app
[snf-cloudcms] / cloudcmsguide / models.py
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.application.models import reverse
48 from feincms.content.richtext.models import RichTextContent
49 from feincms.content.section.models import SectionContent
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 from feincms.models import Base, create_base_model
56
57 from cloudcms.cms_utils import get_app_page
58
59 class UserGuideEntryManager(models.Manager):
60
61     def active(self):
62         return self.filter(is_active=True)
63
64     def latest(self, limit=3):
65         return self.filter()[:limit]
66
67
68 def get_guide_page():
69     """
70     Returns Page model that has been associated with userguide application
71     """
72     return get_app_page(Page, "cloudcmsguide")
73
74
75 try:
76     # MPTT 0.4
77     from mptt.models import MPTTModel
78     mptt_register = False
79     Base = create_base_model(MPTTModel)
80 except ImportError:
81     # MPTT 0.3
82     mptt_register = True
83
84 class UserGuideEntry(Base):
85     """
86     User guide entry
87     """
88     is_active = models.BooleanField(_('is active'), default=True)
89     is_featured = models.BooleanField(_('is featured'), default=False)
90
91     title = models.CharField(_('title'), max_length=100)
92     slug = models.SlugField(_('slug'), max_length=100, unique_for_date='published_on')
93     author = models.ForeignKey(User, related_name='guide_pages', verbose_name=_('author'))
94     language = models.CharField(max_length=255, choices=settings.LANGUAGES)
95
96     published_on = models.DateTimeField(_('published on'), blank=True, null=True, default=datetime.now,
97         help_text=_('Will be filled in automatically when entry gets published.'))
98     last_changed = models.DateTimeField(_('last change'), auto_now=True, editable=False)
99
100     service = models.ForeignKey('cloudcms.Service', verbose_name=_('service'),
101         related_name='userguideentries', null=True, blank=False)
102
103     parent = models.ForeignKey('self', verbose_name=_('Parent'), blank=True, null=True, related_name='children')
104
105     objects = UserGuideEntryManager()
106
107     class Meta:
108         get_latest_by = 'published_on'
109         ordering = ['service', '-published_on']
110         verbose_name = _('User guide entry')
111         verbose_name_plural = _('User guide entries')
112
113     def __unicode__(self):
114         return self.title
115
116     def get_absolute_url(self):
117         try:
118             r = reverse('cloudcmsguide_entry_detail', 'cloudcmsguide.urls', (),
119                     {
120                      'service': self.service.translation.slug,
121                      'slug': self.slug,
122                     })
123         except Exception, e:
124             print e
125             return ""
126
127         # ugly hack to fix proper application reverse url
128         GUIDE_URL = ""
129         try:
130             GUIDE_URL = get_guide_page().get_navigation_url()
131         except Exception, e:
132             pass
133
134         if r.startswith(GUIDE_URL):
135             return r
136         else:
137             return GUIDE_URL + r.lstrip('/')
138
139     def back_url(self):
140         return get_guide_page().get_navigation_url()
141
142 if mptt_register: # MPTT 0.3 legacy support
143     mptt.register(Page)
144
145 UserGuideEntry.register_extensions(
146     'changedate',
147     'seo'
148 )