Show More
@@ -3,54 +3,54 b' from django.utils import timezone' | |||||
3 | from django.conf import settings |
|
3 | from django.conf import settings | |
4 |
|
4 | |||
5 | class BoardManager(models.Manager): |
|
5 | class BoardManager(models.Manager): | |
6 |
def create_thread(self |
|
6 | def create_thread(self): | |
7 |
|
|
7 | thread = self.create(posts=0, pub_date=timezone.now()) | |
8 |
|
8 | |||
9 |
|
|
9 | return thread | |
10 |
|
10 | |||
11 | def create_post(self, title, text, thread, image): |
|
11 | def create_post(self, title, text, thread, image): | |
12 |
|
|
12 | post = self.create(title=title, text=text, pub_date=timezone.now(), | |
13 |
|
|
13 | thread=thread, image=image, poster_ip='0.0.0.0', | |
14 |
|
|
14 | poster_user_agent='') | |
15 |
|
|
15 | thread.posts += 1 | |
16 |
|
16 | |||
17 |
|
|
17 | return post | |
18 |
|
18 | |||
19 | def delete_post(self, post): |
|
19 | def delete_post(self, post): | |
20 |
|
|
20 | thread = post.thread | |
21 |
|
|
21 | post.delete() | |
22 |
|
|
22 | thread.posts -= 1 | |
23 |
|
23 | |||
24 |
|
|
24 | if thread.is_empty: | |
25 |
|
|
25 | thread.delete() | |
26 |
|
26 | |||
27 | def delete_posts_by_ip(self, ip): |
|
27 | def delete_posts_by_ip(self, ip): | |
28 |
|
|
28 | posts = self.filter(poster_ip=ip) | |
29 |
|
|
29 | for post in posts: | |
30 |
|
|
30 | self.delete_post(post) | |
31 |
|
31 | |||
32 | class Board(models.Model): |
|
32 | class Board(models.Model): | |
33 | name = models.CharField(max_length=10) |
|
33 | name = models.CharField(max_length=10) | |
34 | description = models.CharField(max_length=50) |
|
34 | description = models.CharField(max_length=50) | |
35 |
|
35 | |||
36 | def __unicode__(self): |
|
36 | def __unicode__(self): | |
37 |
|
|
37 | return self.name | |
38 |
|
38 | |||
39 | class Thread(models.Model): |
|
39 | class Thread(models.Model): | |
40 | objects = BoardManager() |
|
40 | objects = BoardManager() | |
41 |
|
41 | |||
42 |
board = models. |
|
42 | boards = models.ManyToManyField(Board) | |
43 | posts = models.IntegerField() |
|
43 | posts = models.IntegerField() | |
44 | pub_date = models.DateTimeField() |
|
44 | pub_date = models.DateTimeField() | |
45 |
|
45 | |||
46 | def __unicode__(self): |
|
46 | def __unicode__(self): | |
47 |
|
|
47 | return str(self.id) | |
48 |
|
48 | |||
49 | def is_dead(self): |
|
49 | def is_dead(self): | |
50 |
|
|
50 | return self.posts >= settings.MAX_POSTS_PER_THREAD | |
51 |
|
51 | |||
52 | def is_empty(self): |
|
52 | def is_empty(self): | |
53 |
|
|
53 | return self.posts == 0 | |
54 |
|
54 | |||
55 | class Post(models.Model): |
|
55 | class Post(models.Model): | |
56 | objects = BoardManager() |
|
56 | objects = BoardManager() | |
@@ -59,9 +59,9 b' class Post(models.Model):' | |||||
59 | pub_date = models.DateTimeField() |
|
59 | pub_date = models.DateTimeField() | |
60 | text = models.TextField() |
|
60 | text = models.TextField() | |
61 | thread = models.ForeignKey(Thread) |
|
61 | thread = models.ForeignKey(Thread) | |
62 |
image = models.ImageField(upload_to= |
|
62 | image = models.ImageField(upload_to=settings.IMAGES_PATH) | |
63 | poster_ip = models.IPAddressField() |
|
63 | poster_ip = models.IPAddressField() | |
64 | poster_user_agent = models.TextField() |
|
64 | poster_user_agent = models.TextField() | |
65 |
|
65 | |||
66 | def __unicode__(self): |
|
66 | def __unicode__(self): | |
67 |
|
|
67 | return self.title + ' (' + self.text + ')' No newline at end of file |
@@ -1,5 +1,4 b'' | |||||
1 | from django.test import TestCase |
|
1 | from django.test import TestCase | |
2 | from django.utils import timezone |
|
|||
3 | from django.conf import settings |
|
2 | from django.conf import settings | |
4 |
|
3 | |||
5 | from boards.models import Board, Thread, Post |
|
4 | from boards.models import Board, Thread, Post | |
@@ -7,58 +6,61 b' from boards.models import Board, Thread,' | |||||
7 | class BoardTests(TestCase): |
|
6 | class BoardTests(TestCase): | |
8 |
|
7 | |||
9 | def create_board(self): |
|
8 | def create_board(self): | |
10 |
|
|
9 | return Board.objects.create(name='t', description='test') | |
11 |
|
10 | |||
12 | def create_thread(self, board): |
|
11 | def create_thread(self, board): | |
13 |
|
|
12 | thread = Thread.objects.create_thread() | |
|
13 | thread.boards.add(board) | |||
|
14 | ||||
|
15 | return thread | |||
14 |
|
16 | |||
15 | def create_post(self, thread): |
|
17 | def create_post(self, thread): | |
16 |
|
|
18 | return Post.objects.create_post(title='title', | |
17 |
|
|
19 | text='text', thread=thread, image=None) | |
18 |
|
20 | |||
19 | def test_add_thread(self): |
|
21 | def test_add_thread(self): | |
20 |
|
|
22 | thread = self.create_thread(self.create_board()) | |
21 |
|
23 | |||
22 | self.assertIsNotNone(thread) |
|
24 | self.assertIsNotNone(thread) | |
23 |
|
25 | |||
24 | def test_post_add(self): |
|
26 | def test_post_add(self): | |
25 |
|
|
27 | thread = self.create_thread(self.create_board()) | |
26 |
|
|
28 | post = self.create_post(thread) | |
27 |
|
29 | |||
28 |
|
|
30 | self.assertIsNotNone(post) | |
29 |
|
|
31 | self.assertEqual(thread, post.thread) | |
30 |
|
32 | |||
31 | def test_thread_post_count(self): |
|
33 | def test_thread_post_count(self): | |
32 |
|
|
34 | thread = self.create_thread(self.create_board()) | |
33 |
|
|
35 | self.create_post(thread) | |
34 |
|
36 | |||
35 |
|
|
37 | self.assertEqual(1, thread.posts) | |
36 |
|
38 | |||
37 | def test_thread_dead(self): |
|
39 | def test_thread_dead(self): | |
38 |
|
|
40 | thread = self.create_thread(self.create_board()) | |
39 |
|
41 | |||
40 |
|
|
42 | for i in range(0, settings.MAX_POSTS_PER_THREAD): | |
41 |
|
|
43 | self.create_post(thread) | |
42 |
|
44 | |||
43 |
|
|
45 | self.assertTrue(thread.is_dead()) | |
44 |
|
46 | |||
45 | def test_thread_empty(self): |
|
47 | def test_thread_empty(self): | |
46 |
|
|
48 | thread = self.create_thread(self.create_board()) | |
47 |
|
49 | |||
48 |
|
|
50 | self.assertTrue(thread.is_empty) | |
49 |
|
51 | |||
50 | def test_delete_post(self): |
|
52 | def test_delete_post(self): | |
51 |
|
|
53 | thread = self.create_thread(self.create_board()) | |
52 |
|
|
54 | post = self.create_post(thread) | |
53 |
|
55 | |||
54 |
|
|
56 | Post.objects.delete_post(post) | |
55 |
|
57 | |||
56 |
|
|
58 | self.assertTrue(thread.is_empty()) | |
57 |
|
59 | |||
58 | def test_delete_posts_by_ip(self): |
|
60 | def test_delete_posts_by_ip(self): | |
59 |
|
|
61 | thread = self.create_thread(self.create_board()) | |
60 |
|
|
62 | self.create_post(thread) | |
61 |
|
63 | |||
62 |
|
|
64 | Post.objects.delete_posts_by_ip('0.0.0.0') | |
63 |
|
65 | |||
64 |
|
|
66 | self.assertTrue(thread.is_empty()) |
General Comments 0
You need to be logged in to leave comments.
Login now