settings.py
76 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
|
r547 | from django.db import transaction | ||
from django.shortcuts import render, redirect | ||||
|
r1065 | from django.utils import timezone | ||
|
r547 | |||
|
r990 | from boards.abstracts.settingsmanager import get_settings_manager, \ | ||
|
r1122 | SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER | ||
|
r1090 | from boards.middlewares import SESSION_TIMEZONE | ||
|
r722 | from boards.views.base import BaseBoardView, CONTEXT_FORM | ||
|
r729 | from boards.forms import SettingsForm, PlainErrorList | ||
|
r1122 | from boards import settings | ||
|
r547 | |||
|
r1090 | |||
|
r900 | FORM_THEME = 'theme' | ||
|
r990 | FORM_USERNAME = 'username' | ||
|
r1065 | FORM_TIMEZONE = 'timezone' | ||
|
r1122 | FORM_IMAGE_VIEWER = 'image_viewer' | ||
|
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, | ||||
|
r1122 | FORM_IMAGE_VIEWER: settings_manager.get_setting( | ||
SETTING_IMAGE_VIEWER, default=settings.DEFAULT_IMAGE_VIEWER), | ||||
|
r1065 | FORM_USERNAME: settings_manager.get_setting(SETTING_USERNAME), | ||
|
r1090 | FORM_TIMEZONE: request.session.get( | ||
SESSION_TIMEZONE, timezone.get_current_timezone()), | ||||
|
r1065 | }, | ||
|
r990 | 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) | ||
|
r1122 | settings_manager.set_setting(SETTING_IMAGE_VIEWER, | ||
form.cleaned_data[FORM_IMAGE_VIEWER]) | ||||
|
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) | ||||
|
r1090 | request.session[SESSION_TIMEZONE] = form.cleaned_data[FORM_TIMEZONE] | ||
|
r1065 | |||
|
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) | ||||