def commit(self): if not self.is_validated(): return False poll = self.poll or PollRepository().create() poll.title = self.title.value poll.question = self.question.value poll.multiple = self.multiple.value poll.end_date = self.end_date.value + ONE_DAY # insert/update choices polls_to_delete = [] for idx, choice_property in enumerate(self.choices): choice_label = choice_property.value if choice_label: choice = poll.choices[idx] if idx < len( poll.choices) else PollChoiceData(poll=poll) choice.label = choice_label else: if idx < len(poll.choices): polls_to_delete.append(poll.choices[idx].id) # delete choices for id in polls_to_delete: for choice in poll.choices: if choice.id == id: choice.delete() return True
class PollsAdmin(object): def __init__(self): self.poll_repository = PollRepository() self.content = component.Component(None) @property def polls(self): return sorted(self.poll_repository.get_all(), key=lambda poll: poll.end_date, reverse=True) def set_enabled(self, id, status): # disable other polls for poll in self.polls: poll.enabled = False # enable the current poll, if necessary if status: self.poll_repository.get_by_id(id).enabled = True def delete_poll(self, id): self.poll_repository.get_by_id(id).delete()
def poll(self): return PollRepository().get_by_id(self.id)
def __init__(self): self.poll_repository = PollRepository() self.content = component.Component(None)