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
|
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
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
from .api import config
|
2015-02-04 13:45:50 +01:00
|
|
|
from .exceptions import ConfigNotFound
|
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})
|