from django.template import RequestContext from boards import forms from boards.models import Post, Admin from django.shortcuts import render, get_list_or_404 from django.http import HttpResponseRedirect, Http404 def index(request): context = RequestContext(request) if request.method == 'POST': Post.objects.create_post(request.POST['title'], request.POST['text'], ip = request.META['REMOTE_ADDR']) threads = Post.objects.get_threads() context['threads'] = None if len(threads) == 0 else threads context['form'] = forms.NewThreadForm() return render(request, 'posting_general.html', context) def new_post(request): """Add a new post (in thread or as a reply).""" title = request.POST['title'] text = request.POST['text'] image = request.POST['image'] # TODO Get tags list, download image (if link is given) post = Post.objects.create_post(title = title, text = text, image = image) # TODO Show the thread with a newly created post def tag(request): """Get all tag threads (posts without a parent).""" tag_name = request.GET['tag'] threads = get_list_or_404(Post, tag = tag_name) context = RequestContext(request) context['threads'] = None if len(threads) == 0 else threads context['tag'] = tag_name return render(request, 'posting_general.html', context) def thread(request, id): """Get all thread posts""" # TODO Show 404 if there is no such thread posts = Post.objects.get_thread(id) context = RequestContext(request) context['posts'] = posts 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('/boards') 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('/boards')