]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/models.py
Do some ordering in the admin interface.
[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 from django.core.urlresolvers import reverse
6
7 FORMAT_CHOICES = (
8         ('rst', 'reStructuredText'),
9         ('html', 'HTML'),
10     )
11
12 class CSSDocument(models.Model):
13     title = models.CharField(max_length=150)
14     slug = models.SlugField()
15     content = models.TextField()
16
17     def __str__(self):
18         return self.__unicode__()
19
20     def __unicode__(self):
21         return u'%s' %(self.title,)
22
23 class Document(models.Model):
24     title = models.CharField(max_length=150)
25     islive = models.BooleanField()
26     extracss = models.ManyToManyField(CSSDocument, null=True, blank=True)
27     folder = models.ForeignKey('Folder', null=True, blank=True)
28     format = models.CharField(max_length=10, choices=FORMAT_CHOICES)
29     slug = models.SlugField()
30     content = models.TextField()
31
32     def __str__(self):
33         return self.__unicode__()
34
35     def __unicode__(self):
36         return u'%s (%s)' %(self.title, self.slug)
37
38     def save(self):
39         super(Document, self).save()
40         # now check if we just set our selves to be live
41         if self.islive:
42             if self.folder:
43                 otherdocs = Document.objects.filter(slug__exact=self.slug, islive__exact=True, folder__exact=self.folder).exclude(pk=self.id)
44             else:
45                 otherdocs = Document.objects.filter(slug__exact=self.slug, islive__exact=True, folder__isnull=True).exclude(pk=self.id)
46             for doc in otherdocs:
47                 doc.islive = False
48                 doc.save()
49
50     def get_basic_url(self):
51         folderstring = self.get_folder_path()
52         if folderstring != u'':
53             folderstring = "%s/" %(folderstring,)
54
55         return "%s%s%s/" %(settings.BPCMS_ROOT, folderstring, self.slug)
56
57     def get_folder_path(self):
58         if not self.folder:
59             return ''
60         else:
61             return self.folder.get_path()
62
63     def get_absolute_url(self):
64         if self.slug == 'index':
65             if self.folder.parent:
66                 return reverse('bpcms.views.document_view', kwargs={'folders': self.folder.parent.get_path(), 'slug': self.folder.slug})
67             else:
68                 return reverse('bpcms.views.document_view', kwargs={'slug': self.folder.slug})
69         else:
70             if self.folder:
71                 return reverse('bpcms.views.document_view', kwargs={'folders': self.get_folder_path(), 'slug': self.slug})
72             else:
73                 return reverse('bpcms.views.document_view', kwargs={'slug': self.slug})
74
75 class Folder(models.Model):
76     title = models.CharField(max_length=150)
77     slug = models.SlugField()
78     parent = models.ForeignKey('self', null=True, blank=True)
79
80     def __str__(self):
81         return self.__unicode__()
82
83     def __unicode__(self):
84         return u'%s - %s' %(self.get_path(), self.title)
85
86     def get_path(self):
87         folders = []
88         curfolder = self
89         while curfolder != None:
90             folders.append(curfolder.slug)
91             curfolder = curfolder.parent
92         
93         folders.reverse()
94         folderstring = "/".join(folders)
95         return folderstring
96     
97     def save(self, force_insert=False, force_update=False):
98         super(Folder, self).save(force_insert, force_update)
99         if self.parent == None:
100             cache.delete('bpcms_mainmenu')
101         else:
102             cache.delete('bpcms_submenu_%s' %(self.get_path()))
103
104     def get_absolute_url(self):
105         if self.parent:
106             return reverse('bpcms.views.document_view', kwargs={'folders': self.parent.get_path(), 'slug': self.slug})
107         else:
108             return reverse('bpcms.views.document_view', kwargs={'slug': self.slug})