2018-01-20 15:58:36 +01:00
|
|
|
from collections import OrderedDict
|
|
|
|
from operator import attrgetter
|
2018-08-29 15:49:44 +02:00
|
|
|
from typing import Any, Dict, List
|
2018-01-20 15:58:36 +01:00
|
|
|
|
2014-11-13 22:23:16 +01:00
|
|
|
from django.apps import AppConfig
|
2017-01-20 10:23:14 +01:00
|
|
|
from django.conf import settings
|
2018-08-30 09:07:06 +02:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2017-08-22 14:17:20 +02:00
|
|
|
from django.db.models.signals import post_migrate
|
2018-09-07 10:39:16 +02:00
|
|
|
from django.db.utils import OperationalError
|
2014-11-13 22:23:16 +01:00
|
|
|
|
2017-08-30 00:07:54 +02:00
|
|
|
from ..utils.projector import register_projector_elements
|
2017-03-06 16:34:20 +01:00
|
|
|
|
2014-11-13 22:23:16 +01:00
|
|
|
|
|
|
|
class CoreAppConfig(AppConfig):
|
|
|
|
name = 'openslides.core'
|
|
|
|
verbose_name = 'OpenSlides Core'
|
2015-07-01 17:48:41 +02:00
|
|
|
angular_site_module = True
|
|
|
|
angular_projector_module = True
|
2014-11-13 22:23:16 +01:00
|
|
|
|
|
|
|
def ready(self):
|
|
|
|
# Import all required stuff.
|
2017-01-14 12:29:42 +01:00
|
|
|
from .config import config
|
|
|
|
from ..utils.rest_api import router
|
2018-09-01 08:00:00 +02:00
|
|
|
from ..utils.cache import element_cache
|
2017-08-30 00:07:54 +02:00
|
|
|
from .projector import get_projector_elements
|
2017-02-21 09:34:24 +01:00
|
|
|
from .signals import (
|
|
|
|
delete_django_app_permissions,
|
|
|
|
get_permission_change_data,
|
2017-04-10 16:28:38 +02:00
|
|
|
permission_change,
|
2018-02-02 13:51:19 +01:00
|
|
|
post_permission_creation,
|
2017-05-22 16:08:52 +02:00
|
|
|
required_users,
|
2018-02-02 13:51:19 +01:00
|
|
|
user_data_required,
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
from .views import (
|
2015-09-07 16:46:04 +02:00
|
|
|
ChatMessageViewSet,
|
2015-06-29 12:08:15 +02:00
|
|
|
ConfigViewSet,
|
2016-10-21 11:05:24 +02:00
|
|
|
CountdownViewSet,
|
|
|
|
ProjectorMessageViewSet,
|
2015-06-29 12:08:15 +02:00
|
|
|
ProjectorViewSet,
|
|
|
|
TagViewSet,
|
|
|
|
)
|
2018-08-30 09:07:06 +02:00
|
|
|
from ..utils.constants import set_constants, get_constants_from_apps
|
|
|
|
|
2018-09-10 08:15:31 +02:00
|
|
|
# Collect all config variables before getting the constants.
|
|
|
|
config.collect_config_variables_from_apps()
|
|
|
|
|
2018-08-30 09:07:06 +02:00
|
|
|
# Set constants
|
|
|
|
try:
|
|
|
|
set_constants(get_constants_from_apps())
|
2018-09-07 10:39:16 +02:00
|
|
|
except (ImproperlyConfigured, OperationalError):
|
|
|
|
# Database is not loaded. This happens in tests and migrations.
|
2018-08-30 09:07:06 +02:00
|
|
|
pass
|
2014-11-13 22:23:16 +01:00
|
|
|
|
2018-09-10 08:15:31 +02:00
|
|
|
# Define projector elements.
|
2017-08-30 00:07:54 +02:00
|
|
|
register_projector_elements(get_projector_elements())
|
2016-06-02 12:47:01 +02:00
|
|
|
|
2014-11-13 22:23:16 +01:00
|
|
|
# Connect signals.
|
2016-01-25 14:48:00 +01:00
|
|
|
post_permission_creation.connect(
|
|
|
|
delete_django_app_permissions,
|
|
|
|
dispatch_uid='delete_django_app_permissions')
|
2017-02-21 09:34:24 +01:00
|
|
|
permission_change.connect(
|
|
|
|
get_permission_change_data,
|
|
|
|
dispatch_uid='core_get_permission_change_data')
|
2017-04-10 16:28:38 +02:00
|
|
|
user_data_required.connect(
|
2017-05-22 16:08:52 +02:00
|
|
|
required_users,
|
|
|
|
dispatch_uid='core_required_users')
|
2014-11-13 22:23:16 +01:00
|
|
|
|
2017-08-22 14:17:20 +02:00
|
|
|
post_migrate.connect(call_save_default_values, sender=self, dispatch_uid='core_save_config_default_values')
|
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
# Register viewsets.
|
2016-02-11 22:58:32 +01:00
|
|
|
router.register(self.get_model('Projector').get_collection_string(), ProjectorViewSet)
|
|
|
|
router.register(self.get_model('ChatMessage').get_collection_string(), ChatMessageViewSet)
|
|
|
|
router.register(self.get_model('Tag').get_collection_string(), TagViewSet)
|
|
|
|
router.register(self.get_model('ConfigStore').get_collection_string(), ConfigViewSet, 'config')
|
2016-10-21 11:05:24 +02:00
|
|
|
router.register(self.get_model('ProjectorMessage').get_collection_string(), ProjectorMessageViewSet)
|
|
|
|
router.register(self.get_model('Countdown').get_collection_string(), CountdownViewSet)
|
2015-01-17 14:01:44 +01:00
|
|
|
|
2018-09-01 08:00:00 +02:00
|
|
|
# Sets the cache
|
|
|
|
try:
|
|
|
|
element_cache.ensure_cache()
|
|
|
|
except (ImproperlyConfigured, OperationalError):
|
|
|
|
# This happens in the tests or in migrations. Do nothing
|
|
|
|
pass
|
|
|
|
|
2018-09-10 08:15:31 +02:00
|
|
|
def get_config_variables(self):
|
|
|
|
from .config_variables import get_config_variables
|
|
|
|
return get_config_variables()
|
|
|
|
|
2017-01-14 12:29:42 +01:00
|
|
|
def get_startup_elements(self):
|
2017-03-06 16:34:20 +01:00
|
|
|
"""
|
2018-07-09 23:22:26 +02:00
|
|
|
Yields all Cachables required on startup i. e. opening the websocket
|
2017-03-06 16:34:20 +01:00
|
|
|
connection.
|
|
|
|
"""
|
2018-07-09 23:22:26 +02:00
|
|
|
for model_name in ('Projector', 'ChatMessage', 'Tag', 'ProjectorMessage', 'Countdown', 'ConfigStore'):
|
|
|
|
yield self.get_model(model_name)
|
2017-01-20 10:23:14 +01:00
|
|
|
|
|
|
|
def get_angular_constants(self):
|
2018-01-20 15:58:36 +01:00
|
|
|
from .config import config
|
|
|
|
|
2018-08-29 15:49:44 +02:00
|
|
|
constants: Dict[str, Any] = {}
|
|
|
|
|
2017-01-20 10:23:14 +01:00
|
|
|
# Client settings
|
|
|
|
client_settings_keys = [
|
2018-01-26 14:56:42 +01:00
|
|
|
'MOTION_IDENTIFIER_MIN_DIGITS',
|
|
|
|
'MOTION_IDENTIFIER_WITHOUT_BLANKS',
|
2017-01-20 10:23:14 +01:00
|
|
|
'MOTIONS_ALLOW_AMENDMENTS_OF_AMENDMENTS'
|
|
|
|
]
|
|
|
|
client_settings_dict = {}
|
|
|
|
for key in client_settings_keys:
|
|
|
|
try:
|
|
|
|
client_settings_dict[key] = getattr(settings, key)
|
|
|
|
except AttributeError:
|
|
|
|
# Settings key does not exist. Do nothing. The client will
|
|
|
|
# treat this as undefined.
|
|
|
|
pass
|
2018-08-29 15:49:44 +02:00
|
|
|
constants['OpenSlidesSettings'] = client_settings_dict
|
2018-01-20 15:58:36 +01:00
|
|
|
|
|
|
|
# Config variables
|
2018-08-22 22:00:08 +02:00
|
|
|
config_groups: List[Any] = []
|
2018-01-20 15:58:36 +01:00
|
|
|
for config_variable in sorted(config.config_variables.values(), key=attrgetter('weight')):
|
|
|
|
if config_variable.is_hidden():
|
|
|
|
# Skip hidden config variables. Do not even check groups and subgroups.
|
|
|
|
continue
|
|
|
|
if not config_groups or config_groups[-1]['name'] != config_variable.group:
|
|
|
|
# Add new group.
|
|
|
|
config_groups.append(OrderedDict(
|
|
|
|
name=config_variable.group,
|
|
|
|
subgroups=[]))
|
|
|
|
if not config_groups[-1]['subgroups'] or config_groups[-1]['subgroups'][-1]['name'] != config_variable.subgroup:
|
|
|
|
# Add new subgroup.
|
|
|
|
config_groups[-1]['subgroups'].append(OrderedDict(
|
|
|
|
name=config_variable.subgroup,
|
|
|
|
items=[]))
|
|
|
|
# Add the config variable to the current group and subgroup.
|
|
|
|
config_groups[-1]['subgroups'][-1]['items'].append(config_variable.data)
|
2018-08-29 15:49:44 +02:00
|
|
|
constants['OpenSlidesConfigVariables'] = config_groups
|
2018-01-20 15:58:36 +01:00
|
|
|
|
2018-08-29 15:49:44 +02:00
|
|
|
return constants
|
2017-08-22 14:17:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
def call_save_default_values(**kwargs):
|
|
|
|
from .config import config
|
|
|
|
config.save_default_values()
|