diff --git a/boards/urls.py b/boards/urls.py --- a/boards/urls.py +++ b/boards/urls.py @@ -1,7 +1,8 @@ from django.conf.urls import patterns, url, include from boards import views from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed -from boards.views import api, tag_threads, all_threads, archived_threads, login +from boards.views import api, tag_threads, all_threads, archived_threads, \ + login, settings js_info_dict = { 'packages': ('boards',), @@ -41,7 +42,7 @@ urlpatterns = patterns('', name='thread'), url(r'^thread/(?P\w+)/(?P\w+)/$', views.thread.ThreadView .as_view(), name='thread_mode'), - url(r'^settings/$', views.settings, name='settings'), + url(r'^settings/$', settings.SettingsView.as_view(), name='settings'), url(r'^tags/$', views.all_tags, name='tags'), url(r'^captcha/', include('captcha.urls')), url(r'^jump/(?P\w+)/$', views.jump_to_post, name='jumper'), diff --git a/boards/views/__init__.py b/boards/views/__init__.py --- a/boards/views/__init__.py +++ b/boards/views/__init__.py @@ -14,14 +14,12 @@ from django.views.decorators.cache impor from django.views.i18n import javascript_catalog import boards -from boards.forms import SettingsForm, PlainErrorList, \ - ModeratorSettingsForm +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 -import all_threads BAN_REASON_SPAM = 'Autoban: spam bot' @@ -29,47 +27,6 @@ BAN_REASON_SPAM = 'Autoban: spam bot' DEFAULT_PAGE = 1 -def settings(request): - """User's settings""" - - context = _init_default_context(request) - user = _get_user(request) - is_moderator = user.is_moderator() - - if request.method == 'POST': - with transaction.atomic(): - if is_moderator: - form = ModeratorSettingsForm(request.POST, - error_class=PlainErrorList) - else: - form = SettingsForm(request.POST, error_class=PlainErrorList) - - if form.is_valid(): - selected_theme = form.cleaned_data['theme'] - - user.save_setting('theme', selected_theme) - - if is_moderator: - moderate = form.cleaned_data['moderate'] - user.save_setting(SETTING_MODERATE, moderate) - - return redirect(settings) - else: - selected_theme = _get_theme(request) - - if is_moderator: - form = ModeratorSettingsForm(initial={'theme': selected_theme, - 'moderate': context['moderator']}, - error_class=PlainErrorList) - else: - form = SettingsForm(initial={'theme': selected_theme}, - error_class=PlainErrorList) - - context['form'] = form - - return render(request, 'boards/settings.html', context) - - def all_tags(request): """All tags list""" diff --git a/boards/views/settings.py b/boards/views/settings.py new file mode 100644 --- /dev/null +++ b/boards/views/settings.py @@ -0,0 +1,49 @@ +from django.db import transaction +from django.shortcuts import render, redirect + +from boards.views.base import BaseBoardView, PARAMETER_FORM +from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList + +class SettingsView(BaseBoardView): + + def get(self, request): + context = self.get_context_data(request=request) + user = context['user'] + is_moderator = user.is_moderator() + + selected_theme = context['theme'] + + if is_moderator: + form = ModeratorSettingsForm(initial={'theme': selected_theme, + 'moderate': context['moderator']}, + error_class=PlainErrorList) + else: + form = SettingsForm(initial={'theme': selected_theme}, + error_class=PlainErrorList) + + context[PARAMETER_FORM] = form + + return render(request, 'boards/settings.html', context) + + def post(self, request): + context = self.get_context_data(request=request) + user = context['user'] + is_moderator = user.is_moderator() + + with transaction.atomic(): + if is_moderator: + form = ModeratorSettingsForm(request.POST, + error_class=PlainErrorList) + else: + form = SettingsForm(request.POST, error_class=PlainErrorList) + + if form.is_valid(): + selected_theme = form.cleaned_data['theme'] + + user.save_setting('theme', selected_theme) + + if is_moderator: + moderate = form.cleaned_data['moderate'] + user.save_setting(SETTING_MODERATE, moderate) + + return redirect('settings')