OpenSlides/openslides/core/apps.py

166 lines
5.4 KiB
Python
Raw Normal View History

import sys
from typing import Any, Dict
from django.apps import AppConfig
from django.conf import settings
from django.db.models.signals import post_migrate, pre_delete
from openslides.utils import logging
from openslides.utils.schema_version import schema_version_handler
class CoreAppConfig(AppConfig):
2019-01-06 16:22:33 +01:00
name = "openslides.core"
verbose_name = "OpenSlides Core"
def ready(self):
# Import all required stuff.
# Let all client websocket message register
from ..utils import websocket_client_messages # noqa
from ..utils.rest_api import router
from . import serializers # noqa
2017-01-14 12:29:42 +01:00
from .config import config
from .projector import register_projector_slides
2017-02-21 09:34:24 +01:00
from .signals import (
autoupdate_for_many_to_many_relations,
2019-07-23 12:52:28 +02:00
cleanup_unused_permissions,
2017-02-21 09:34:24 +01:00
delete_django_app_permissions,
get_permission_change_data,
permission_change,
post_permission_creation,
)
2015-06-29 12:08:15 +02:00
from .views import (
ConfigViewSet,
CountdownViewSet,
ProjectionDefaultViewSet,
ProjectorMessageViewSet,
2015-06-29 12:08:15 +02:00
ProjectorViewSet,
TagViewSet,
)
2018-08-30 09:07:06 +02:00
# Collect all config variables before getting the constants.
config.collect_config_variables_from_apps()
# Define projector elements.
register_projector_slides()
# Connect signals.
post_permission_creation.connect(
2019-01-06 16:22:33 +01:00
delete_django_app_permissions, dispatch_uid="delete_django_app_permissions"
)
2019-07-23 12:52:28 +02:00
post_permission_creation.connect(
cleanup_unused_permissions, dispatch_uid="cleanup_unused_permissions"
)
2017-02-21 09:34:24 +01:00
permission_change.connect(
2019-01-06 16:22:33 +01:00
get_permission_change_data, dispatch_uid="core_get_permission_change_data"
)
2019-01-06 16:22:33 +01:00
post_migrate.connect(
manage_config, sender=self, dispatch_uid="core_manage_config"
2019-01-06 16:22:33 +01:00
)
pre_delete.connect(
autoupdate_for_many_to_many_relations,
dispatch_uid="core_autoupdate_for_many_to_many_relations",
)
2017-08-22 14:17:20 +02:00
# Register viewsets.
2019-01-06 16:22:33 +01:00
router.register(
self.get_model("Projector").get_collection_string(), ProjectorViewSet
)
router.register(
self.get_model("Projectiondefault").get_collection_string(),
ProjectionDefaultViewSet,
)
2019-01-06 16:22:33 +01:00
router.register(self.get_model("Tag").get_collection_string(), TagViewSet)
router.register(
self.get_model("ConfigStore").get_collection_string(),
ConfigViewSet,
"config",
)
router.register(
self.get_model("ProjectorMessage").get_collection_string(),
ProjectorMessageViewSet,
)
router.register(
self.get_model("Countdown").get_collection_string(), CountdownViewSet
)
2019-08-20 12:00:54 +02:00
if "runserver" in sys.argv or "changeconfig" in sys.argv:
from openslides.utils.startup import run_startup_hooks
run_startup_hooks()
def get_startup_hooks(self):
from openslides.core.models import History
from openslides.utils.cache import element_cache
from openslides.utils.constants import set_constants_from_apps
2019-08-20 12:00:54 +02:00
return {
10: element_cache.ensure_schema_version,
40: set_constants_from_apps,
90: History.objects.build_history,
}
def get_config_variables(self):
from .config_variables import get_config_variables
2019-01-06 16:22:33 +01:00
return get_config_variables()
2017-01-14 12:29:42 +01:00
def get_startup_elements(self):
"""
Yields all Cachables required on startup i. e. opening the websocket
connection.
"""
2019-01-06 16:22:33 +01:00
for model_name in (
"Projector",
"ProjectionDefault",
2019-01-06 16:22:33 +01:00
"Tag",
"ProjectorMessage",
"Countdown",
"ConfigStore",
):
yield self.get_model(model_name)
def get_angular_constants(self):
constants: Dict[str, Any] = {}
# Client settings
client_settings_keys = [
"PRIORITIZED_GROUP_IDS",
"PING_INTERVAL",
"PING_TIMEOUT",
"ENABLE_ELECTRONIC_VOTING",
"JITSI_DOMAIN",
"JITSI_ROOM_NAME",
"JITSI_ROOM_PASSWORD",
]
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
constants["Settings"] = client_settings_dict
constants["SchemaVersion"] = schema_version_handler.get()
return constants
2017-08-22 14:17:20 +02:00
def manage_config(**kwargs):
"""
Should be run after every migration. Saves default values
of all non db-existing config objects into the db. Deletes all
unnecessary old config values, e.g. all db entries, that does
not have a config_variable anymore. Increments the config version,
if at least one of the operations altered some data.
"""
2017-08-22 14:17:20 +02:00
from .config import config
2019-01-06 16:22:33 +01:00
altered = config.save_default_values()
altered = config.cleanup_old_config_values() or altered
if altered:
config.increment_version()
logging.getLogger(__name__).info("Updated config variables")