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