+# Copyright 2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+# 1. Redistributions of source code must retain the above
+# copyright notice, this list of conditions and the following
+# disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following
+# disclaimer in the documentation and/or other materials
+# provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+
+from datetime import datetime
+
+from django.db import models
+from django.conf import settings
+from django.contrib.auth.models import User
+from django.utils.translation import ugettext_lazy as _, ugettext, ungettext
+from django.template.loader import render_to_string
+from django.core import urlresolvers
+
+from feincms import translations
+from feincms.models import Base
+from feincms.module.page.models import Page
+from feincms.content.application.models import reverse
+from feincms.content.richtext.models import RichTextContent
+from feincms.content.section.models import SectionContent
+from feincms.module.medialibrary.fields import MediaFileForeignKey
+from feincms.module.medialibrary.models import MediaFile
+from feincms.module.page.extensions.navigation import NavigationExtension
+from feincms.module.page.extensions.navigation import PagePretender
+from feincms.content.application.models import ApplicationContent
+from feincms.models import Base, create_base_model
+
+from cloudcms.cms_utils import get_app_page
+
+class UserGuideEntryManager(models.Manager):
+
+ def active(self):
+ return self.filter(is_active=True)
+
+ def latest(self, limit=3):
+ return self.filter()[:limit]
+
+
+def get_guide_page():
+ """
+ Returns Page model that has been associated with userguide application
+ """
+ return get_app_page(Page, "cloudcmsguide")
+
+
+try:
+ # MPTT 0.4
+ from mptt.models import MPTTModel
+ mptt_register = False
+ Base = create_base_model(MPTTModel)
+except ImportError:
+ # MPTT 0.3
+ mptt_register = True
+
+class UserGuideEntry(Base):
+ """
+ User guide entry
+ """
+ is_active = models.BooleanField(_('is active'), default=True)
+ is_featured = models.BooleanField(_('is featured'), default=False)
+
+ title = models.CharField(_('title'), max_length=100)
+ slug = models.SlugField(_('slug'), max_length=100, unique_for_date='published_on')
+ author = models.ForeignKey(User, related_name='guide_pages', verbose_name=_('author'))
+ language = models.CharField(max_length=255, choices=settings.LANGUAGES)
+
+ published_on = models.DateTimeField(_('published on'), blank=True, null=True, default=datetime.now,
+ help_text=_('Will be filled in automatically when entry gets published.'))
+ last_changed = models.DateTimeField(_('last change'), auto_now=True, editable=False)
+
+ service = models.ForeignKey('cloudcms.Service', verbose_name=_('service'),
+ related_name='userguideentries', null=True, blank=False)
+
+ parent = models.ForeignKey('self', verbose_name=_('Parent'), blank=True, null=True, related_name='children')
+
+ objects = UserGuideEntryManager()
+
+ class Meta:
+ get_latest_by = 'published_on'
+ ordering = ['service', '-published_on']
+ verbose_name = _('User guide entry')
+ verbose_name_plural = _('User guide entries')
+
+ def __unicode__(self):
+ return self.title
+
+ def get_absolute_url(self):
+ try:
+ r = reverse('cloudcmsguide_entry_detail', 'cloudcmsguide.urls', (),
+ {
+ 'service': self.service.translation.slug,
+ 'slug': self.slug,
+ })
+ except Exception, e:
+ print e
+ return ""
+
+ # ugly hack to fix proper application reverse url
+ GUIDE_URL = ""
+ try:
+ GUIDE_URL = get_guide_page().get_navigation_url()
+ except Exception, e:
+ pass
+
+ if r.startswith(GUIDE_URL):
+ return r
+ else:
+ return GUIDE_URL + r.lstrip('/')
+
+ def back_url(self):
+ return get_guide_page().get_navigation_url()
+
+if mptt_register: # MPTT 0.3 legacy support
+ mptt.register(Page)
+
+UserGuideEntry.register_extensions(
+ 'changedate',
+ 'seo'
+)