settings.py
59 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
neko259
|
r547 | from django.db import transaction | ||
from django.shortcuts import render, redirect | ||||
neko259
|
r990 | from boards.abstracts.settingsmanager import get_settings_manager, \ | ||
SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID | ||||
neko259
|
r722 | from boards.views.base import BaseBoardView, CONTEXT_FORM | ||
neko259
|
r729 | from boards.forms import SettingsForm, PlainErrorList | ||
neko259
|
r547 | |||
neko259
|
r900 | FORM_THEME = 'theme' | ||
neko259
|
r990 | FORM_USERNAME = 'username' | ||
neko259
|
r900 | |||
neko259
|
r733 | CONTEXT_HIDDEN_TAGS = 'hidden_tags' | ||
neko259
|
r995 | TEMPLATE = 'boards/settings.html' | ||
neko259
|
r561 | |||
neko259
|
r547 | class SettingsView(BaseBoardView): | ||
def get(self, request): | ||||
neko259
|
r995 | params = dict() | ||
neko259
|
r730 | settings_manager = get_settings_manager(request) | ||
neko259
|
r547 | |||
neko259
|
r728 | selected_theme = settings_manager.get_theme() | ||
neko259
|
r547 | |||
neko259
|
r990 | form = SettingsForm( | ||
initial={ | ||||
FORM_THEME: selected_theme, | ||||
FORM_USERNAME: settings_manager.get_setting(SETTING_USERNAME)}, | ||||
error_class=PlainErrorList) | ||||
neko259
|
r547 | |||
neko259
|
r919 | params[CONTEXT_FORM] = form | ||
params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags() | ||||
neko259
|
r547 | |||
neko259
|
r995 | return render(request, TEMPLATE, params) | ||
neko259
|
r547 | |||
def post(self, request): | ||||
neko259
|
r730 | settings_manager = get_settings_manager(request) | ||
neko259
|
r547 | |||
with transaction.atomic(): | ||||
neko259
|
r729 | form = SettingsForm(request.POST, error_class=PlainErrorList) | ||
neko259
|
r547 | |||
if form.is_valid(): | ||||
neko259
|
r900 | selected_theme = form.cleaned_data[FORM_THEME] | ||
neko259
|
r990 | username = form.cleaned_data[FORM_USERNAME] | ||
neko259
|
r547 | |||
neko259
|
r728 | settings_manager.set_theme(selected_theme) | ||
neko259
|
r547 | |||
neko259
|
r990 | settings_manager.set_setting(SETTING_USERNAME, username) | ||
settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID, None) | ||||
neko259
|
r995 | |||
return redirect('settings') | ||||
else: | ||||
params = dict() | ||||
neko259
|
r990 | |||
neko259
|
r995 | params[CONTEXT_FORM] = form | ||
params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags() | ||||
return render(request, TEMPLATE, params) | ||||