OpenSlides/openslides/core/apps.py

207 lines
6.6 KiB
Python
Raw Normal View History

2019-01-20 10:05:50 +01:00
import os
import sys
from collections import OrderedDict
from operator import attrgetter
from typing import Any, Dict, List, Set
from django.apps import AppConfig
from django.conf import settings
2017-08-22 14:17:20 +02:00
from django.db.models.signals import post_migrate
class CoreAppConfig(AppConfig):
2019-01-06 16:22:33 +01:00
name = "openslides.core"
verbose_name = "OpenSlides Core"
angular_site_module = True
def ready(self):
# Import all required stuff.
2017-01-14 12:29:42 +01:00
from .config import config
2018-12-23 11:05:38 +01:00
from .projector import register_projector_elements
from . import serializers # noqa
2017-02-21 09:34:24 +01:00
from .signals import (
delete_django_app_permissions,
get_permission_change_data,
permission_change,
post_permission_creation,
)
2015-06-29 12:08:15 +02:00
from .views import (
ChatMessageViewSet,
2015-06-29 12:08:15 +02:00
ConfigViewSet,
CountdownViewSet,
HistoryViewSet,
ProjectorMessageViewSet,
2015-06-29 12:08:15 +02:00
ProjectorViewSet,
TagViewSet,
)
2018-10-26 15:37:29 +02:00
from .websocket import (
NotifyWebsocketClientMessage,
ConstantsWebsocketClientMessage,
GetElementsWebsocketClientMessage,
AutoupdateWebsocketClientMessage,
2018-12-23 11:05:38 +01:00
ListenToProjectors,
2018-10-26 15:37:29 +02:00
)
from ..utils.access_permissions import required_user
2018-12-23 11:05:38 +01:00
from ..utils.rest_api import router
from ..utils.websocket import register_client_message
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.
2018-12-23 11:05:38 +01:00
register_projector_elements()
# Connect signals.
post_permission_creation.connect(
2019-01-06 16:22:33 +01:00
delete_django_app_permissions, dispatch_uid="delete_django_app_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(
call_save_default_values,
sender=self,
dispatch_uid="core_save_config_default_values",
)
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("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",
)
router.register(
self.get_model("ProjectorMessage").get_collection_string(),
ProjectorMessageViewSet,
)
router.register(
self.get_model("Countdown").get_collection_string(), CountdownViewSet
)
router.register(
self.get_model("History").get_collection_string(), HistoryViewSet
)
2019-01-20 10:05:50 +01:00
if "runserver" in sys.argv:
startup()
2018-09-01 08:00:00 +02:00
2018-10-26 15:37:29 +02:00
# Register client messages
register_client_message(NotifyWebsocketClientMessage())
register_client_message(ConstantsWebsocketClientMessage())
register_client_message(GetElementsWebsocketClientMessage())
register_client_message(AutoupdateWebsocketClientMessage())
2018-12-23 11:05:38 +01:00
register_client_message(ListenToProjectors())
2018-10-26 15:37:29 +02:00
# register required_users
2019-01-06 16:22:33 +01:00
required_user.add_collection_string(
self.get_model("ChatMessage").get_collection_string(), required_users
)
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",
"ChatMessage",
"Tag",
"ProjectorMessage",
"Countdown",
"ConfigStore",
"History",
):
yield self.get_model(model_name)
def get_angular_constants(self):
from .config import config
constants: Dict[str, Any] = {}
# Client settings
client_settings_keys = [
2019-01-06 16:22:33 +01:00
"MOTION_IDENTIFIER_MIN_DIGITS",
"MOTION_IDENTIFIER_WITHOUT_BLANKS",
"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
2019-01-06 16:22:33 +01:00
constants["OpenSlidesSettings"] = client_settings_dict
# Config variables
2018-08-22 22:00:08 +02:00
config_groups: List[Any] = []
2019-01-06 16:22:33 +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
2019-01-06 16:22:33 +01:00
if not config_groups or config_groups[-1]["name"] != config_variable.group:
# Add new group.
2019-01-06 16:22:33 +01:00
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.
2019-01-06 16:22:33 +01:00
config_groups[-1]["subgroups"].append(
OrderedDict(name=config_variable.subgroup, items=[])
)
# Add the config variable to the current group and subgroup.
2019-01-06 16:22:33 +01:00
config_groups[-1]["subgroups"][-1]["items"].append(config_variable.data)
constants["OpenSlidesConfigVariables"] = config_groups
return constants
2017-08-22 14:17:20 +02:00
def call_save_default_values(**kwargs):
from .config import config
2019-01-06 16:22:33 +01:00
2017-08-22 14:17:20 +02:00
config.save_default_values()
def required_users(element: Dict[str, Any]) -> Set[int]:
"""
Returns all user ids that are displayed as chatters.
"""
2019-01-06 16:22:33 +01:00
return set(element["user_id"])
2019-01-20 10:05:50 +01:00
def startup():
"""
Runs commands that are needed at startup.
Sets the cache, constants and startup history
"""
if os.environ.get("NO_STARTUP"):
return
from openslides.utils.constants import set_constants, get_constants_from_apps
from openslides.utils.cache import element_cache
from openslides.core.models import History
set_constants(get_constants_from_apps())
element_cache.ensure_cache()
History.objects.build_history()