##// END OF EJS Templates
Moved ban view to class-based
Moved ban view to class-based

File last commit:

r553:fd6431be 1.7-dev
r553:fd6431be 1.7-dev
Show More
__init__.py
193 lines | 5.1 KiB | text/x-python | PythonLexer
__author__ = 'neko259'
import hashlib
from django.core import serializers
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.http.response import HttpResponse
from django.template import RequestContext
from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone
from django.db import transaction
from django.views.decorators.cache import cache_page
from django.views.i18n import javascript_catalog
import boards
from boards.forms import PlainErrorList
from boards.models import Post, Tag, Ban, User
from boards.models.post import SETTING_MODERATE
from boards.models.user import RANK_USER
from boards import authors
import neboard
BAN_REASON_SPAM = 'Autoban: spam bot'
DEFAULT_PAGE = 1
# TODO Maybe this jumper is not needed any more? Only as a hack to find some
# post without knowing its thread
def jump_to_post(request, post_id):
"""Determine thread in which the requested post is and open it's page"""
post = get_object_or_404(Post, id=post_id)
if not post.thread:
return redirect('thread', post_id=post.id)
else:
return redirect(reverse('thread', kwargs={'post_id': post.thread.id})
+ '#' + str(post.id))
def page_404(request):
"""Show page 404 (not found error)"""
context = _init_default_context(request)
return render(request, 'boards/404.html', context)
@transaction.atomic
def tag_subscribe(request, tag_name):
"""Add tag to favorites"""
user = _get_user(request)
tag = get_object_or_404(Tag, name=tag_name)
if not tag in user.fav_tags.all():
user.add_tag(tag)
return _redirect_to_next(request)
@transaction.atomic
def tag_unsubscribe(request, tag_name):
"""Remove tag from favorites"""
user = _get_user(request)
tag = get_object_or_404(Tag, name=tag_name)
if tag in user.fav_tags.all():
user.remove_tag(tag)
return _redirect_to_next(request)
def static_page(request, name):
"""Show a static page that needs only tags list and a CSS"""
context = _init_default_context(request)
return render(request, 'boards/staticpages/' + name + '.html', context)
# TODO This has to be moved under the api module
def api_get_post(request, post_id):
"""
Get the JSON of a post. This can be
used as and API for external clients.
"""
post = get_object_or_404(Post, id=post_id)
json = serializers.serialize("json", [post], fields=(
"pub_time", "_text_rendered", "title", "text", "image",
"image_width", "image_height", "replies", "tags"
))
return HttpResponse(content=json)
@cache_page(86400)
def cached_js_catalog(request, domain='djangojs', packages=None):
return javascript_catalog(request, domain, packages)
# TODO This method is deprecated and should be removed after switching to
# class-based view
def _get_theme(request, user=None):
"""Get user's CSS theme"""
if not user:
user = _get_user(request)
theme = user.get_setting('theme')
if not theme:
theme = neboard.settings.DEFAULT_THEME
return theme
# TODO This method is deprecated and should be removed after switching to
# class-based view
def _init_default_context(request):
"""Create context with default values that are used in most views"""
context = RequestContext(request)
user = _get_user(request)
context['user'] = user
context['tags'] = user.get_sorted_fav_tags()
context['posts_per_day'] = float(Post.objects.get_posts_per_day())
theme = _get_theme(request, user)
context['theme'] = theme
context['theme_css'] = 'css/' + theme + '/base_page.css'
# This shows the moderator panel
moderate = user.get_setting(SETTING_MODERATE)
if moderate == 'True':
context['moderator'] = user.is_moderator()
else:
context['moderator'] = False
return context
# TODO This method is deprecated and should be removed after switching to
# class-based view
def _get_user(request):
"""
Get current user from the session. If the user does not exist, create
a new one.
"""
session = request.session
if not 'user_id' in session:
request.session.save()
md5 = hashlib.md5()
md5.update(session.session_key)
new_id = md5.hexdigest()
while User.objects.filter(user_id=new_id).exists():
md5.update(str(timezone.now()))
new_id = md5.hexdigest()
time_now = timezone.now()
user = User.objects.create(user_id=new_id, rank=RANK_USER,
registration_time=time_now)
# TODO This is just a test. This method should be removed
# _delete_old_users()
session['user_id'] = user.id
else:
user = User.objects.get(id=session['user_id'])
return user
def _redirect_to_next(request):
"""
If a 'next' parameter was specified, redirect to the next page. This is
used when the user is required to return to some page after the current
view has finished its work.
"""
if 'next' in request.GET:
next_page = request.GET['next']
return HttpResponseRedirect(next_page)
else:
return redirect('index')