Added api retrieve method for config variables. Refactored api output.

This commit is contained in:
Norman Jäckel 2015-02-04 13:45:50 +01:00
parent c6705e687a
commit ecf7e07fe7
2 changed files with 24 additions and 6 deletions

View File

@ -39,13 +39,13 @@ class ConfigHandler(object):
config_variable.on_change() config_variable.on_change()
break break
def get_data_as_dict(self): def items(self):
""" """
Returns all config variables as dictionary retrieved from the config cache. Returns key-value pairs of all config variables.
""" """
if not hasattr(self, '_cache'): if not hasattr(self, '_cache'):
self.setup_cache() self.setup_cache()
return self._cache return self._cache.items()
def get_default(self, key): def get_default(self, key):
""" """

View File

@ -1,12 +1,14 @@
from django import forms from django import forms
from django.contrib import messages from django.contrib import messages
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import Http404
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from openslides.utils.rest_api import response, viewsets from openslides.utils.rest_api import response, viewsets
from openslides.utils.views import FormView from openslides.utils.views import FormView
from .api import config from .api import config
from .exceptions import ConfigNotFound
from .signals import config_signal from .signals import config_signal
@ -105,16 +107,32 @@ class ConfigView(FormView):
class ConfigViewSet(viewsets.ViewSet): class ConfigViewSet(viewsets.ViewSet):
""" """
API endpoint to list and update the config. API endpoint to list, retrieve and update the config.
""" """
def list(self, request): def list(self, request):
""" """
Lists als config variables. Everybody can see this. Lists all config variables. Everybody can see them.
""" """
# TODO: Check if we need permission check here. # TODO: Check if we need permission check here.
return response.Response(config.get_data_as_dict()) data = ({'key': key, 'value': value} for key, value in config.items())
return response.Response(data)
def retrieve(self, request, *args, **kwargs):
"""
Retrieves one config variable.
"""
# TODO: Check if we need permission check here.
key = kwargs['pk']
try:
data = {'key': key, 'value': config[key]}
except ConfigNotFound:
raise Http404
return response.Response(data)
def update(self, request, pk=None): def update(self, request, pk=None):
"""
TODO
"""
if not request.user.has_perm('config.can_manage'): if not request.user.has_perm('config.can_manage'):
self.permission_denied(request) self.permission_denied(request)
else: else: