X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/ff928e937ca4498faa2b51e9ee818499fbcc8eaf..b39c329ecd604d815de1f772ed9c2e9598db4c46:/src/social/models.py diff --git a/src/social/models.py b/src/social/models.py index 37cfecd24..4c8cd7430 100644 --- a/src/social/models.py +++ b/src/social/models.py @@ -6,7 +6,7 @@ from django.db import models from django.conf import settings from django.core.exceptions import ValidationError from django.urls import reverse -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ugettext_lazy as _, get_language from catalogue.models import Book from wolnelektury.utils import cached_render, clear_cached_renders @@ -123,15 +123,27 @@ class Cite(models.Model): class Carousel(models.Model): - slug = models.SlugField(_('slug'), unique=True) + placement = models.SlugField(_('placement'), choices=[ + ('main', 'main'), + ]) + priority = models.SmallIntegerField(_('priority'), default=0) + language = models.CharField(_('language'), max_length=2, blank=True, default='', choices=settings.LANGUAGES) class Meta: - ordering = ('slug',) +# ordering = ('placement', '-priority') verbose_name = _('carousel') verbose_name_plural = _('carousels') def __str__(self): - return self.slug + return self.placement + + @classmethod + def get(cls, placement): + carousel = cls.objects.filter(models.Q(language='') | models.Q(language=get_language()), placement=placement).order_by('-priority', '?').first() + if carousel is None: + carousel = cls.objects.create(placement=placement) + return carousel + class CarouselItem(models.Model): order = models.PositiveSmallIntegerField(_('order'), unique=True)