]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/bpcms/models.py
Change / to Home in breadcrumb
[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
5 FORMAT_CHOICES = (
6         ('rst', 'reStructuredText'),
7         ('html', 'HTML'),
8     )
9
10 class Document(models.Model):
11     title = models.CharField(max_length=150)
12     islive = models.BooleanField()
13     folder = models.ForeignKey('Folder', null=True, blank=True)
14     format = models.CharField(max_length=10, choices=FORMAT_CHOICES)
15     slug = models.SlugField()
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 Folder(models.Model):
51     title = models.CharField(max_length=150)
52     slug = models.SlugField()
53     parent = models.ForeignKey('self', null=True, blank=True)
54
55     def __str__(self):
56         return self.__unicode__()
57
58     def __unicode__(self):
59         return u'%s - %s' %(self.get_path(), self.title)
60
61     def get_path(self):
62         folders = []
63         curfolder = self
64         while curfolder != None:
65             folders.append(curfolder.slug)
66             curfolder = curfolder.parent
67
68         folders.reverse()
69         folderstring = "/".join(folders)
70         if folderstring != u'':
71             folderstring = "%s/" %(folderstring,)
72
73         return folderstring
74         
75     def get_basic_url(self):
76         return "%s%s" %(settings.BPCMS_ROOT, self.get_path())