update
[prawokultury.git] / chunks / models.py
1 from django.core.cache import cache
2 from django.db import models
3 from django.utils.translation import ugettext_lazy as _
4 from markupfield.fields import MarkupField
5 from fnpdjango.utils.models.translation import add_translatable
6
7
8 class Chunk(models.Model):
9     """
10     A Chunk is a piece of content associated with a unique key that can be inserted into
11     any template with the use of a special template tag.
12     """
13     key = models.CharField(_('key'), help_text=_('A unique name for this chunk of content'), primary_key=True, max_length=255)
14     description = models.CharField(_('description'), blank=True, max_length=255)
15
16     class Meta:
17         ordering = ('key',)
18         verbose_name = _('chunk')
19         verbose_name_plural = _('chunks')
20
21     def __unicode__(self):
22         return self.key
23
24     def cache_key(self):
25         return 'chunk_' + self.key
26
27     def save(self, *args, **kwargs):
28         ret = super(Chunk, self).save(*args, **kwargs)
29         cache.delete(self.cache_key())
30         return ret
31
32 add_translatable(Chunk, {
33     'content': MarkupField(_('content'), blank=True, markup_type='textile_pl',
34         help_text=_('Use <a href="http://txstyle.org/">Textile</a> syntax.')),
35 })
36
37
38 class Attachment(models.Model):
39     key = models.CharField(_('key'), help_text=_('A unique name for this attachment'), primary_key=True, max_length=255)
40     attachment = models.FileField(upload_to='chunks/attachment')
41
42     class Meta:
43         ordering = ('key',)
44         verbose_name, verbose_name_plural = _('attachment'), _('attachments')
45
46     def __unicode__(self):
47         return self.key