2015-06-17 18:32:05 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
from operator import attrgetter
|
|
|
|
|
2015-02-04 13:45:50 +01:00
|
|
|
from django.http import Http404
|
2012-02-15 12:04:11 +01:00
|
|
|
|
2015-06-17 18:32:05 +02:00
|
|
|
from openslides.utils.rest_api import (
|
|
|
|
Response,
|
|
|
|
SimpleMetadata,
|
|
|
|
ValidationError,
|
|
|
|
ViewSet,
|
|
|
|
)
|
2013-09-25 10:01:01 +02:00
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
from .api import config
|
2015-06-17 18:32:05 +02:00
|
|
|
from .exceptions import ConfigError, ConfigNotFound
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigMetadata(SimpleMetadata):
|
|
|
|
"""
|
|
|
|
Custom metadata class to add config info to responses on OPTIONS requests.
|
|
|
|
"""
|
|
|
|
def determine_metadata(self, request, view):
|
|
|
|
# Sort config variables by weight.
|
|
|
|
config_variables = sorted(config.get_config_variables().values(), key=attrgetter('weight'))
|
|
|
|
|
|
|
|
# Build tree.
|
|
|
|
config_groups = []
|
|
|
|
for config_variable in config_variables:
|
|
|
|
if not config_groups or config_groups[-1]['name'] != config_variable.group:
|
|
|
|
config_groups.append(OrderedDict(
|
|
|
|
name=config_variable.group,
|
|
|
|
subgroups=[]))
|
|
|
|
if not config_groups[-1]['subgroups'] or config_groups[-1]['subgroups'][-1]['name'] != config_variable.subgroup:
|
|
|
|
config_groups[-1]['subgroups'].append(OrderedDict(
|
|
|
|
name=config_variable.subgroup,
|
|
|
|
items=[]))
|
|
|
|
config_groups[-1]['subgroups'][-1]['items'].append(config_variable.data)
|
|
|
|
|
|
|
|
# Add tree to metadata.
|
|
|
|
metadata = super().determine_metadata(request, view)
|
|
|
|
metadata['config_groups'] = config_groups
|
|
|
|
return metadata
|
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
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
metadata_class = ConfigMetadata
|
|
|
|
|
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
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
return Response([{'key': key, 'value': value} for key, value in config.items()])
|
2015-02-04 13:45:50 +01:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
"""
|
2015-06-17 18:32:05 +02:00
|
|
|
Retrieves a config variable. Everybody can see it.
|
2015-02-04 13:45:50 +01:00
|
|
|
"""
|
|
|
|
key = kwargs['pk']
|
|
|
|
try:
|
2015-06-17 18:32:05 +02:00
|
|
|
value = config[key]
|
2015-02-04 13:45:50 +01:00
|
|
|
except ConfigNotFound:
|
|
|
|
raise Http404
|
2015-06-17 18:32:05 +02:00
|
|
|
return Response({'key': key, 'value': value})
|
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-17 18:32:05 +02:00
|
|
|
Updates a 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
|
|
|
|
|
|
|
key = kwargs['pk']
|
|
|
|
value = request.data['value']
|
2015-06-17 18:32:05 +02:00
|
|
|
|
|
|
|
# Validate and change value.
|
|
|
|
try:
|
|
|
|
config[key] = value
|
|
|
|
except ConfigNotFound:
|
|
|
|
raise Http404
|
|
|
|
except ConfigError as e:
|
|
|
|
raise ValidationError({'detail': e})
|
2015-06-16 14:03:42 +02:00
|
|
|
|
2015-06-11 14:21:54 +02:00
|
|
|
# Return response.
|
2015-06-16 14:03:42 +02:00
|
|
|
return Response({'key': key, 'value': value})
|