settings.py
53 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
|
r547 | from django.db import transaction | ||
from django.shortcuts import render, redirect | ||||
|
r690 | from boards import utils | ||
|
r547 | |||
|
r722 | from boards.views.base import BaseBoardView, CONTEXT_FORM | ||
|
r547 | from boards.forms import SettingsForm, ModeratorSettingsForm, PlainErrorList | ||
|
r565 | from boards.models.post import SETTING_MODERATE | ||
|
r547 | |||
|
r561 | |||
|
r547 | class SettingsView(BaseBoardView): | ||
def get(self, request): | ||||
context = self.get_context_data(request=request) | ||||
|
r690 | user = utils.get_user(request) | ||
|
r547 | is_moderator = user.is_moderator() | ||
|
r690 | selected_theme = utils.get_theme(request, user) | ||
|
r547 | |||
if is_moderator: | ||||
|
r561 | form = ModeratorSettingsForm(initial={ | ||
'theme': selected_theme, | ||||
|
r690 | 'moderate': user.get_setting(SETTING_MODERATE) and \ | ||
user.is_moderator() | ||||
|
r561 | }, error_class=PlainErrorList) | ||
|
r547 | else: | ||
form = SettingsForm(initial={'theme': selected_theme}, | ||||
error_class=PlainErrorList) | ||||
|
r722 | context[CONTEXT_FORM] = form | ||
|
r547 | |||
return render(request, 'boards/settings.html', context) | ||||
def post(self, request): | ||||
|
r690 | user = utils.get_user(request) | ||
|
r547 | 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') | ||||