2017-08-24 12:26:55 +02:00
|
|
|
from typing import Any, Dict, Iterable, List, Optional # noqa
|
2017-08-23 20:51:06 +02:00
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
from django.dispatch import Signal
|
|
|
|
|
2016-09-30 23:39:42 +02:00
|
|
|
from .collection import CollectionElement
|
2015-02-18 01:45:39 +01:00
|
|
|
from .dispatch import SignalConnectMetaClass
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectorElement(object, metaclass=SignalConnectMetaClass):
|
|
|
|
"""
|
|
|
|
Base class for an element on the projector.
|
|
|
|
|
|
|
|
Every app which wants to add projector elements has to create classes
|
2015-06-17 09:45:00 +02:00
|
|
|
subclassing from this base class with different names. The name attribute
|
|
|
|
has to be set. The metaclass (SignalConnectMetaClass) does the rest of the
|
|
|
|
magic.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
|
|
|
signal = Signal()
|
2017-08-23 20:51:06 +02:00
|
|
|
name = None # type: Optional[str]
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def __init__(self, **kwargs: str) -> None:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
|
|
|
Initializes the projector element instance. This is done when the
|
|
|
|
signal is sent.
|
|
|
|
|
|
|
|
Because of Django's signal API, we have to take wildcard keyword
|
|
|
|
arguments. But they are not used here.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
2017-08-24 12:26:55 +02:00
|
|
|
def get_dispatch_uid(cls) -> Optional[str]:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
|
|
|
Returns the classname as a unique string for each class. Returns None
|
|
|
|
for the base class so it will not be connected to the signal.
|
|
|
|
"""
|
|
|
|
if not cls.__name__ == 'ProjectorElement':
|
|
|
|
return cls.__name__
|
2017-08-24 12:26:55 +02:00
|
|
|
return None
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def check_and_update_data(self, projector_object: Any, config_entry: Any) -> Any:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2016-02-27 20:25:06 +01:00
|
|
|
Checks projector element data via self.check_data() and updates
|
|
|
|
them via self.update_data(). The projector object and the config
|
|
|
|
entry have to be given.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
|
|
|
self.projector_object = projector_object
|
|
|
|
self.config_entry = config_entry
|
|
|
|
assert self.config_entry.get('name') == self.name, (
|
|
|
|
'To get data of a projector element, the correct config entry has to be given.')
|
2016-02-27 20:25:06 +01:00
|
|
|
self.check_data()
|
|
|
|
return self.update_data() or {}
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def check_data(self) -> None:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2016-02-27 20:25:06 +01:00
|
|
|
Method can be overridden to validate projector element data. This
|
|
|
|
may raise ProjectorException in case of an error.
|
|
|
|
|
|
|
|
Default: Does nothing.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def update_data(self) -> Dict[Any, Any]:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2016-02-27 20:25:06 +01:00
|
|
|
Method can be overridden to update the projector element data
|
|
|
|
output. This should return a dictonary. Use this for server
|
|
|
|
calculated data which have to be forwared to the client.
|
|
|
|
|
|
|
|
Default: Does nothing.
|
|
|
|
"""
|
|
|
|
pass
|
2015-06-12 21:08:57 +02:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def get_requirements(self, config_entry: Any) -> Iterable[Any]:
|
2015-06-12 21:08:57 +02:00
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
Returns an iterable of instances that are required for this projector
|
|
|
|
element. The config_entry has to be given.
|
2015-06-12 21:08:57 +02:00
|
|
|
"""
|
|
|
|
return ()
|
2016-09-23 13:12:02 +02:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def get_requirements_as_collection_elements(self, config_entry: Any) -> Iterable[CollectionElement]:
|
2016-09-23 13:12:02 +02:00
|
|
|
"""
|
2016-09-30 23:39:42 +02:00
|
|
|
Returns an iterable of collection elements that are required for this
|
|
|
|
projector element. The config_entry has to be given.
|
2016-09-23 13:12:02 +02:00
|
|
|
"""
|
2016-09-30 23:39:42 +02:00
|
|
|
return (CollectionElement.from_instance(instance) for instance in self.get_requirements(config_entry))
|
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def get_collection_elements_required_for_this(
|
|
|
|
self, collection_element: CollectionElement,
|
|
|
|
config_entry: Any) -> List[CollectionElement]:
|
2016-09-30 23:39:42 +02:00
|
|
|
"""
|
|
|
|
Returns a list of CollectionElements that have to be sent to every
|
|
|
|
projector that shows this projector element according to the given
|
2016-10-01 12:58:49 +02:00
|
|
|
collection_element.
|
2016-09-30 23:39:42 +02:00
|
|
|
|
|
|
|
Default: Returns only the collection_element if it belongs to the
|
|
|
|
requirements but return all requirements if the projector changes.
|
|
|
|
"""
|
|
|
|
requirements_as_collection_elements = list(self.get_requirements_as_collection_elements(config_entry))
|
|
|
|
for requirement in requirements_as_collection_elements:
|
|
|
|
if collection_element == requirement:
|
|
|
|
output = [collection_element]
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if collection_element.information.get('this_projector'):
|
|
|
|
output = [collection_element]
|
|
|
|
output.extend(requirements_as_collection_elements)
|
|
|
|
else:
|
|
|
|
output = []
|
|
|
|
return output
|