initial code for www.sommitrealweird.co.uk
[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     class Admin:
36         pass
37
38 class Folder(models.Model):
39     title = models.CharField(maxlength=150)
40     slug = models.SlugField(prepopulate_from=("title",))
41     parent = models.ForeignKey('self', null=True, blank=True)
42
43     def __str__(self):
44         return self.__unicode__()
45
46     def __unicode__(self):
47         return u'%s' %(self.title)
48
49     class Admin:
50         pass