OpenSlides/openslides/config/models.py

127 lines
3.4 KiB
Python
Raw Normal View History

2011-07-31 10:46:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.config.models
2011-07-31 10:46:29 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~
Models for the config app.
2011-07-31 10:46:29 +02:00
:copyright: 2011 by the OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
from pickle import dumps, loads
import base64
2011-07-31 10:46:29 +02:00
from django.db import models
from django.dispatch import receiver
2012-04-14 14:31:09 +02:00
from utils.translation_ext import ugettext as _
2011-07-31 10:46:29 +02:00
from openslides.config.signals import default_config_value
2011-07-31 10:46:29 +02:00
2012-02-15 12:04:11 +01:00
class ConfigStore(models.Model):
key = models.CharField(max_length=100, primary_key=True)
2011-07-31 10:46:29 +02:00
value = models.CharField(max_length=100)
def __unicode__(self):
2012-02-19 22:20:29 +01:00
return self.key
2011-07-31 10:46:29 +02:00
class Meta:
2012-02-15 12:04:11 +01:00
verbose_name = 'config'
2011-07-31 10:46:29 +02:00
permissions = (
('can_manage_config', _("Can manage config configuration", fixstr=True)),
2011-07-31 10:46:29 +02:00
)
2012-02-15 12:04:11 +01:00
# TODO:
# I used base64 to save pickled Data, there has to be another way see:
# http://stackoverflow.com/questions/2524970/djangounicodedecodeerror-while-storing-pickled-data
2012-02-15 12:04:11 +01:00
class Config(object):
2012-02-19 22:20:29 +01:00
def load_config(self):
self.config = {}
for key, value in ConfigStore.objects.all().values_list():
self.config[key] = loads(base64.decodestring(str(value)))
2012-02-19 22:20:29 +01:00
2012-02-15 12:04:11 +01:00
def __getitem__(self, key):
try:
2012-02-19 22:20:29 +01:00
self.config
except AttributeError:
self.load_config()
2012-02-19 22:20:29 +01:00
try:
return self.config[key]
except KeyError:
pass
for receiver, value in default_config_value.send(sender='config', key=key):
if value is not None:
return value
return None
2012-02-15 12:04:11 +01:00
def __setitem__(self, key, value):
try:
c = ConfigStore.objects.get(pk=key)
2012-02-15 12:04:11 +01:00
except ConfigStore.DoesNotExist:
c = ConfigStore(pk=key)
c.value = base64.encodestring(dumps(value))
2012-02-15 12:04:11 +01:00
c.save()
2012-04-14 09:47:34 +02:00
try:
self.config[key] = value
except AttributeError:
self.load_config()
self.config[key] = value
2012-02-15 12:04:11 +01:00
config = Config()
@receiver(default_config_value, dispatch_uid="config_default_config")
def default_config(sender, key, **kwargs):
return {
'event_name': 'OpenSlides',
'event_description': 'Presentation and voting system',
'system_url': 'http://127.0.0.1:8000',
'system_welcometext': 'Welcome to OpenSlides!',
}.get(key)
2012-03-18 17:11:58 +01:00
from django.dispatch import receiver
from django.core.urlresolvers import reverse
from django.utils.importlib import import_module
import settings
from openslides.utils.signals import template_manipulation
@receiver(template_manipulation, dispatch_uid="config_submenu")
2012-03-18 17:11:58 +01:00
def set_submenu(sender, request, context, **kwargs):
2012-04-12 14:20:05 +02:00
if not request.path.startswith('/config/'):
2012-04-11 10:58:59 +02:00
return None
2012-03-18 17:11:58 +01:00
selected = True if request.path == reverse('config_general') else False
menu_links = [
(reverse('config_general'), _('General'), selected),
]
for app in settings.INSTALLED_APPS:
try:
mod = import_module(app + '.views')
mod.Config
except (ImportError, AttributeError):
continue
appname = mod.__name__.split('.')[0]
selected = True if reverse('config_%s' % appname) == request.path else False
menu_links.append(
(reverse('config_%s' % appname), _(appname.title()), selected)
)
context.update({
'menu_links': menu_links,
})
2012-02-15 12:04:11 +01:00