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