2018-04-18 16:19:51 +02:00
|
|
|
import re
|
|
|
|
|
2017-08-30 00:07:54 +02:00
|
|
|
from typing import Generator, Type
|
|
|
|
|
2018-04-18 16:19:51 +02:00
|
|
|
from ..core.config import config
|
2016-09-17 22:26:23 +02:00
|
|
|
from ..core.exceptions import ProjectorException
|
|
|
|
from ..utils.projector import ProjectorElement
|
2017-02-17 15:32:44 +01:00
|
|
|
from .models import Motion, MotionBlock, MotionChangeRecommendation, Workflow
|
2015-06-24 22:11:54 +02:00
|
|
|
|
|
|
|
|
2015-06-25 20:36:46 +02:00
|
|
|
class MotionSlide(ProjectorElement):
|
2015-06-24 22:11:54 +02:00
|
|
|
"""
|
2015-06-25 20:36:46 +02:00
|
|
|
Slide definitions for Motion model.
|
2015-06-24 22:11:54 +02:00
|
|
|
"""
|
|
|
|
name = 'motions/motion'
|
|
|
|
|
2016-02-27 20:25:06 +01:00
|
|
|
def check_data(self):
|
2016-09-17 22:26:23 +02:00
|
|
|
if not Motion.objects.filter(pk=self.config_entry.get('id')).exists():
|
|
|
|
raise ProjectorException('Motion does not exist.')
|
2015-06-24 22:11:54 +02:00
|
|
|
|
|
|
|
def get_requirements(self, config_entry):
|
2016-09-17 22:26:23 +02:00
|
|
|
try:
|
|
|
|
motion = Motion.objects.get(pk=config_entry.get('id'))
|
|
|
|
except Motion.DoesNotExist:
|
|
|
|
# Motion does not exist. Just do nothing.
|
|
|
|
pass
|
2015-06-25 20:36:46 +02:00
|
|
|
else:
|
2016-09-17 22:26:23 +02:00
|
|
|
yield motion
|
|
|
|
yield motion.agenda_item
|
|
|
|
yield motion.state.workflow
|
2018-04-18 16:19:51 +02:00
|
|
|
yield from self.required_motions_for_state_and_recommendation(motion)
|
2018-06-12 14:17:02 +02:00
|
|
|
for submitter in motion.submitters.all():
|
|
|
|
yield submitter.user
|
2016-09-17 22:26:23 +02:00
|
|
|
yield from motion.supporters.all()
|
2016-12-19 10:40:55 +01:00
|
|
|
yield from MotionChangeRecommendation.objects.filter(motion_version=motion.get_active_version().id)
|
2016-09-23 13:12:02 +02:00
|
|
|
|
2018-04-18 16:19:51 +02:00
|
|
|
def required_motions_for_state_and_recommendation(self, motion):
|
|
|
|
"""
|
|
|
|
Returns a list of motions needed for the projector, because they are mentioned
|
|
|
|
in additional fieds for the state and recommendation.
|
|
|
|
Keep the motion_syntax syncronized with the MotionStateAndRecommendationParser on the client.
|
|
|
|
"""
|
|
|
|
# get the comments field for state and recommendation
|
|
|
|
motion_syntax = re.compile('\[motion:(\d+)\]')
|
|
|
|
fields = config['motions_comments']
|
|
|
|
state_field_id = None
|
|
|
|
recommendation_field_id = None
|
|
|
|
|
|
|
|
for id, field in fields.items():
|
2018-05-03 17:26:47 +02:00
|
|
|
if isinstance(field, dict):
|
|
|
|
if field.get('forState', False):
|
|
|
|
state_field_id = id
|
|
|
|
if field.get('forRecommendation', False):
|
|
|
|
recommendation_field_id = id
|
2018-04-18 16:19:51 +02:00
|
|
|
|
|
|
|
# extract all mentioned motions from the state and recommendation
|
|
|
|
motion_ids = set()
|
|
|
|
if state_field_id is not None:
|
|
|
|
state_text = motion.comments.get(state_field_id)
|
|
|
|
motion_ids.update([int(id) for id in motion_syntax.findall(state_text)])
|
|
|
|
|
|
|
|
if recommendation_field_id is not None:
|
|
|
|
recommendation_text = motion.comments.get(recommendation_field_id)
|
|
|
|
motion_ids.update([int(id) for id in motion_syntax.findall(recommendation_text)])
|
|
|
|
|
|
|
|
# return all motions
|
|
|
|
return Motion.objects.filter(pk__in=motion_ids)
|
|
|
|
|
2016-09-30 23:39:42 +02:00
|
|
|
def get_collection_elements_required_for_this(self, collection_element, config_entry):
|
|
|
|
output = super().get_collection_elements_required_for_this(collection_element, config_entry)
|
2016-09-23 13:12:02 +02:00
|
|
|
# Full update if motion changes because then we may have new
|
|
|
|
# submitters or supporters and therefor need new users.
|
|
|
|
#
|
|
|
|
# Add some logic here if we support live changing of workflows later.
|
|
|
|
#
|
2017-03-09 00:01:51 +01:00
|
|
|
if collection_element.collection_string == Motion.get_collection_string() and collection_element.id == config_entry.get('id'):
|
2016-09-30 23:39:42 +02:00
|
|
|
output.extend(self.get_requirements_as_collection_elements(config_entry))
|
|
|
|
return output
|
2016-10-05 18:25:50 +02:00
|
|
|
|
|
|
|
def update_data(self):
|
|
|
|
data = None
|
|
|
|
try:
|
|
|
|
motion = Motion.objects.get(pk=self.config_entry.get('id'))
|
|
|
|
except Motion.DoesNotExist:
|
|
|
|
# Motion does not exist, so just do nothing.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
data = {'agenda_item_id': motion.agenda_item_id}
|
|
|
|
return data
|
2016-10-01 20:42:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MotionBlockSlide(ProjectorElement):
|
|
|
|
"""
|
|
|
|
Slide definitions for a block of motions (MotionBlock model).
|
|
|
|
"""
|
|
|
|
name = 'motions/motion-block'
|
|
|
|
|
|
|
|
def check_data(self):
|
|
|
|
if not MotionBlock.objects.filter(pk=self.config_entry.get('id')).exists():
|
|
|
|
raise ProjectorException('MotionBlock does not exist.')
|
|
|
|
|
|
|
|
def get_requirements(self, config_entry):
|
|
|
|
try:
|
|
|
|
motion_block = MotionBlock.objects.get(pk=config_entry.get('id'))
|
|
|
|
except MotionBlock.DoesNotExist:
|
|
|
|
# MotionBlock does not exist. Just do nothing.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
yield motion_block
|
|
|
|
yield motion_block.agenda_item
|
|
|
|
yield from motion_block.motion_set.all()
|
2017-02-17 15:32:44 +01:00
|
|
|
yield from Workflow.objects.all()
|
2016-10-01 20:42:44 +02:00
|
|
|
|
|
|
|
def get_collection_elements_required_for_this(self, collection_element, config_entry):
|
|
|
|
output = super().get_collection_elements_required_for_this(collection_element, config_entry)
|
2017-02-17 13:21:11 +01:00
|
|
|
# Send all changed motions to the projector, because it may be appended
|
2016-10-01 20:42:44 +02:00
|
|
|
# or removed from the block.
|
|
|
|
if collection_element.collection_string == Motion.get_collection_string():
|
2017-02-17 13:21:11 +01:00
|
|
|
output.append(collection_element)
|
2016-10-01 20:42:44 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
def update_data(self):
|
|
|
|
data = None
|
|
|
|
try:
|
|
|
|
motion_block = MotionBlock.objects.get(pk=self.config_entry.get('id'))
|
|
|
|
except MotionBlock.DoesNotExist:
|
|
|
|
# MotionBlock does not exist, so just do nothing.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
data = {'agenda_item_id': motion_block.agenda_item_id}
|
|
|
|
return data
|
2017-08-30 00:07:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_projector_elements() -> Generator[Type[ProjectorElement], None, None]:
|
|
|
|
yield MotionSlide
|
|
|
|
yield MotionBlockSlide
|