2013-03-01 17:13:12 +01:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import messages
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
2015-06-16 14:03:42 +02:00
|
|
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
2015-02-04 13:45:50 +01:00
|
|
|
from django.http import Http404
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2012-02-15 12:04:11 +01:00
|
|
|
|
2015-06-16 14:03:42 +02:00
|
|
|
from openslides.utils.rest_api import Response, ValidationError, ViewSet
|
2013-09-25 10:01:01 +02:00
|
|
|
from openslides.utils.views import FormView
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
from .api import config
|
2015-02-04 13:45:50 +01:00
|
|
|
from .exceptions import ConfigNotFound
|
2013-03-01 17:13:12 +01:00
|
|
|
from .signals import config_signal
|
2012-04-15 12:39:28 +02:00
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
class ConfigView(FormView):
|
2012-07-07 14:48:21 +02:00
|
|
|
"""
|
2013-12-02 19:33:43 +01:00
|
|
|
The view for a config collection.
|
2012-07-07 14:48:21 +02:00
|
|
|
"""
|
2014-05-15 20:07:09 +02:00
|
|
|
required_permission = 'config.can_manage'
|
2013-03-01 17:13:12 +01:00
|
|
|
template_name = 'config/config_form.html'
|
2013-12-02 19:33:43 +01:00
|
|
|
config_collection = None
|
2013-03-01 17:13:12 +01:00
|
|
|
form_class = forms.Form
|
|
|
|
|
|
|
|
def get_form(self, *args):
|
|
|
|
"""
|
|
|
|
Gets the form for the view. Includes all form fields given by the
|
2013-12-02 19:33:43 +01:00
|
|
|
config collection.
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
|
|
|
form = super(ConfigView, self).get_form(*args)
|
2013-12-02 19:33:43 +01:00
|
|
|
for name, field in self.generate_form_fields_from_config_collection():
|
2013-03-01 17:13:12 +01:00
|
|
|
form.fields[name] = field
|
|
|
|
return form
|
|
|
|
|
2013-12-02 19:33:43 +01:00
|
|
|
def generate_form_fields_from_config_collection(self):
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
|
|
|
Generates the fields for the get_form function.
|
|
|
|
"""
|
2013-12-02 19:33:43 +01:00
|
|
|
for variable in self.config_collection.variables:
|
2013-03-01 17:13:12 +01:00
|
|
|
if variable.form_field is not None:
|
|
|
|
yield (variable.name, variable.form_field)
|
2012-03-16 14:31:59 +01:00
|
|
|
|
|
|
|
def get_initial(self):
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
|
|
|
Returns a dictonary with the actual values of the config variables
|
|
|
|
as intial value for the form.
|
|
|
|
"""
|
|
|
|
initial = super(ConfigView, self).get_initial()
|
2013-12-02 19:33:43 +01:00
|
|
|
for variable in self.config_collection.variables:
|
2013-03-01 17:13:12 +01:00
|
|
|
initial.update({variable.name: config[variable.name]})
|
|
|
|
return initial
|
2012-03-16 14:31:59 +01:00
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
"""
|
2013-12-02 19:33:43 +01:00
|
|
|
Adds to the context the active config view, a list of dictionaries
|
|
|
|
containing all config collections each with a flag which is true if its
|
|
|
|
view is the active one and adds a flag whether the config collection
|
|
|
|
has groups. Adds also extra_stylefiles and extra_javascript.
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
|
|
|
context = super(ConfigView, self).get_context_data(**kwargs)
|
|
|
|
|
2013-12-02 19:33:43 +01:00
|
|
|
context['active_config_collection_view'] = self.config_collection
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2013-12-02 19:33:43 +01:00
|
|
|
config_collection_list = []
|
|
|
|
for receiver, config_collection in config_signal.send(sender=self):
|
|
|
|
if config_collection.is_shown():
|
|
|
|
config_collection_list.append({
|
|
|
|
'config_collection': config_collection,
|
|
|
|
'active': self.request.path == reverse('config_%s' % config_collection.url)})
|
|
|
|
context['config_collection_list'] = sorted(
|
|
|
|
config_collection_list, key=lambda config_collection_dict: config_collection_dict['config_collection'].weight)
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2013-12-02 19:33:43 +01:00
|
|
|
if hasattr(self.config_collection, 'groups'):
|
|
|
|
context['groups'] = self.config_collection.groups
|
2011-11-14 23:40:19 +01:00
|
|
|
else:
|
2013-03-01 17:13:12 +01:00
|
|
|
context['groups'] = None
|
2012-04-15 13:26:01 +02:00
|
|
|
|
2013-12-02 19:33:43 +01:00
|
|
|
if 'extra_stylefiles' in self.config_collection.extra_context:
|
2013-03-01 17:13:12 +01:00
|
|
|
if 'extra_stylefiles' in context:
|
2013-12-02 19:33:43 +01:00
|
|
|
context['extra_stylefiles'].extend(self.config_collection.extra_context['extra_stylefiles'])
|
2013-03-01 17:13:12 +01:00
|
|
|
else:
|
2013-12-02 19:33:43 +01:00
|
|
|
context['extra_stylefiles'] = self.config_collection.extra_context['extra_stylefiles']
|
2012-04-15 12:39:28 +02:00
|
|
|
|
2013-12-02 19:33:43 +01:00
|
|
|
if 'extra_javascript' in self.config_collection.extra_context:
|
2013-03-01 17:13:12 +01:00
|
|
|
if 'extra_javascript' in context:
|
2013-12-02 19:33:43 +01:00
|
|
|
context['extra_javascript'].extend(self.config_collection.extra_context['extra_javascript'])
|
2013-03-01 17:13:12 +01:00
|
|
|
else:
|
2013-12-02 19:33:43 +01:00
|
|
|
context['extra_javascript'] = self.config_collection.extra_context['extra_javascript']
|
2012-04-15 12:39:28 +02:00
|
|
|
|
|
|
|
return context
|
2012-03-16 14:31:59 +01:00
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
def get_success_url(self):
|
|
|
|
"""
|
|
|
|
Returns the success url when changes are saved. Here it is the same
|
2013-12-02 19:33:43 +01:00
|
|
|
url as the main menu entry.
|
2013-03-01 17:13:12 +01:00
|
|
|
"""
|
2013-12-02 19:33:43 +01:00
|
|
|
return reverse('config_%s' % self.config_collection.url)
|
2013-03-01 17:13:12 +01:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
"""
|
|
|
|
Saves all data of a valid form.
|
|
|
|
"""
|
|
|
|
for key in form.cleaned_data:
|
|
|
|
config[key] = form.cleaned_data[key]
|
2013-12-02 19:33:43 +01:00
|
|
|
messages.success(self.request, _('%s settings successfully saved.') % _(self.config_collection.title))
|
2013-03-01 17:13:12 +01:00
|
|
|
return super(ConfigView, self).form_valid(form)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class ConfigViewSet(ViewSet):
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
2015-02-04 13:45:50 +01:00
|
|
|
API endpoint to list, retrieve and update the config.
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
def list(self, request):
|
|
|
|
"""
|
2015-02-04 13:45:50 +01:00
|
|
|
Lists all config variables. Everybody can see them.
|
2015-01-24 16:35:50 +01:00
|
|
|
"""
|
|
|
|
# TODO: Check if we need permission check here.
|
2015-02-04 13:45:50 +01:00
|
|
|
data = ({'key': key, 'value': value} for key, value in config.items())
|
2015-02-12 18:48:14 +01:00
|
|
|
return Response(data)
|
2015-02-04 13:45:50 +01:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
"""
|
2015-06-11 14:21:54 +02:00
|
|
|
Retrieves one config variable. Everybody can see it.
|
2015-02-04 13:45:50 +01:00
|
|
|
"""
|
|
|
|
# TODO: Check if we need permission check here.
|
|
|
|
key = kwargs['pk']
|
|
|
|
try:
|
|
|
|
data = {'key': key, 'value': config[key]}
|
|
|
|
except ConfigNotFound:
|
|
|
|
raise Http404
|
2015-02-12 18:48:14 +01:00
|
|
|
return Response(data)
|
2015-01-24 16:35:50 +01:00
|
|
|
|
2015-06-11 14:21:54 +02:00
|
|
|
def update(self, request, *args, **kwargs):
|
2015-02-04 13:45:50 +01:00
|
|
|
"""
|
2015-06-11 14:21:54 +02:00
|
|
|
Updates one config variable. Only managers can do this.
|
2015-06-16 14:03:42 +02:00
|
|
|
|
|
|
|
Example: {"value": 42}
|
2015-02-04 13:45:50 +01:00
|
|
|
"""
|
2015-06-16 14:03:42 +02:00
|
|
|
# Check permission.
|
2015-01-24 16:35:50 +01:00
|
|
|
if not request.user.has_perm('config.can_manage'):
|
|
|
|
self.permission_denied(request)
|
2015-06-16 14:03:42 +02:00
|
|
|
|
2015-06-11 14:21:54 +02:00
|
|
|
# Check if pk is a valid config variable key.
|
2015-06-16 14:03:42 +02:00
|
|
|
key = kwargs['pk']
|
2015-06-11 14:21:54 +02:00
|
|
|
if key not in config:
|
|
|
|
raise Http404
|
2015-06-16 14:03:42 +02:00
|
|
|
|
|
|
|
# Validate value.
|
|
|
|
form_field = config.get_config_variables()[key].form_field
|
|
|
|
value = request.data['value']
|
|
|
|
if form_field:
|
|
|
|
try:
|
|
|
|
form_field.clean(value)
|
|
|
|
except DjangoValidationError as e:
|
|
|
|
raise ValidationError({'detail': e.messages[0]})
|
|
|
|
|
2015-06-11 14:21:54 +02:00
|
|
|
# Change value.
|
2015-06-16 14:03:42 +02:00
|
|
|
config[key] = value
|
|
|
|
|
2015-06-11 14:21:54 +02:00
|
|
|
# Return response.
|
2015-06-16 14:03:42 +02:00
|
|
|
return Response({'key': key, 'value': value})
|