load config at once

This commit is contained in:
Oskar Hahn 2012-02-19 22:20:29 +01:00
parent 70f1cb8b3e
commit a404c3fe89
3 changed files with 19 additions and 24 deletions

View File

@ -1,16 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.system.admin
~~~~~~~~~~~~~~~~~~~~~~~
Register app for admin site.
:copyright: 2011 by the OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
from django.contrib import admin
from system.models import ConfigStore
admin.site.register(ConfigStore)

View File

@ -33,7 +33,7 @@ class ConfigStore(models.Model):
value = models.CharField(max_length=100)
def __unicode__(self):
return self.id
return self.key
class Meta:
verbose_name = 'config'
@ -43,22 +43,33 @@ class ConfigStore(models.Model):
class Config(object):
def load_config(self):
self.config = {}
for key, value in ConfigStore.objects.all().values_list():
self.config[key] = loads(str(value))
def __getitem__(self, key):
try:
return loads(str(ConfigStore.objects.get(pk=key).value))
except ConfigStore.DoesNotExist:
try:
return DEFAULT_DATA[key]
except KeyError:
return None
self.config
except AttributeError:
self.load_config()
try:
return self.config[key]
except KeyError:
pass
try:
return DEFAULT_DATA[key]
except KeyError:
return None
def __setitem__(self, key, value):
try:
c = ConfigStore.objects.get(pk=key)
c = self.config.get(pk=key)
except ConfigStore.DoesNotExist:
c = ConfigStore(pk=key)
c.value = dumps(value)
c.save()
self.config[key] = value