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