forked from zerok/django-flatblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
47 lines (39 loc) · 1.8 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.core.cache import cache
from flatblocks.settings import CACHE_PREFIX
@python_2_unicode_compatible
class FlatBlock(models.Model):
"""
Think of a flatblock as a flatpage but for just part of a site. It's
basically a piece of content with a given name (slug) and an optional
title (header) which you can, for example, use in a sidebar of a website.
"""
slug = models.CharField(max_length=255, unique=True,
verbose_name=_('Slug'),
help_text=_("A unique name used for reference in "
"the templates"))
header = models.CharField(blank=True, null=True, max_length=255,
verbose_name=_('Header'),
help_text=_("An optional header for this "
"content"))
content = models.TextField(verbose_name=_('Content'), blank=True,
null=True)
# Helper attributes used if content should be evaluated in order to
# represent the original content.
raw_content = None
raw_header = None
def __str__(self):
return self.slug
def save(self, *args, **kwargs):
super(FlatBlock, self).save(*args, **kwargs)
# Now also invalidate the cache used in the templatetag
cache.delete('%s%s' % (CACHE_PREFIX, self.slug, ))
def delete(self, *args, **kwargs):
cache_key = '%s%s' % (CACHE_PREFIX, self.slug,)
super(FlatBlock, self).delete(*args, **kwargs)
cache.delete(cache_key)
class Meta:
verbose_name = _('Flat block')
verbose_name_plural = _('Flat blocks')