cmsplugin-tabslider / cmsplugin_tabslider / models.py

# -*- coding: utf-8 -*-
from filer.fields.folder import FilerFolderField
import os
from cms.models import CMSPlugin
from cms.models.fields import PageField
from django.db import models
from filer.fields.file import FilerFileField
from filer.fields.image import FilerImageField
from django.utils.translation import ugettext_lazy as _
from sortedm2m.fields import SortedManyToManyField
from django.contrib.staticfiles.finders import find as staticfiles_find

EFFECTS = """
    sliceDown
    sliceDownLeft
    sliceUp
    sliceUpLeft
    sliceUpDown
    sliceUpDownLeft
    fold
    fade
    random
    slideInRight
    slideInLeft
    boxRandom
    boxRain
    boxRainReverse
    boxRainGrow
    boxRainGrowReverse
"""
EFFECT_CHOICES = ((e, e) for e in EFFECTS.split())

def find_themes():
    themedirs = staticfiles_find("nivo/themes/", all=True)
    for dir in themedirs:
        for theme in os.listdir(dir):
            yield theme

THEME_CHOICES = ((t, t) for t in set(find_themes()))

class TabSlider(CMSPlugin):
    tab_switch_timeout = models.SmallIntegerField(help_text=_("miliseconds"), default=6000)
    width = models.PositiveIntegerField(_('width'), null=True, blank=True,
        help_text=_("Width of the plugin (px)"))
    height = models.PositiveIntegerField(_('height'), null=True, blank=True,
        help_text=_("Height of the plugin (px)"))

    tabs = SortedManyToManyField('Tab')

    @property
    def tab_height(self):
        if self.height:
            return self.height / self.tabs.count()
        return 0

class Tab(models.Model):
    title = models.CharField(_('title'), max_length=255, null=True, blank=True)
    album = FilerFolderField(verbose_name=_('album'))
    theme = models.CharField(_('theme'), choices=THEME_CHOICES, max_length=50,
        default="default")
    effect = models.CharField(_('effect'), choices=EFFECT_CHOICES, max_length=50,
        default="random")
    manual_advance = models.BooleanField(_('manual advance'))
    anim_speed = models.PositiveIntegerField(_('anim speed'), default=500,
        help_text=_("Animation Speed (ms)"))
    pause_time = models.PositiveIntegerField(_('pause time'), default=3000,
        help_text=_("Pause time (ms)"))
    arrows = models.BooleanField(_('arrows'), default=True,
        help_text=_('Arrow buttons for navigation'))
    thumbnails = models.BooleanField(_('thumbnails'),
        help_text=_('Thumbnails for navigation [only '
                    'works with the default theme!]'))
    random_start = models.BooleanField(_('random start'))
    pause_on_hover = models.BooleanField(_('pause on mouse hover'), default=True)

    def __unicode__(self):
        if self.title:
            return self.title
        return unicode(self.album)

    @property
    def images(self):
        if not hasattr(self, '__images'):
            files = self.album.files
            self.__images = [f for f in files if f.file_type == 'Image']
            self.__images.sort()
        return self.__images

    @property
    def size(self):
        if self.width and self.height:
            return self.width, self.height

    search_fields = ('title', 'album__title',)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.