Show More
@@ -1,93 +1,90 | |||
|
1 | 1 | from django.template import RequestContext |
|
2 | 2 | from boards import forms |
|
3 | 3 | import boards |
|
4 | 4 | from boards.models import Post, Admin |
|
5 | 5 | from django.shortcuts import render, get_list_or_404, redirect |
|
6 | 6 | from django.http import HttpResponseRedirect, Http404 |
|
7 | 7 | |
|
8 | 8 | def index(request): |
|
9 | 9 | context = RequestContext(request) |
|
10 | 10 | |
|
11 | 11 | if request.method == 'POST': |
|
12 | 12 | return new_post(request) |
|
13 | 13 | else: |
|
14 | 14 | threads = Post.objects.get_threads() |
|
15 | 15 | |
|
16 | 16 | context['threads'] = None if len(threads) == 0 else threads |
|
17 | 17 | context['form'] = forms.NewThreadForm() |
|
18 | 18 | |
|
19 | 19 | return render(request, 'posting_general.html', |
|
20 | 20 | context) |
|
21 | 21 | |
|
22 | 22 | def new_post(request, thread_id = boards.models.NO_PARENT): |
|
23 | 23 | """Add a new post (in thread or as a reply).""" |
|
24 | 24 | |
|
25 | 25 | title = request.POST['title'] |
|
26 | 26 | text = request.POST['text'] |
|
27 | 27 | ip = request.META['REMOTE_ADDR'] |
|
28 | 28 | |
|
29 | 29 | # TODO Get tags list, download image (if link is given) |
|
30 | 30 | |
|
31 | 31 | post = Post.objects.create_post(title = title, text = text, ip = ip, |
|
32 | 32 | parent_id = thread_id) |
|
33 | 33 | |
|
34 |
if thread_id |
|
|
35 | request.method = 'GET' | |
|
36 | return thread(request, thread_id) | |
|
37 | else: | |
|
38 | return redirect(thread, id = post.id) | |
|
34 | thread_to_show = post.id if thread_id == boards.models.NO_PARENT else thread_id | |
|
35 | return redirect(thread, id = thread_to_show) | |
|
39 | 36 | |
|
40 | 37 | def tag(request): |
|
41 | 38 | """Get all tag threads (posts without a parent).""" |
|
42 | 39 | |
|
43 | 40 | tag_name = request.GET['tag'] |
|
44 | 41 | |
|
45 | 42 | threads = get_list_or_404(Post, tag = tag_name) |
|
46 | 43 | |
|
47 | 44 | context = RequestContext(request) |
|
48 | 45 | context['threads'] = None if len(threads) == 0 else threads |
|
49 | 46 | context['tag'] = tag_name |
|
50 | 47 | |
|
51 | 48 | return render(request, 'posting_general.html', |
|
52 | 49 | context) |
|
53 | 50 | |
|
54 | 51 | def thread(request, id): |
|
55 | 52 | """Get all thread posts""" |
|
56 | 53 | |
|
57 | 54 | if request.method == 'POST': |
|
58 | 55 | return new_post(request, id) |
|
59 | 56 | else: |
|
60 | 57 | # TODO Show 404 if there is no such thread |
|
61 | 58 | posts = Post.objects.get_thread(id) |
|
62 | 59 | |
|
63 | 60 | context = RequestContext(request) |
|
64 | 61 | context['posts'] = posts |
|
65 | 62 | |
|
66 | 63 | context['form'] = forms.NewThreadForm() |
|
67 | 64 | |
|
68 | 65 | return render(request, 'thread.html', context) |
|
69 | 66 | |
|
70 | 67 | def login(request): |
|
71 | 68 | """Log in as admin""" |
|
72 | 69 | |
|
73 | 70 | if 'name' in request.POST and 'password' in request.POST: |
|
74 | 71 | request.session['admin'] = False |
|
75 | 72 | |
|
76 | 73 | isAdmin = len(Admin.objects.filter(name = request.POST['name'], |
|
77 | 74 | password = request.POST['password'])) > 0 |
|
78 | 75 | |
|
79 | 76 | if isAdmin : |
|
80 | 77 | request.session['admin'] = True |
|
81 | 78 | |
|
82 | 79 | response = HttpResponseRedirect('/boards') |
|
83 | 80 | |
|
84 | 81 | else: |
|
85 | 82 | response = render(request, 'login.html', {'error' : 'Login error'}) |
|
86 | 83 | else: |
|
87 | 84 | response = render(request, 'login.html', {}) |
|
88 | 85 | |
|
89 | 86 | return response |
|
90 | 87 | |
|
91 | 88 | def logout(request): |
|
92 | 89 | request.session['admin'] = False |
|
93 | 90 | return HttpResponseRedirect('/boards') |
General Comments 0
You need to be logged in to leave comments.
Login now