]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/models.py
add caching to the toplevel menu
[sommitrealweird.git] / sommitrealweird / bpcms / models.py
1 from django.db import models
2 from bpcms.content_val import is_allowed_name
3 from django.conf import settings
4
5 FORMAT_CHOICES = (
6         ('rst', 'reStructuredText'),
7         ('html', 'HTML'),
8     )
9
10 class Document(models.Model):
11     title = models.CharField(maxlength=150)
12     islive = models.BooleanField()
13     folder = models.ForeignKey('Folder', null=True, blank=True)
14     format = models.CharField(maxlength=10, choices=FORMAT_CHOICES)
15     slug = models.SlugField(prepopulate_from=("title",), validator_list=[is_allowed_name,])
16     content = models.TextField()
17
18     def __str__(self):
19         return self.__unicode__()
20
21     def __unicode__(self):
22         return u'%s (%s)' %(self.title, self.slug)
23
24     def save(self):
25         super(Document, self).save()
26         # now check if we just set our selves to be live
27         if self.islive:
28             if self.folder:
29                 otherdocs = Document.objects.filter(slug__exact=self.slug, islive__exact=True, folder__exact=self.folder).exclude(pk=self.id)
30             else:
31                 otherdocs = Document.objects.filter(slug__exact=self.slug, islive__exact=True, folder__isnull=True).exclude(pk=self.id)
32             for doc in otherdocs:
33                 doc.islive = False
34                 doc.save()
35
36     def get_basic_url(self):
37         folders = []
38         curfolder = self.folder
39         while curfolder != None:
40             folders.append(curfolder.slug)
41             curfolder = curfolder.parent
42
43         folders.reverse()
44         folderstring = "/".join(folders)
45         if folderstring != u'':
46             folderstring = "%s/" %(folderstring,)
47
48         return "%s%s%s/" %(settings.BPCMS_ROOT, folderstring, self.slug)
49
50     class Admin:
51         pass
52
53 class Folder(models.Model):
54     title = models.CharField(maxlength=150)
55     slug = models.SlugField(prepopulate_from=("title",), validator_list=[is_allowed_name,])
56     parent = models.ForeignKey('self', null=True, blank=True)
57
58     def __str__(self):
59         return self.__unicode__()
60
61     def __unicode__(self):
62         return u'%s' %(self.title)
63
64     def get_basic_url(self):
65         folders = []
66         curfolder = self
67         while curfolder != None:
68             folders.append(curfolder.slug)
69             curfolder = curfolder.parent
70
71         folders.reverse()
72         folderstring = "/".join(folders)
73         if folderstring != u'':
74             folderstring = "%s/" %(folderstring,)
75
76         return "%s%s" %(settings.BPCMS_ROOT, folderstring)
77
78     class Admin:
79         pass