Code layout change.
[wolnelektury.git] / src / chunks / models.py
1 from django.conf import settings
2 from django.db import models
3 from django.utils.translation import ugettext_lazy as _
4 from ssify import flush_ssi_includes
5
6
7 class Chunk(models.Model):
8     """
9     A Chunk is a piece of content associated with a unique key that can be inserted into
10     any template with the use of a special template tag.
11     """
12     key = models.CharField(_('key'), help_text=_('A unique name for this chunk of content'), primary_key=True, max_length=255)
13     description = models.CharField(_('description'), blank=True, max_length=255)
14     content = models.TextField(_('content'), blank=True)
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 save(self, *args, **kwargs):
25         ret = super(Chunk, self).save(*args, **kwargs)
26         self.flush_includes()
27         return ret
28
29     def flush_includes(self):
30         flush_ssi_includes([
31             '/chunks/chunk/%s.%s.html' % (self.key, lang)
32             for lang in [lc for (lc, _ln) in settings.LANGUAGES]])
33
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 __unicode__(self):
45         return self.key