fixes
[wolnelektury.git] / src / chunks / models.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
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.translation import ugettext_lazy as _
9
10
11 class Chunk(models.Model):
12     """
13     A Chunk is a piece of content associated with a unique key that can be inserted into
14     any template with the use of a special template tag.
15     """
16     key = models.CharField(_('key'), help_text=_('A unique name for this chunk of content'), primary_key=True,
17                            max_length=255)
18     description = models.CharField(_('description'), blank=True, max_length=255)
19     content = models.TextField(_('content'), blank=True)
20
21     class Meta:
22         ordering = ('key',)
23         verbose_name = _('chunk')
24         verbose_name_plural = _('chunks')
25
26     def __str__(self):
27         return self.key
28
29     def save(self, *args, **kwargs):
30         ret = super(Chunk, self).save(*args, **kwargs)
31         for lc, ln in settings.LANGUAGES:
32             cache.delete('chunk:%s:%s' % (self.key, lc))
33         return ret
34
35
36 class Attachment(models.Model):
37     key = models.CharField(_('key'), help_text=_('A unique name for this attachment'), primary_key=True, max_length=255)
38     attachment = models.FileField(upload_to='chunks/attachment')
39
40     class Meta:
41         ordering = ('key',)
42         verbose_name, verbose_name_plural = _('attachment'), _('attachments')
43
44     def __str__(self):
45         return self.key
46
47     def get_absolute_url(self):
48         return reverse('chunks_attachment', args=[self.key, self.attachment.name.rsplit('.', 1)[-1]])