views.py
124 lines
| 3.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
|
r22 | from boards.forms import NewThreadForm | |
neko259
|
r24 | from boards.models import Post, Admin, Tag | |
neko259
|
r20 | from django.shortcuts import render, get_list_or_404, redirect | |
Ilyas
|
r14 | from django.http import HttpResponseRedirect, Http404 | |
neko259
|
r0 | ||
neko259
|
r22 | ||
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', | |
neko259
|
r22 | context) | |
neko259
|
r0 | ||
neko259
|
r22 | def new_post(request, thread_id=boards.models.NO_PARENT): | |
neko259
|
r4 | """Add a new post (in thread or as a reply).""" | |
neko259
|
r22 | form = NewThreadForm(request.POST, request.FILES) | |
neko259
|
r0 | title = request.POST['title'] | |
text = request.POST['text'] | |||
neko259
|
r22 | ||
if 'image' in request.FILES.keys(): | |||
image = request.FILES['image'] | |||
else: | |||
image = None | |||
neko259
|
r20 | ip = request.META['REMOTE_ADDR'] | |
neko259
|
r0 | ||
neko259
|
r24 | tags = [] | |
if thread_id == boards.models.NO_PARENT: | |||
tag_strings = request.POST['tags'] | |||
if tag_strings: | |||
tag_strings = tag_strings.split(',') | |||
for tag_name in tag_strings: | |||
tag_name = tag_name.strip() | |||
if len(tag_name) > 0: | |||
tag, created = Tag.objects.get_or_create(name=tag_name) | |||
tags.append(tag) | |||
# TODO Add a possibility to define a link image instead of an image file. | |||
# If a link is given, download the image automatically. | |||
neko259
|
r6 | ||
neko259
|
r22 | post = Post.objects.create_post(title=title, text=text, ip=ip, | |
neko259
|
r24 | parent_id=thread_id, image=image, | |
tags=tags) | |||
neko259
|
r4 | ||
neko259
|
r22 | thread_to_show = (post.id if thread_id == boards.models.NO_PARENT else | |
thread_id) | |||
neko259
|
r24 | return redirect(thread, post_id=thread_to_show) | |
neko259
|
r22 | ||
neko259
|
r5 | ||
neko259
|
r24 | def tag(request, tag_name): | |
neko259
|
r4 | """Get all tag threads (posts without a parent).""" | |
neko259
|
r24 | tag = Tag.objects.get(name=tag_name) | |
neko259
|
r27 | threads = Post.objects.get_threads(tag=tag) | |
neko259
|
r5 | ||
neko259
|
r24 | if request.method == 'POST': | |
return new_post(request) | |||
else: | |||
context = RequestContext(request) | |||
context['threads'] = None if len(threads) == 0 else threads | |||
context['tag'] = tag_name | |||
neko259
|
r12 | ||
neko259
|
r24 | context['form'] = forms.NewThreadForm(initial={'tags': tag_name}) | |
return render(request, 'posting_general.html', | |||
context) | |||
neko259
|
r22 | ||
neko259
|
r4 | ||
neko259
|
r24 | def thread(request, post_id): | |
neko259
|
r5 | """Get all thread posts""" | |
neko259
|
r20 | if request.method == 'POST': | |
neko259
|
r24 | return new_post(request, post_id) | |
neko259
|
r20 | else: | |
# TODO Show 404 if there is no such thread | |||
neko259
|
r24 | posts = Post.objects.get_thread(post_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 | ||
neko259
|
r22 | ||
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 | ||
neko259
|
r22 | isAdmin = len(Admin.objects.filter(name=request.POST['name'], | |
password=request.POST[ | |||
'password'])) > 0 | |||
Ilyas
|
r13 | ||
neko259
|
r22 | if isAdmin: | |
Ilyas
|
r9 | request.session['admin'] = True | |
Ilyas
|
r13 | ||
neko259
|
r22 | response = HttpResponseRedirect('/') | |
Ilyas
|
r9 | ||
Ilyas
|
r13 | else: | |
neko259
|
r22 | response = render(request, 'login.html', {'error': 'Login error'}) | |
Ilyas
|
r13 | else: | |
response = render(request, 'login.html', {}) | |||
Ilyas
|
r9 | ||
Ilyas
|
r13 | return response | |
neko259
|
r22 | ||
Ilyas
|
r13 | def logout(request): | |
request.session['admin'] = False | |||
neko259
|
r22 | return HttpResponseRedirect('/') |