views.py
192 lines
| 5.7 KiB
| text/x-python
|
PythonLexer
/ boards / views.py
|
r41 | from django.core.urlresolvers import reverse | ||
|
r9 | from django.template import RequestContext | ||
|
r14 | from boards import forms | ||
|
r20 | import boards | ||
|
r35 | from boards.forms import ThreadForm, PostForm, SettingsForm | ||
|
r24 | from boards.models import Post, Admin, Tag | ||
|
r20 | from django.shortcuts import render, get_list_or_404, redirect | ||
|
r14 | from django.http import HttpResponseRedirect, Http404 | ||
|
r35 | import neboard | ||
|
r0 | |||
|
r22 | |||
|
r46 | def index(request, page=0): | ||
|
r14 | context = RequestContext(request) | ||
if request.method == 'POST': | ||||
|
r20 | return new_post(request) | ||
else: | ||||
|
r46 | threads = Post.objects.get_threads(page=int(page)) | ||
|
r5 | |||
|
r46 | # TODO Get rid of the duplicate code in index and tag views | ||
|
r20 | context['threads'] = None if len(threads) == 0 else threads | ||
|
r29 | context['form'] = forms.ThreadForm() | ||
|
r57 | context['tags'] = Tag.objects.get_popular_tags() | ||
|
r35 | context['theme'] = _get_theme(request) | ||
|
r46 | context['pages'] = range(Post.objects.get_thread_page_count()) | ||
|
r9 | |||
|
r20 | return render(request, 'posting_general.html', | ||
|
r22 | context) | ||
|
r0 | |||
|
r22 | def new_post(request, thread_id=boards.models.NO_PARENT): | ||
|
r4 | """Add a new post (in thread or as a reply).""" | ||
|
r29 | if thread_id == boards.models.NO_PARENT: | ||
form = ThreadForm(request.POST, request.FILES) | ||||
else: | ||||
form = PostForm(request.POST, request.FILES) | ||||
|
r22 | |||
|
r29 | if form.is_valid(): | ||
data = form.cleaned_data | ||||
else: | ||||
return redirect(index) | ||||
|
r22 | |||
|
r29 | title = data['title'] | ||
text = data['text'] | ||||
if 'image' in data.keys(): | ||||
image = data['image'] | ||||
|
r22 | else: | ||
image = None | ||||
|
r70 | ip = _get_client_ip(request) | ||
|
r0 | |||
|
r24 | tags = [] | ||
|
r41 | |||
new_thread = thread_id == boards.models.NO_PARENT | ||||
if new_thread: | ||||
|
r29 | tag_strings = data['tags'] | ||
|
r24 | |||
if tag_strings: | ||||
|
r35 | tag_strings = tag_strings.split(' ') | ||
|
r24 | 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. | ||||
|
r6 | |||
|
r22 | post = Post.objects.create_post(title=title, text=text, ip=ip, | ||
|
r24 | parent_id=thread_id, image=image, | ||
tags=tags) | ||||
|
r4 | |||
|
r41 | thread_to_show = (post.id if new_thread else thread_id) | ||
if new_thread: | ||||
return redirect(thread, post_id=thread_to_show) | ||||
else: | ||||
return redirect(reverse(thread, | ||||
kwargs={'post_id': thread_to_show}) + '#' | ||||
+ str(post.id)) | ||||
|
r22 | |||
|
r5 | |||
|
r46 | def tag(request, tag_name, page=0): | ||
|
r4 | """Get all tag threads (posts without a parent).""" | ||
|
r24 | tag = Tag.objects.get(name=tag_name) | ||
|
r46 | threads = Post.objects.get_threads(tag=tag, page=int(page)) | ||
|
r5 | |||
|
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 | ||||
|
r57 | context['tags'] = Tag.objects.get_popular_tags() | ||
|
r35 | context['theme'] = _get_theme(request) | ||
|
r46 | context['pages'] = range(Post.objects.get_thread_page_count(tag=tag)) | ||
|
r12 | |||
|
r29 | context['form'] = forms.ThreadForm(initial={'tags': tag_name}) | ||
|
r24 | |||
return render(request, 'posting_general.html', | ||||
context) | ||||
|
r22 | |||
|
r4 | |||
|
r24 | def thread(request, post_id): | ||
|
r5 | """Get all thread posts""" | ||
|
r20 | if request.method == 'POST': | ||
|
r24 | return new_post(request, post_id) | ||
|
r20 | else: | ||
|
r24 | posts = Post.objects.get_thread(post_id) | ||
|
r5 | |||
|
r20 | context = RequestContext(request) | ||
|
r33 | |||
|
r20 | context['posts'] = posts | ||
|
r29 | context['form'] = forms.PostForm() | ||
|
r57 | context['tags'] = Tag.objects.get_popular_tags() | ||
|
r35 | context['theme'] = _get_theme(request) | ||
|
r20 | |||
return render(request, 'thread.html', context) | ||||
|
r9 | |||
|
r22 | |||
|
r9 | def login(request): | ||
|
r11 | """Log in as admin""" | ||
|
r13 | if 'name' in request.POST and 'password' in request.POST: | ||
request.session['admin'] = False | ||||
|
r9 | |||
|
r22 | isAdmin = len(Admin.objects.filter(name=request.POST['name'], | ||
password=request.POST[ | ||||
'password'])) > 0 | ||||
|
r13 | |||
|
r22 | if isAdmin: | ||
|
r9 | request.session['admin'] = True | ||
|
r13 | |||
|
r22 | response = HttpResponseRedirect('/') | ||
|
r9 | |||
|
r13 | else: | ||
|
r22 | response = render(request, 'login.html', {'error': 'Login error'}) | ||
|
r13 | else: | ||
response = render(request, 'login.html', {}) | ||||
|
r9 | |||
|
r13 | return response | ||
|
r22 | |||
|
r13 | def logout(request): | ||
request.session['admin'] = False | ||||
|
r35 | return HttpResponseRedirect('/') | ||
def settings(request): | ||||
context = RequestContext(request) | ||||
if request.method == 'POST': | ||||
form = SettingsForm(request.POST) | ||||
if form.is_valid(): | ||||
selected_theme = form.cleaned_data['theme'] | ||||
request.session['theme'] = selected_theme | ||||
return redirect(settings) | ||||
else: | ||||
selected_theme = _get_theme(request) | ||||
form = SettingsForm(initial={'theme': selected_theme}) | ||||
context['form'] = form | ||||
|
r57 | context['tags'] = Tag.objects.get_popular_tags() | ||
|
r35 | context['theme'] = _get_theme(request) | ||
return render(request, 'settings.html', context) | ||||
|
r57 | def all_tags(request): | ||
context = RequestContext(request) | ||||
context['tags'] = Tag.objects.get_popular_tags() | ||||
context['theme'] = _get_theme(request) | ||||
context['all_tags'] = Tag.objects.get_not_empty_tags() | ||||
return render(request, 'tags.html', context) | ||||
|
r35 | def _get_theme(request): | ||
|
r70 | return request.session.get('theme', neboard.settings.DEFAULT_THEME) | ||
def _get_client_ip(request): | ||||
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') | ||||
if x_forwarded_for: | ||||
ip = x_forwarded_for.split(',')[-1].strip() | ||||
else: | ||||
ip = request.META.get('REMOTE_ADDR') | ||||
|
r74 | return ip | ||