]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/models.py
5df5a437bff6a78d08f27beb03dfec717f621337
[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(default=False)
26     extracss = models.ManyToManyField(CSSDocument, 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     class Meta:
33         ordering = ["folder"]
34
35     def __str__(self):
36         return self.__unicode__()
37
38     def __unicode__(self):
39         return u'%s - %s' %(self.get_basic_url(), self.title)
40
41     def save(self):
42         super(Document, self).save()
43         # now check if we just set our selves to be live
44         if self.islive:
45             if self.folder:
46                 otherdocs = Document.objects.filter(slug__exact=self.slug, islive__exact=True, folder__exact=self.folder).exclude(pk=self.id)
47             else:
48                 otherdocs = Document.objects.filter(slug__exact=self.slug, islive__exact=True, folder__isnull=True).exclude(pk=self.id)
49             for doc in otherdocs:
50                 doc.islive = False
51                 doc.save()
52
53     def get_basic_url(self):
54         folderstring = self.get_folder_path()
55         if folderstring != u'':
56             folderstring = "%s/" %(folderstring,)
57
58         return "%s%s%s/" %(settings.BPCMS_ROOT, folderstring, self.slug)
59
60     def get_folder_path(self):
61         if not self.folder:
62             return ''
63         else:
64             return self.folder.get_path()
65
66     def get_absolute_url(self):
67         if self.slug == 'index':
68             if self.folder.parent:
69                 return reverse('bpcms-document-view', kwargs={'folders': self.folder.parent.get_path(), 'slug': self.folder.slug})
70             else:
71                 return reverse('bpcms-document-view', kwargs={'slug': self.folder.slug})
72         else:
73             if self.folder:
74                 return reverse('bpcms-document-view', kwargs={'folders': self.get_folder_path(), 'slug': self.slug})
75             else:
76                 return reverse('bpcms-document-view', kwargs={'slug': self.slug})
77
78 class Folder(models.Model):
79     title = models.CharField(max_length=150)
80     slug = models.SlugField()
81     parent = models.ForeignKey('self', null=True, blank=True)
82
83     def __str__(self):
84         return self.__unicode__()
85
86     def __unicode__(self):
87         return u'%s - %s' %(self.get_path(), self.title)
88
89     def get_path(self):
90         folders = []
91         curfolder = self
92         while curfolder != None:
93             folders.append(curfolder.slug)
94             curfolder = curfolder.parent
95
96         folders.reverse()
97         folderstring = "/".join(folders)
98         return folderstring
99
100     def save(self, force_insert=False, force_update=False):
101         super(Folder, self).save(force_insert, force_update)
102         if self.parent == None:
103             cache.delete('bpcms_mainmenu')
104         else:
105             cache.delete('bpcms_submenu_%s' %(self.get_path()))
106
107     def get_absolute_url(self):
108         if self.parent:
109             return reverse('bpcms-document-view', kwargs={'folders': self.parent.get_path(), 'slug': self.slug})
110         else:
111             return reverse('bpcms-document-view', kwargs={'slug': self.slug})