1 | from django.db import models
|
---|
2 | from django.contrib.auth.models import User
|
---|
3 | from django.contrib.contenttypes.models import ContentType
|
---|
4 | from django.contrib.contenttypes import generic
|
---|
5 |
|
---|
6 | class Channel(models.Model):
|
---|
7 | creator = models.ForeignKey(User, related_name='owned_channels')
|
---|
8 | users = models.ManyToManyField(User, related_name='subscribed_channels')
|
---|
9 | moderators = models.ManyToManyField(User, related_name='moderated_channels')
|
---|
10 | name = models.CharField(max_length=60, unique=True)
|
---|
11 | topic = models.TextField()
|
---|
12 |
|
---|
13 | post_depth = models.CharField(max_length=3, choices=(("1", "flat"), ("2", "2-deep"), ("inf", "tree")))
|
---|
14 | view_depth = models.CharField(max_length=1, choices=(("0", "titles"),("1", "first-level"), ("2", "second-level")))
|
---|
15 | toplevel_order = models.CharField(max_length=2, choices=(("-", "chronologically descending by-update"),("+", "chronologically ascending by-update"),
|
---|
16 | ("-s", "static, descending"), ("+s", "static, ascending")))
|
---|
17 | child_order = models.CharField(max_length=2, choices=(("-", "chronologically descending by-update"),("+", "chronologically ascending by-update"),
|
---|
18 | ("-s", "static, descending"), ("+s", "static, ascending")))
|
---|
19 |
|
---|
20 | anonymity = models.CharField(max_length=2, choices=(("2", "logged out users may post or view anonymously"),("1", "logged out users may only view anonymously"),
|
---|
21 | ("0", "users must log in to view the channel")))
|
---|
22 | registration = models.CharField(max_length=2, choices=(("2", "unregistered users may post or view"),("1", "unregistered users can only view"),
|
---|
23 | ("0", "unregistered users cannot enter the channel")))
|
---|
24 |
|
---|
25 | posts = generic.GenericRelation('Post')
|
---|
26 |
|
---|
27 | def __unicode__(self):
|
---|
28 | return self.name
|
---|
29 |
|
---|
30 | def sorted_posts(self):
|
---|
31 | print type(self.posts)
|
---|
32 | if self.toplevel_order in ("-","+"):
|
---|
33 | return self.posts.all().order_by('%supdate_time' % self.toplevel_order)
|
---|
34 | elif self.toplevel_order in ("-s", "+s"):
|
---|
35 | return self.posts.all().order_by('%screation_time' % self.toplevel_order[0])
|
---|
36 |
|
---|
37 | class Post(models.Model):
|
---|
38 | author = models.ForeignKey(User, related_name='posts')
|
---|
39 | title = models.CharField(max_length=60)
|
---|
40 | content = models.TextField()
|
---|
41 | creation_time = models.DateTimeField()
|
---|
42 | update_time = models.DateTimeField()
|
---|
43 |
|
---|
44 | parent_type = models.ForeignKey(ContentType)
|
---|
45 | parent_id = models.PositiveIntegerField()
|
---|
46 | parent_object = generic.GenericForeignKey('parent_type', 'parent_id')
|
---|
47 |
|
---|
48 | replies = generic.GenericRelation('self')
|
---|
49 |
|
---|
50 | def __unicode__(self):
|
---|
51 | return self.title
|
---|
52 |
|
---|
53 | def sorted_replies(self):
|
---|
54 | if self.channel.child_order in ("-","+"):
|
---|
55 | return self.replies.all().order_by('%supdate_time' % self.channel.child_order)
|
---|
56 | elif self.channel.child_order in ("-s", "+s"):
|
---|
57 | return self.replies.all().order_by('%screation_time' % self.channel.child_order[0])
|
---|