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