2011-07-31 10:46:29 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2012-04-14 12:52:56 +02:00
|
|
|
openslides.config.models
|
2011-07-31 10:46:29 +02:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-04-14 12:52:56 +02:00
|
|
|
Models for the config app.
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-04-25 22:29:19 +02:00
|
|
|
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
2012-07-07 14:48:21 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.urlresolvers import reverse
|
2011-07-31 10:46:29 +02:00
|
|
|
from django.db import models
|
2012-04-14 12:52:56 +02:00
|
|
|
from django.dispatch import receiver
|
2012-07-07 14:48:21 +02:00
|
|
|
from django.utils.importlib import import_module
|
2012-07-10 01:33:03 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _, ugettext_noop
|
2012-04-14 12:52:56 +02:00
|
|
|
|
2012-07-07 14:48:21 +02:00
|
|
|
from openslides.utils.jsonfield import JSONField
|
|
|
|
from openslides.utils.signals import template_manipulation
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-04-14 12:52:56 +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):
|
2012-07-07 14:48:21 +02:00
|
|
|
"""
|
|
|
|
Stores the config values.
|
|
|
|
"""
|
2012-02-15 12:04:11 +01:00
|
|
|
key = models.CharField(max_length=100, primary_key=True)
|
2012-07-07 14:48:21 +02:00
|
|
|
value = JSONField()
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
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 = (
|
2012-07-07 14:48:21 +02:00
|
|
|
('can_manage_config', ugettext_noop("Can manage configuration")),
|
2011-07-31 10:46:29 +02:00
|
|
|
)
|
2012-02-15 12:04:11 +01:00
|
|
|
|
2012-04-14 12:52:56 +02:00
|
|
|
|
2012-02-15 12:04:11 +01:00
|
|
|
class Config(object):
|
2012-07-07 14:48:21 +02:00
|
|
|
"""
|
|
|
|
Access the config values via config[...]
|
|
|
|
"""
|
2012-02-15 12:04:11 +01:00
|
|
|
def __getitem__(self, key):
|
2012-02-19 22:20:29 +01:00
|
|
|
try:
|
2012-07-07 14:48:21 +02:00
|
|
|
return ConfigStore.objects.get(key=key).value
|
2012-06-09 19:39:52 +02:00
|
|
|
except ConfigStore.DoesNotExist:
|
2012-02-19 22:20:29 +01:00
|
|
|
pass
|
2012-04-14 12:52:56 +02:00
|
|
|
|
2012-07-07 14:48:21 +02:00
|
|
|
for receiver, value in default_config_value.send(sender='config',
|
|
|
|
key=key):
|
2012-04-14 12:52:56 +02:00
|
|
|
if value is not None:
|
|
|
|
return value
|
2012-04-18 18:35:02 +02:00
|
|
|
if settings.DEBUG:
|
|
|
|
print "No default value for: %s" % key
|
2012-04-14 12:52:56 +02:00
|
|
|
return None
|
2012-02-15 12:04:11 +01:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
try:
|
2012-02-20 14:51:05 +01:00
|
|
|
c = ConfigStore.objects.get(pk=key)
|
2012-02-15 12:04:11 +01:00
|
|
|
except ConfigStore.DoesNotExist:
|
|
|
|
c = ConfigStore(pk=key)
|
2012-07-07 14:48:21 +02:00
|
|
|
c.value = value
|
2012-02-15 12:04:11 +01:00
|
|
|
c.save()
|
2012-07-07 14:48:21 +02:00
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return ConfigStore.objects.filter(key=item).exists()
|
|
|
|
|
2012-02-15 12:04:11 +01:00
|
|
|
|
2012-04-14 12:52:56 +02:00
|
|
|
config = Config()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(default_config_value, dispatch_uid="config_default_config")
|
|
|
|
def default_config(sender, key, **kwargs):
|
2012-07-07 14:48:21 +02:00
|
|
|
"""
|
|
|
|
Global default values.
|
|
|
|
"""
|
2012-04-14 12:52:56 +02:00
|
|
|
return {
|
2012-04-20 11:15:15 +02:00
|
|
|
'event_name': 'OpenSlides',
|
2012-07-07 14:48:21 +02:00
|
|
|
'event_description':
|
|
|
|
_('Presentation system for agenda, applications and elections'),
|
2012-04-18 19:30:32 +02:00
|
|
|
'event_date': '',
|
|
|
|
'event_location': '',
|
|
|
|
'event_organizer': '',
|
2012-04-18 18:35:02 +02:00
|
|
|
'presentation': '',
|
2012-04-18 15:04:16 +02:00
|
|
|
'frontpage_title': _('Welcome'),
|
|
|
|
'frontpage_welcometext': _('Welcome to OpenSlides!'),
|
|
|
|
'show_help_text': True,
|
2012-07-07 14:48:21 +02:00
|
|
|
'help_text': _("Get professional support for OpenSlides on %s.") %
|
|
|
|
"<a href='http://openslides.org/' target='_blank'> \
|
|
|
|
www.openslides.org</a>",
|
2012-04-18 18:35:02 +02:00
|
|
|
'system_enable_anonymous': False,
|
2012-04-14 12:52:56 +02:00
|
|
|
}.get(key)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(template_manipulation, dispatch_uid="config_submenu")
|
2012-03-18 17:11:58 +01:00
|
|
|
def set_submenu(sender, request, context, **kwargs):
|
2012-07-07 14:48:21 +02:00
|
|
|
"""
|
|
|
|
Submenu for the config tab.
|
|
|
|
"""
|
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
|
|
|
menu_links = [
|
2012-07-07 14:48:21 +02:00
|
|
|
(reverse('config_general'), _('General'),
|
|
|
|
request.path == reverse('config_general')),
|
2012-03-18 17:11:58 +01:00
|
|
|
]
|
2012-04-15 10:36:37 +02:00
|
|
|
|
2012-03-18 17:11:58 +01:00
|
|
|
for app in settings.INSTALLED_APPS:
|
|
|
|
try:
|
2012-04-16 11:36:50 +02:00
|
|
|
mod = import_module(app)
|
|
|
|
views = mod.views
|
|
|
|
views.Config
|
2012-03-18 17:11:58 +01:00
|
|
|
except (ImportError, AttributeError):
|
|
|
|
continue
|
|
|
|
|
|
|
|
appname = mod.__name__.split('.')[0]
|
2012-04-16 11:36:50 +02:00
|
|
|
selected = reverse('config_%s' % appname) == request.path
|
|
|
|
try:
|
|
|
|
title = mod.NAME
|
|
|
|
except AttributeError:
|
|
|
|
title = appname.title()
|
2012-03-18 17:11:58 +01:00
|
|
|
menu_links.append(
|
2012-04-16 11:36:50 +02:00
|
|
|
(reverse('config_%s' % appname), _(title), selected)
|
2012-03-18 17:11:58 +01:00
|
|
|
)
|
|
|
|
|
2012-04-15 10:36:37 +02:00
|
|
|
menu_links.append (
|
2012-07-07 14:48:21 +02:00
|
|
|
(reverse('config_version'), _('Version'),
|
|
|
|
request.path == reverse('config_version'))
|
2012-04-15 10:36:37 +02:00
|
|
|
)
|
|
|
|
|
2012-03-18 17:11:58 +01:00
|
|
|
context.update({
|
|
|
|
'menu_links': menu_links,
|
|
|
|
})
|