views.py
93 lines
| 2.6 KiB
| text/x-python
|
PythonLexer
/ boards / views.py
Ilyas
|
r9 | from django.template import RequestContext | ||
Ilyas
|
r14 | from boards import forms | ||
neko259
|
r20 | import boards | ||
neko259
|
r11 | from boards.models import Post, Admin | ||
neko259
|
r20 | from django.shortcuts import render, get_list_or_404, redirect | ||
Ilyas
|
r14 | from django.http import HttpResponseRedirect, Http404 | ||
neko259
|
r0 | |||
def index(request): | ||||
Ilyas
|
r14 | context = RequestContext(request) | ||
if request.method == 'POST': | ||||
neko259
|
r20 | return new_post(request) | ||
else: | ||||
threads = Post.objects.get_threads() | ||||
neko259
|
r5 | |||
neko259
|
r20 | context['threads'] = None if len(threads) == 0 else threads | ||
context['form'] = forms.NewThreadForm() | ||||
Ilyas
|
r9 | |||
neko259
|
r20 | return render(request, 'posting_general.html', | ||
context) | ||||
neko259
|
r0 | |||
neko259
|
r20 | def new_post(request, thread_id = boards.models.NO_PARENT): | ||
neko259
|
r4 | """Add a new post (in thread or as a reply).""" | ||
neko259
|
r0 | title = request.POST['title'] | ||
text = request.POST['text'] | ||||
neko259
|
r20 | ip = request.META['REMOTE_ADDR'] | ||
neko259
|
r0 | |||
neko259
|
r6 | # TODO Get tags list, download image (if link is given) | ||
neko259
|
r20 | post = Post.objects.create_post(title = title, text = text, ip = ip, | ||
parent_id = thread_id) | ||||
neko259
|
r4 | |||
neko259
|
r20 | if thread_id != boards.models.NO_PARENT: | ||
request.method = 'GET' | ||||
return thread(request, thread_id) | ||||
else: | ||||
return redirect(thread, id = post.id) | ||||
neko259
|
r5 | |||
neko259
|
r4 | def tag(request): | ||
"""Get all tag threads (posts without a parent).""" | ||||
tag_name = request.GET['tag'] | ||||
Ilyas
|
r14 | threads = get_list_or_404(Post, tag = tag_name) | ||
neko259
|
r5 | |||
neko259
|
r12 | context = RequestContext(request) | ||
context['threads'] = None if len(threads) == 0 else threads | ||||
context['tag'] = tag_name | ||||
return render(request, 'posting_general.html', | ||||
context) | ||||
neko259
|
r4 | |||
neko259
|
r17 | def thread(request, id): | ||
neko259
|
r5 | """Get all thread posts""" | ||
neko259
|
r20 | if request.method == 'POST': | ||
return new_post(request, id) | ||||
else: | ||||
# TODO Show 404 if there is no such thread | ||||
posts = Post.objects.get_thread(id) | ||||
neko259
|
r5 | |||
neko259
|
r20 | context = RequestContext(request) | ||
context['posts'] = posts | ||||
neko259
|
r12 | |||
neko259
|
r20 | context['form'] = forms.NewThreadForm() | ||
return render(request, 'thread.html', context) | ||||
Ilyas
|
r9 | |||
def login(request): | ||||
neko259
|
r11 | """Log in as admin""" | ||
Ilyas
|
r13 | if 'name' in request.POST and 'password' in request.POST: | ||
request.session['admin'] = False | ||||
Ilyas
|
r9 | |||
Ilyas
|
r13 | isAdmin = len(Admin.objects.filter(name = request.POST['name'], | ||
password = request.POST['password'])) > 0 | ||||
if isAdmin : | ||||
Ilyas
|
r9 | request.session['admin'] = True | ||
Ilyas
|
r13 | |||
response = HttpResponseRedirect('/boards') | ||||
Ilyas
|
r9 | |||
Ilyas
|
r13 | else: | ||
response = render(request, 'login.html', {'error' : 'Login error'}) | ||||
else: | ||||
response = render(request, 'login.html', {}) | ||||
Ilyas
|
r9 | |||
Ilyas
|
r13 | return response | ||
def logout(request): | ||||
request.session['admin'] = False | ||||
return HttpResponseRedirect('/boards') | ||||