views.py
95 lines
| 2.5 KiB
| text/x-python
|
PythonLexer
/ boards / views.py
Ilyas
|
r9 | from django.template import RequestContext | ||
Ilyas
|
r14 | from boards import forms | ||
Ilyas
|
r18 | from boards.forms import NewThreadForm | ||
neko259
|
r11 | from boards.models import Post, Admin | ||
Ilyas
|
r14 | from django.shortcuts import render, get_list_or_404 | ||
Ilyas
|
r18 | from django.http import HttpResponseRedirect | ||
neko259
|
r0 | |||
def index(request): | ||||
Ilyas
|
r14 | context = RequestContext(request) | ||
if request.method == 'POST': | ||||
Ilyas
|
r18 | |||
form = NewThreadForm(request.POST) | ||||
if form.is_valid(): | ||||
Post.objects.create_post(form.cleaned_data['title'], | ||||
form.cleaned_data['text'], ip = request.META['REMOTE_ADDR']) | ||||
else: | ||||
context['form'] = form | ||||
Ilyas
|
r14 | |||
neko259
|
r5 | threads = Post.objects.get_threads() | ||
Ilyas
|
r9 | context['threads'] = None if len(threads) == 0 else threads | ||
Ilyas
|
r18 | |||
if 'form' not in context: | ||||
context['form'] = forms.NewThreadForm() | ||||
Ilyas
|
r9 | |||
return render(request, 'posting_general.html', | ||||
context) | ||||
neko259
|
r0 | |||
neko259
|
r4 | def new_post(request): | ||
"""Add a new post (in thread or as a reply).""" | ||||
neko259
|
r0 | title = request.POST['title'] | ||
text = request.POST['text'] | ||||
image = request.POST['image'] | ||||
neko259
|
r6 | # TODO Get tags list, download image (if link is given) | ||
neko259
|
r3 | post = Post.objects.create_post(title = title, text = text, image = image) | ||
neko259
|
r4 | |||
neko259
|
r5 | # TODO Show the thread with a newly created post | ||
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
|
r12 | # TODO Show 404 if there is no such thread | ||
neko259
|
r17 | posts = Post.objects.get_thread(id) | ||
neko259
|
r5 | |||
neko259
|
r12 | context = RequestContext(request) | ||
context['posts'] = posts | ||||
neko259
|
r17 | 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') | ||||