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