Beispiel #1
0
 def test_default_choice_of_none_should_be_none(self):
     self.assertIsNone(default_choice(None))
Beispiel #2
0
 def test_default_choice_of_empty_list_should_be_none(self):
     self.assertIsNone(default_choice([]))
Beispiel #3
0
 def test_default_choice_of_choices_with_at_least_one_item_should_be_first_item_label(
         self):
     self.assertEqual(default_choice(self.create_choices(['a', 'b', 'c'])),
                      'a')
Beispiel #4
0
class PageBase(
    DateTimeBase,
    EditableContentMixin,
    SEOMixin,
    LegacyUrlMixin,
    ExcerptMixin
):
    """
    Base class for a default CMS page (with timestamps and with SEO data).
    Derive your own CMS entities from this base class.
    """
    class Meta:
        abstract            = True
        ordering            = ['seq', 'title']
        verbose_name        = 'Page'
        verbose_name_plural = 'Pages'

    class Listing:
        columns = ['title', '_meta_description', 'disabled']
        grid_view = True
        sortable = True
        filter_by = [
            'title',
            'slug',
            'template',
            'disabled'
        ]
        data_export = True
        data_ignore = ['_data']


    def save(self, *args, **kwargs):
        """
        Automatically generate slug based on title if no slug has been provided.
        """
        # slug changed?
        if not self.slug:
            self.slug = slugify(self.title)

        # actually save model
        super(PageBase, self).save(*args, **kwargs)


    title = models.CharField(
        verbose_name='Title',
        max_length=255,
        db_index=True,
        help_text='The title of the page that is displayed on the website.'
    )

    slug = models.SlugField(
        verbose_name='Slug',
        db_index=True,
        max_length=120,
        help_text='The name of the html file for the page as part of the ' +
                  'page''s address.'
    )

    template = models.CharField(
        verbose_name='Template',
        choices=settings.CMS_TEMPLATES,
        default=default_choice(settings.CMS_TEMPLATES),
        max_length=255,
        help_text='The name of the template that is used to render this page.'
    )

    image = models.ForeignKey(
        Media,
        verbose_name='Primary Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        help_text='Select an image that is used to represent this entity ' +
                  'within a list of entities.'
    )

    gallery_images = GenericRelation(
        MediaGallery,
        content_type_field='content_type',
        object_id_field='target_id'
    )

    disabled = models.BooleanField(
        verbose_name='Disabled',
        db_index=True,
        default=False,
        help_text='A disabled web page is not visible to visitors nor to ' +
                  'search engines. A disabled web page will also not show ' +
                  'up in the navigation section(s) of your website.'
    )

    sitemap = models.BooleanField(
        verbose_name='Sitemap Listed',
        default=True,
        help_text='This page is listed within the sitemap.xml file for this website.'
    )

    seq = models.IntegerField(
        verbose_name='Sequence',
        db_index=True,
        default=0,
        editable=False,
        help_text='The sequence number determines the order in which pages ' +
                  'are presented, for example within the navigation ' +
                  'section(s) of your website.'
    )

    nav_updated_on = models.DateTimeField(
        verbose_name='Navigation-relevant data updated timestamp',
        db_index=True,
        null=True,
        blank=False,
        editable=False
    )


    @classmethod
    def get_backend_section_group(cls):
        """
        Return the group name for the section within the backend system or None.
        """
        return get_listing_option(cls, 'group')


    @classmethod
    def get_form(cls):
        from cubane.cms.forms import PageForm
        return PageForm


    @classmethod
    def filter_visibility(cls, objects, visibility_filter_args={}):
        """
        Filter given queryset by visibility. The system will use this method
        whenever content is loaded from the database to check if this content is
        suppose to be visible and will give derived class implementation an
        opportunity to implement custom logic here.
        """
        return objects.filter(disabled=False)


    def is_visible(self):
        """
        Return True, if this page is visible; otherwise return False.
        """
        return not self.disabled


    @property
    def html_title(self):
        """
        Return the page title, which is marked as a safe string, because it
        may contain markup, such as  .
        """
        if self.title == None:
            return ''
        else:
            return mark_safe(self.title.replace('_', ' '))


    @property
    def text_title(self):
        """
        Return the page title as plain text without any markup and underscore
        characters replaced with spaces.
        """
        if self.title == None:
            return ''
        else:
            return self.title.replace('_', ' ')


    @property
    def url(self):
        return self._get_absolute_url()


    @property
    def url_path(self):
        return self._get_absolute_url(path_only=True)


    @property
    def gallery(self):
        """
        Return a (cached) list of gallery images that are assigned to this
        page or child page.
        """
        if not hasattr(self, '_cached_gallery'):
            media = self.gallery_images.select_related('media').order_by('seq')
            self._cached_gallery = list([m.media for m in media])

        return self._cached_gallery


    @property
    def page_path(self):
        """
        Returns a list of all pages with in a hierarchy starting with root element.
        """
        items = []
        p = self
        while p is not None:
            items.insert(0, p)
            p = p.parent
        return items


    def get_filepath(self):
        """
        Return path to cache file for this page.
        """
        return os.path.join(self.slug, 'index.html')


    def get_slug(self):
        """
        Return the slug for this page, which might be empty for the homepage.
        """
        return self.slug


    def get_fullslug(self):
        """
        Return the full slug for this page.
        """
        if self.slug:
            return '/%s/' % self.slug
        else:
            return '/'


    def get_absolute_url(self):
        """
        Return absolute url.
        """
        return self._get_absolute_url()


    def _get_absolute_url(self, path_only=False):
        """
        Return absolute url or url path.
        """
        slug = normalise_slug(self.slug)
        if settings.APPEND_SLASH: slug = slug + '/'
        return get_absolute_url('cubane.cms.page', [slug], path_only=path_only)


    def to_dict(self, extras=None):
        d = {
            'id': self.pk,
            'title': self.title,
            'slug': self.slug,
            'url': self.url,
            'url_path': self.url_path
        }

        if extras != None:
            d.update(extras)

        return d


    def __unicode__(self):
        return u'%s' % self.title