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