initial commit
[emels.git] / chunks / models.py
1 # -*- coding: utf-8 -*-
2 from django.core.cache import cache
3 from django.db import models
4 from django.utils.translation import ugettext_lazy as _
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(
13         _('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     content = models.TextField(_('content'), blank=True)
16
17     class Meta:
18         ordering = ('key',)
19         verbose_name = _('chunk')
20         verbose_name_plural = _('chunks')
21
22     def __unicode__(self):
23         return self.key
24
25     def cache_key(self):
26         return 'chunk_' + self.key
27
28     def save(self, *args, **kwargs):
29         ret = super(Chunk, self).save(*args, **kwargs)
30         cache.delete(self.cache_key())
31         return ret
32
33
34 class Attachment(models.Model):
35     key = models.CharField(_('key'), help_text=_('A unique name for this attachment'), primary_key=True, max_length=255)
36     attachment = models.FileField(upload_to='chunks/attachment')
37
38     class Meta:
39         ordering = ('key',)
40         verbose_name, verbose_name_plural = _('attachment'), _('attachments')
41
42     def __unicode__(self):
43         return self.key