|
|
from django.db import transaction
|
|
|
from django.shortcuts import render, redirect
|
|
|
from django.utils import timezone
|
|
|
from django.utils.decorators import method_decorator
|
|
|
from django.views.decorators.csrf import csrf_protect
|
|
|
|
|
|
from boards.abstracts.settingsmanager import get_settings_manager, \
|
|
|
SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER, \
|
|
|
SETTING_SUBSCRIBE_BY_DEFAULT
|
|
|
from boards.middlewares import SESSION_TIMEZONE
|
|
|
from boards.views.base import BaseBoardView, CONTEXT_FORM
|
|
|
from boards.forms import SettingsForm, PlainErrorList
|
|
|
from boards import settings
|
|
|
from boards.models import Attachment
|
|
|
|
|
|
FORM_THEME = 'theme'
|
|
|
FORM_USERNAME = 'username'
|
|
|
FORM_TIMEZONE = 'timezone'
|
|
|
FORM_IMAGE_VIEWER = 'image_viewer'
|
|
|
FORM_SUBSCRIBE = 'subscribe_by_default'
|
|
|
|
|
|
CONTEXT_HIDDEN_TAGS = 'hidden_tags'
|
|
|
|
|
|
TEMPLATE = 'boards/settings.html'
|
|
|
|
|
|
|
|
|
class SettingsView(BaseBoardView):
|
|
|
@method_decorator(csrf_protect)
|
|
|
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()),
|
|
|
FORM_SUBSCRIBE: settings_manager.get_setting(SETTING_SUBSCRIBE_BY_DEFAULT, False)
|
|
|
},
|
|
|
error_class=PlainErrorList)
|
|
|
|
|
|
params[CONTEXT_FORM] = form
|
|
|
params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
|
|
|
|
|
|
return render(request, TEMPLATE, params)
|
|
|
|
|
|
@method_decorator(csrf_protect)
|
|
|
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])
|
|
|
settings_manager.set_setting(SETTING_SUBSCRIBE_BY_DEFAULT, form.cleaned_data[SETTING_SUBSCRIBE_BY_DEFAULT])
|
|
|
|
|
|
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)
|
|
|
|
|
|
|