Show More
@@ -1,101 +1,109 b'' | |||
|
1 | 1 | from django.db import models |
|
2 | 2 | from django.utils import timezone |
|
3 | 3 | |
|
4 | 4 | from neboard import settings |
|
5 | 5 | |
|
6 | 6 | import thumbs |
|
7 | 7 | |
|
8 | 8 | NO_PARENT = -1 |
|
9 | 9 | NO_IP = '0.0.0.0' |
|
10 | 10 | UNKNOWN_UA = '' |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | class PostManager(models.Manager): |
|
14 | 14 | def create_post(self, title, text, image=None, parent_id=NO_PARENT, |
|
15 | 15 | ip=NO_IP, tags=None): |
|
16 | 16 | post = self.create(title=title, |
|
17 | 17 | text=text, |
|
18 | 18 | pub_time=timezone.now(), |
|
19 | 19 | parent=parent_id, |
|
20 | 20 | image=image, |
|
21 | 21 | poster_ip=ip, |
|
22 |
poster_user_agent=UNKNOWN_UA |
|
|
22 | poster_user_agent=UNKNOWN_UA, | |
|
23 | last_edit_time=timezone.now()) | |
|
23 | 24 | |
|
24 | 25 | if tags: |
|
25 | 26 | for tag in tags: |
|
26 | 27 | post.tags.add(tag) |
|
27 | 28 | |
|
29 | if parent_id != NO_PARENT: | |
|
30 | parent = self.get(id=parent_id) | |
|
31 | parent.last_edit_time=timezone.now() | |
|
32 | parent.save() | |
|
33 | ||
|
28 | 34 | return post |
|
29 | 35 | |
|
30 | 36 | def delete_post(self, post): |
|
31 | 37 | children = self.filter(parent=post.id) |
|
32 | 38 | for child in children: |
|
33 | 39 | self.delete_post(child) |
|
34 | 40 | post.delete() |
|
35 | 41 | |
|
36 | 42 | def delete_posts_by_ip(self, ip): |
|
37 | 43 | posts = self.filter(poster_ip=ip) |
|
38 | 44 | for post in posts: |
|
39 | 45 | self.delete_post(post) |
|
40 | 46 | |
|
41 | 47 | def get_threads(self, tag=None): |
|
42 | 48 | if tag is None: |
|
43 | 49 | threads = self.filter(parent=NO_PARENT) |
|
44 | 50 | else: |
|
45 | 51 | threads = self.filter(parent=NO_PARENT, tag=tag) |
|
52 | threads = list(threads.order_by('-last_edit_time')) | |
|
46 | 53 | |
|
47 | 54 | return threads |
|
48 | 55 | |
|
49 | 56 | def get_thread(self, opening_post_id): |
|
50 | 57 | opening_post = self.get(id=opening_post_id) |
|
51 | 58 | replies = self.filter(parent=opening_post_id) |
|
52 | 59 | |
|
53 | 60 | thread = [opening_post] |
|
54 | 61 | thread.extend(replies) |
|
55 | 62 | |
|
56 | 63 | return thread |
|
57 | 64 | |
|
58 | 65 | def exists(self, post_id): |
|
59 | 66 | posts = self.filter(id=post_id) |
|
60 | 67 | |
|
61 | 68 | return len(posts) > 0 |
|
62 | 69 | |
|
63 | 70 | |
|
64 | 71 | class Tag(models.Model): |
|
65 | 72 | """ |
|
66 | 73 | A tag is a text node assigned to the post. The tag serves as a board |
|
67 | 74 | section. There can be multiple tags for each message |
|
68 | 75 | """ |
|
69 | 76 | |
|
70 | 77 | name = models.CharField(max_length=100) |
|
71 | 78 | |
|
72 | 79 | |
|
73 | 80 | class Post(models.Model): |
|
74 | 81 | """A post is a message.""" |
|
75 | 82 | |
|
76 | 83 | objects = PostManager() |
|
77 | 84 | |
|
78 | 85 | title = models.CharField(max_length=100) |
|
79 | 86 | pub_time = models.DateTimeField() |
|
80 | 87 | text = models.TextField() |
|
81 | 88 | image = thumbs.ImageWithThumbsField(upload_to='images/', |
|
82 | 89 | blank=True, sizes=((200, 150),)) |
|
83 | 90 | poster_ip = models.IPAddressField() |
|
84 | 91 | poster_user_agent = models.TextField() |
|
85 | 92 | parent = models.BigIntegerField() |
|
86 | 93 | tags = models.ManyToManyField(Tag) |
|
94 | last_edit_time = models.DateTimeField() | |
|
87 | 95 | |
|
88 | 96 | def __unicode__(self): |
|
89 | 97 | return self.title + ' (' + self.text + ')' |
|
90 | 98 | |
|
91 | 99 | |
|
92 | 100 | class Admin(models.Model): |
|
93 | 101 | """ |
|
94 | 102 | Model for admin users |
|
95 | 103 | """ |
|
96 | 104 | name = models.CharField(max_length=100) |
|
97 | 105 | password = models.CharField(max_length=100) |
|
98 | 106 | |
|
99 | 107 | def __unicode__(self): |
|
100 | 108 | return self.name + '/' + '*' * len(self.password) |
|
101 | 109 |
General Comments 0
You need to be logged in to leave comments.
Login now