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