Show More
@@ -1,72 +1,78 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 | def exists(self, post_id): | |
|
49 | posts = Post.objects.filter(id = post_id) | |
|
50 | ||
|
51 | return len(posts) == 0 | |
|
52 | ||
|
48 | 53 | |
|
49 | 54 | class Tag(models.Model): |
|
50 | 55 | """ |
|
51 | 56 | A tag is a text node assigned to the post. The tag serves as a board |
|
52 | 57 | section. There can be multiple tags for each message |
|
53 | 58 | """ |
|
54 | 59 | |
|
55 | 60 | name = models.CharField(max_length = 100) |
|
56 | 61 | |
|
62 | ||
|
57 | 63 | class Post(models.Model): |
|
58 | 64 | """A post is a message.""" |
|
59 | 65 | |
|
60 | 66 | objects = PostManager() |
|
61 | 67 | |
|
62 | 68 | title = models.CharField(max_length = 100) |
|
63 | 69 | pub_time = models.DateTimeField() |
|
64 | 70 | text = models.TextField() |
|
65 | 71 | image = models.ImageField(upload_to = DIR_IMAGES) |
|
66 | 72 | poster_ip = models.IPAddressField() |
|
67 | 73 | poster_user_agent = models.TextField() |
|
68 | 74 | parent = models.BigIntegerField() |
|
69 | 75 | tags = models.ManyToManyField(Tag) |
|
70 | 76 | |
|
71 | 77 | def __unicode__(self): |
|
72 | 78 | return self.title + ' (' + self.text + ')' No newline at end of file |
@@ -1,30 +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 | class BoardTests(TestCase): |
|
7 | 8 | |
|
8 | 9 | def create_post(self): |
|
9 | 10 | return Post.objects.create_post(title = 'title', |
|
10 | 11 | text = 'text') |
|
11 | 12 | |
|
12 | 13 | def test_post_add(self): |
|
13 | 14 | post = self.create_post() |
|
14 | 15 | |
|
15 | 16 | self.assertIsNotNone(post) |
|
16 | 17 | self.assertEqual(boards.models.NO_PARENT, post.parent) |
|
17 | 18 | |
|
18 | 19 | def test_delete_post(self): |
|
19 | 20 | post = self.create_post() |
|
21 | post_id = post.id | |
|
20 | 22 | |
|
21 | 23 | Post.objects.delete_post(post) |
|
22 | 24 | |
|
23 |
self.assert |
|
|
25 | self.assertTrue(Post.objects.exists(post_id)) | |
|
24 | 26 | |
|
25 | 27 | def test_delete_posts_by_ip(self): |
|
26 | 28 | post = self.create_post() |
|
29 | post_id = post.id | |
|
27 | 30 | |
|
28 | 31 | Post.objects.delete_posts_by_ip('0.0.0.0') |
|
29 | 32 | |
|
30 |
self.assert |
|
|
33 | self.assertTrue(Post.objects.exists(post_id)) |
General Comments 0
You need to be logged in to leave comments.
Login now