from django.core.urlresolvers import reverse from django.template import RequestContext from django.shortcuts import render, redirect, get_object_or_404 from django.http import HttpResponseRedirect from boards import forms import boards from boards import utils from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ ThreadCaptchaForm, PostCaptchaForm from boards.models import Post, Admin, Tag import neboard def index(request, page=0): context = RequestContext(request) threadFormClass = (ThreadForm if utils.check_if_human(request) else ThreadCaptchaForm) if request.method == 'POST': form = threadFormClass(request.POST, request.FILES, error_class=PlainErrorList) if form.is_valid(): return _new_post(request, form) else: form = threadFormClass(error_class=PlainErrorList) threads = Post.objects.get_threads(page=int(page)) # TODO Get rid of the duplicate code in index and tag views context['threads'] = None if len(threads) == 0 else threads context['form'] = form context['tags'] = Tag.objects.get_popular_tags() context['theme'] = _get_theme(request) context['pages'] = range(Post.objects.get_thread_page_count()) return render(request, 'boards/posting_general.html', context) def _new_post(request, form, thread_id=boards.models.NO_PARENT): """Add a new post (in thread or as a reply).""" data = form.cleaned_data title = data['title'] text = data['text'] if 'image' in data.keys(): image = data['image'] else: image = None ip = _get_client_ip(request) tags = [] new_thread = thread_id == boards.models.NO_PARENT if new_thread: 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 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)) def tag(request, tag_name, page=0): """Get all tag threads (posts without a parent).""" tag = get_object_or_404(Tag, name=tag_name) threads = Post.objects.get_threads(tag=tag, page=int(page)) if request.method == 'POST': form = ThreadForm(request.POST, request.FILES, error_class=PlainErrorList) if form.is_valid(): return _new_post(request, form) else: form = forms.ThreadForm(initial={'tags': tag_name}, error_class=PlainErrorList) context = RequestContext(request) context['threads'] = None if len(threads) == 0 else threads context['tag'] = tag_name context['tags'] = Tag.objects.get_popular_tags() context['theme'] = _get_theme(request) context['pages'] = range(Post.objects.get_thread_page_count(tag=tag)) context['form'] = form return render(request, 'boards/posting_general.html', context) def thread(request, post_id): """Get all thread posts""" postFormClass = (PostForm if utils.check_if_human(request) else PostCaptchaForm) if request.method == 'POST': form = postFormClass(request.POST, request.FILES, error_class=PlainErrorList) if form.is_valid(): return _new_post(request, form, post_id) else: form = postFormClass(error_class=PlainErrorList) posts = Post.objects.get_thread(post_id) context = RequestContext(request) context['posts'] = posts context['form'] = form context['tags'] = Tag.objects.get_popular_tags() context['theme'] = _get_theme(request) return render(request, 'boards/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, 'boards/login.html', {'error': 'Login error'}) else: response = render(request, 'boards/login.html', {}) return response def logout(request): request.session['admin'] = False 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 context['tags'] = Tag.objects.get_popular_tags() context['theme'] = _get_theme(request) return render(request, 'boards/settings.html', context) 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, 'boards/tags.html', context) def _get_theme(request): 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') return ip