##// END OF EJS Templates
Renamed model "admins" to "admin".
neko259 -
r11:ad336fa5 default
parent child Browse files
Show More
@@ -1,6 +1,6 b''
1 from django.contrib import admin
1 from django.contrib import admin
2 from boards.models import Post, Tag, Admins
2 from boards.models import Post, Tag, Admin
3
3
4 admin.site.register(Post)
4 admin.site.register(Post)
5 admin.site.register(Tag)
5 admin.site.register(Tag)
6 admin.site.register(Admins)
6 admin.site.register(Admin)
@@ -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 Admin.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 Admin(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,60 +1,62 b''
1 from django.template import RequestContext
1 from django.template import RequestContext
2 from boards.models import Post, Admins
2 from boards.models import Post, Admin
3 from django.shortcuts import render
3 from django.shortcuts import render
4 from django.http import HttpResponseRedirect
4 from django.http import HttpResponseRedirect
5
5
6 def index(request):
6 def index(request):
7 # TODO Show a real index page with all threads list
7 # TODO Show a real index page with all threads list
8 threads = Post.objects.get_threads()
8 threads = Post.objects.get_threads()
9
9
10 context = RequestContext(request)
10 context = RequestContext(request)
11 context['threads'] = None if len(threads) == 0 else threads
11 context['threads'] = None if len(threads) == 0 else threads
12
12
13 return render(request, 'posting_general.html',
13 return render(request, 'posting_general.html',
14 context)
14 context)
15
15
16 def new_post(request):
16 def new_post(request):
17 """Add a new post (in thread or as a reply)."""
17 """Add a new post (in thread or as a reply)."""
18
18
19 title = request.POST['title']
19 title = request.POST['title']
20 text = request.POST['text']
20 text = request.POST['text']
21
21
22 image = request.POST['image']
22 image = request.POST['image']
23
23
24 # TODO Get tags list, download image (if link is given)
24 # TODO Get tags list, download image (if link is given)
25
25
26 post = Post.objects.create_post(title = title, text = text, image = image)
26 post = Post.objects.create_post(title = title, text = text, image = image)
27
27
28 # TODO Show the thread with a newly created post
28 # TODO Show the thread with a newly created post
29
29
30 def tag(request):
30 def tag(request):
31 """Get all tag threads (posts without a parent)."""
31 """Get all tag threads (posts without a parent)."""
32
32
33 tag_name = request.GET['tag']
33 tag_name = request.GET['tag']
34
34
35 posts = Post.objects.get_threads(tag = tag)
35 posts = Post.objects.get_threads(tag = tag)
36
36
37 # TODO Send a response with the post list
37 # TODO Send a response with the post list
38
38
39 def thread(request):
39 def thread(request):
40 """Get all thread posts"""
40 """Get all thread posts"""
41
41
42 opening_post_id = request.GET['id']
42 opening_post_id = request.GET['id']
43 posts = Post.objects.get_thread(opening_post_id)
43 posts = Post.objects.get_thread(opening_post_id)
44
44
45 # TODO Show all posts for the current thread
45 # TODO Show all posts for the current thread
46
46
47 def login(request):
47 def login(request):
48 if 'name' in request.POST and 'password' in request.POST\
48 """Log in as admin"""
49 and Admins.objects.is_admin(request.POST['name'],
49
50 if 'name' in request.POST and 'password' in request.POST \
51 and Admin.objects.is_admin(request.POST['name'],
50 request.POST['password']):
52 request.POST['password']):
51
53
52 if request.POST['name'] == 'ilyas':
54 if request.POST['name'] == 'ilyas':
53 request.session['admin'] = True
55 request.session['admin'] = True
54 return HttpResponseRedirect('/boards')
56 return HttpResponseRedirect('/boards')
55 else:
57 else:
56
58
57 return render(request, 'login.html', {'error' : 'Login error'})
59 return render(request, 'login.html', {'error' : 'Login error'})
58
60
59 else :
61 else :
60 return render(request, 'login.html', {})
62 return render(request, 'login.html', {})
General Comments 0
You need to be logged in to leave comments. Login now