1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.conf import settings
5 from django.core.cache import cache
6 from django.db import models
7 from django.urls import reverse
8 from django.utils.safestring import mark_safe
9 from django.utils.translation import gettext_lazy as _
12 class Chunk(models.Model):
14 A Chunk is a piece of content associated with a unique key that can be inserted into
15 any template with the use of a special template tag.
17 key = models.CharField(_('key'), help_text=_('A unique name for this chunk of content'), primary_key=True,
19 description = models.CharField(_('description'), blank=True, max_length=255)
20 content = models.TextField(_('content'), blank=True)
24 verbose_name = _('chunk')
25 verbose_name_plural = _('chunks')
30 def save(self, *args, **kwargs):
31 ret = super(Chunk, self).save(*args, **kwargs)
32 for lc, ln in settings.LANGUAGES:
33 cache.delete('chunk:%s:%s' % (self.key, lc))
37 class Attachment(models.Model):
38 key = models.CharField(_('key'), help_text=_('A unique name for this attachment'), primary_key=True, max_length=255)
39 attachment = models.FileField(upload_to='chunks/attachment')
43 verbose_name, verbose_name_plural = _('attachment'), _('attachments')
48 def get_absolute_url(self):
49 return reverse('chunks_attachment', args=[self.key, self.attachment.name.rsplit('.', 1)[-1]])
52 class Menu(models.Model):
53 identifier = models.CharField(max_length=255, unique=True)
56 return self.identifier
59 class MenuItem(models.Model):
60 menu = models.ForeignKey(Menu, models.CASCADE)
61 order = models.SmallIntegerField()
62 highlight = models.BooleanField()
63 infopage = models.ForeignKey(
64 'infopages.InfoPage', models.PROTECT, null=True, blank=True)
65 url = models.CharField(max_length=255, blank=True)
66 name = models.CharField(max_length=255, blank=True)
74 return mark_safe('<hr>')
78 return self.infopage.title
84 return self.infopage.get_absolute_url()
89 return self.url or self.infopage