Source

cmsplugin-tabslider / cmsplugin_tabslider / models.py

Full commit
# -*- 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)"))
    tab_height_correction = models.PositiveSmallIntegerField(_("tab height correction"), default=0,
        help_text=_("This value is substract from calculated tab height (for padding/margin)"))
    tabs = SortedManyToManyField('Tab')

    @property
    def tab_height(self):
        if self.height:
            return self.height / self.tabs.count() - self.tab_height_correction
        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',)