|
|
from django.db import transaction
|
|
|
from django.shortcuts import render, redirect
|
|
|
from django.utils import timezone
|
|
|
|
|
|
from boards.abstracts.settingsmanager import get_settings_manager, \
|
|
|
SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER
|
|
|
from boards.middlewares import SESSION_TIMEZONE
|
|
|
from boards.views.base import BaseBoardView, CONTEXT_FORM
|
|
|
from boards.forms import SettingsForm, PlainErrorList
|
|
|
from boards import settings
|
|
|
|
|
|
FORM_THEME = 'theme'
|
|
|
FORM_USERNAME = 'username'
|
|
|
FORM_TIMEZONE = 'timezone'
|
|
|
FORM_IMAGE_VIEWER = 'image_viewer'
|
|
|
|
|
|
CONTEXT_HIDDEN_TAGS = 'hidden_tags'
|
|
|
|
|
|
TEMPLATE = 'boards/settings.html'
|
|
|
|
|
|
|
|
|
class SettingsView(BaseBoardView):
|
|
|
|
|
|
def get(self, request):
|
|
|
params = dict()
|
|
|
settings_manager = get_settings_manager(request)
|
|
|
|
|
|
selected_theme = settings_manager.get_theme()
|
|
|
|
|
|
form = SettingsForm(
|
|
|
initial={
|
|
|
FORM_THEME: selected_theme,
|
|
|
FORM_IMAGE_VIEWER: settings_manager.get_setting(
|
|
|
SETTING_IMAGE_VIEWER,
|
|
|
default=settings.get('View', 'DefaultImageViewer')),
|
|
|
FORM_USERNAME: settings_manager.get_setting(SETTING_USERNAME),
|
|
|
FORM_TIMEZONE: request.session.get(
|
|
|
SESSION_TIMEZONE, timezone.get_current_timezone()),
|
|
|
},
|
|
|
error_class=PlainErrorList)
|
|
|
|
|
|
params[CONTEXT_FORM] = form
|
|
|
params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
|
|
|
|
|
|
return render(request, TEMPLATE, params)
|
|
|
|
|
|
def post(self, request):
|
|
|
settings_manager = get_settings_manager(request)
|
|
|
|
|
|
with transaction.atomic():
|
|
|
form = SettingsForm(request.POST, error_class=PlainErrorList)
|
|
|
|
|
|
if form.is_valid():
|
|
|
selected_theme = form.cleaned_data[FORM_THEME]
|
|
|
username = form.cleaned_data[FORM_USERNAME].lower()
|
|
|
|
|
|
settings_manager.set_theme(selected_theme)
|
|
|
settings_manager.set_setting(SETTING_IMAGE_VIEWER,
|
|
|
form.cleaned_data[FORM_IMAGE_VIEWER])
|
|
|
|
|
|
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)
|
|
|
|
|
|
request.session[SESSION_TIMEZONE] = form.cleaned_data[FORM_TIMEZONE]
|
|
|
|
|
|
return redirect('settings')
|
|
|
else:
|
|
|
params = dict()
|
|
|
|
|
|
params[CONTEXT_FORM] = form
|
|
|
params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
|
|
|
|
|
|
return render(request, TEMPLATE, params)
|
|
|
|
|
|
|