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