Uncrazy the caching, more.
[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.utils.translation import ugettext_lazy as _
8
9
10 class Chunk(models.Model):
11     """
12     A Chunk is a piece of content associated with a unique key that can be inserted into
13     any template with the use of a special template tag.
14     """
15     key = models.CharField(_('key'), help_text=_('A unique name for this chunk of content'), primary_key=True,
16                            max_length=255)
17     description = models.CharField(_('description'), blank=True, max_length=255)
18     content = models.TextField(_('content'), blank=True)
19
20     class Meta:
21         ordering = ('key',)
22         verbose_name = _('chunk')
23         verbose_name_plural = _('chunks')
24
25     def __str__(self):
26         return self.key
27
28     def save(self, *args, **kwargs):
29         ret = super(Chunk, self).save(*args, **kwargs)
30         for lc, ln in settings.LANGUAGES:
31             cache.delete('chunk:%s:%s' % (self.key, lc))
32         return ret
33
34
35 class Attachment(models.Model):
36     key = models.CharField(_('key'), help_text=_('A unique name for this attachment'), primary_key=True, max_length=255)
37     attachment = models.FileField(upload_to='chunks/attachment')
38
39     class Meta:
40         ordering = ('key',)
41         verbose_name, verbose_name_plural = _('attachment'), _('attachments')
42
43     def __str__(self):
44         return self.key