|
|
from django.template import RequestContext
|
|
|
from boards import forms
|
|
|
import boards
|
|
|
from boards.forms import ThreadForm, PostForm
|
|
|
from boards.models import Post, Admin, Tag
|
|
|
from django.shortcuts import render, get_list_or_404, redirect
|
|
|
from django.http import HttpResponseRedirect, Http404
|
|
|
|
|
|
|
|
|
def index(request):
|
|
|
context = RequestContext(request)
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
return new_post(request)
|
|
|
else:
|
|
|
threads = Post.objects.get_threads()
|
|
|
|
|
|
context['threads'] = None if len(threads) == 0 else threads
|
|
|
context['form'] = forms.ThreadForm()
|
|
|
|
|
|
return render(request, 'posting_general.html',
|
|
|
context)
|
|
|
|
|
|
|
|
|
def new_post(request, thread_id=boards.models.NO_PARENT):
|
|
|
"""Add a new post (in thread or as a reply)."""
|
|
|
|
|
|
if thread_id == boards.models.NO_PARENT:
|
|
|
form = ThreadForm(request.POST, request.FILES)
|
|
|
else:
|
|
|
form = PostForm(request.POST, request.FILES)
|
|
|
|
|
|
if form.is_valid():
|
|
|
data = form.cleaned_data
|
|
|
else:
|
|
|
return redirect(index)
|
|
|
|
|
|
title = data['title']
|
|
|
text = data['text']
|
|
|
|
|
|
if 'image' in data.keys():
|
|
|
image = data['image']
|
|
|
else:
|
|
|
image = None
|
|
|
|
|
|
ip = request.META['REMOTE_ADDR']
|
|
|
|
|
|
tags = []
|
|
|
if thread_id == boards.models.NO_PARENT:
|
|
|
tag_strings = data['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.
|
|
|
|
|
|
post = Post.objects.create_post(title=title, text=text, ip=ip,
|
|
|
parent_id=thread_id, image=image,
|
|
|
tags=tags)
|
|
|
|
|
|
thread_to_show = (post.id if thread_id == boards.models.NO_PARENT else
|
|
|
thread_id)
|
|
|
return redirect(thread, post_id=thread_to_show)
|
|
|
|
|
|
|
|
|
def tag(request, tag_name):
|
|
|
"""Get all tag threads (posts without a parent)."""
|
|
|
|
|
|
tag = Tag.objects.get(name=tag_name)
|
|
|
threads = Post.objects.get_threads(tag=tag)
|
|
|
|
|
|
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
|
|
|
|
|
|
context['form'] = forms.ThreadForm(initial={'tags': tag_name})
|
|
|
|
|
|
return render(request, 'posting_general.html',
|
|
|
context)
|
|
|
|
|
|
|
|
|
def thread(request, post_id):
|
|
|
"""Get all thread posts"""
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
return new_post(request, post_id)
|
|
|
else:
|
|
|
# TODO Show 404 if there is no such thread
|
|
|
posts = Post.objects.get_thread(post_id)
|
|
|
|
|
|
context = RequestContext(request)
|
|
|
context['posts'] = posts
|
|
|
|
|
|
context['form'] = forms.PostForm()
|
|
|
|
|
|
return render(request, 'thread.html', context)
|
|
|
|
|
|
|
|
|
def login(request):
|
|
|
"""Log in as admin"""
|
|
|
|
|
|
if 'name' in request.POST and 'password' in request.POST:
|
|
|
request.session['admin'] = False
|
|
|
|
|
|
isAdmin = len(Admin.objects.filter(name=request.POST['name'],
|
|
|
password=request.POST[
|
|
|
'password'])) > 0
|
|
|
|
|
|
if isAdmin:
|
|
|
request.session['admin'] = True
|
|
|
|
|
|
response = HttpResponseRedirect('/')
|
|
|
|
|
|
else:
|
|
|
response = render(request, 'login.html', {'error': 'Login error'})
|
|
|
else:
|
|
|
response = render(request, 'login.html', {})
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
def logout(request):
|
|
|
request.session['admin'] = False
|
|
|
return HttpResponseRedirect('/')
|