OpenSlides/server/openslides/mediafiles/config.py
FinnStutzenstein 2bcab5d098
Repository restructure
- moved all server related things into the folder `server`, so this
configuration is parallel to the client.
- All main "services" are now folders in the root directory
- Added Dockerfiles to each service (currently server and client)
- Added a docker compose configuration to start everything together.
Currently there are heavy dependencies into https://github.com/OpenSlides/openslides-docker-compose
- Resturctured the .gitignore. If someone needs something excluded,
please add it to the right section.
- Added initial build setup with Docker and docker-compose.
- removed setup.py. We won't deliver OpenSlides via pip anymore.
2020-08-21 08:11:13 +02:00

55 lines
1.8 KiB
Python

from contextlib import contextmanager
from ..core.config import config
from .models import Mediafile
@contextmanager
def watch_and_update_configs():
"""
Watches each font and logo config for changes. If some mediafiles were updated
(also their parents, so some path changes) or were deleted, all affected configs
are updated.
"""
# 1) map logo and font config keys to mediafile ids
mediafiles = Mediafile.objects.get_prefetched_queryset().all()
logos = build_mapping("logos_available", mediafiles)
fonts = build_mapping("fonts_available", mediafiles)
yield
# 2) update changed paths/urls
mediafiles = Mediafile.objects.get_prefetched_queryset().all()
update_mapping(logos, mediafiles)
update_mapping(fonts, mediafiles)
def build_mapping(base_config_key, mediafiles):
""" Returns a map of config keys to medaifile ids """
logos = {}
for key in config[base_config_key]:
url = config[key]["path"]
for mediafile in mediafiles:
if mediafile.url == url:
logos[key] = mediafile.id
break
return logos
def update_mapping(mapping, mediafiles):
"""
Tries to get the mediafile from the id for a specific config field.
If the file was found and the path changed, the config is updated. If the
mediafile cound not be found, the config is cleared (mediafile deleted).
"""
for key, id in mapping.items():
try:
mediafile = mediafiles.filter(pk=id)[0]
print(config[key]["path"], mediafile.url)
if config[key]["path"] != mediafile.url:
config[key] = {
"display_name": config[key]["display_name"],
"path": mediafile.url,
}
except IndexError:
config[key] = {"display_name": config[key]["display_name"], "path": ""}