pollmergehandler.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from django.utils.translation import ugettext as _
  2. from misago.threads.models import Poll
  3. class PollMergeHandler(object):
  4. def __init__(self, threads):
  5. self._list = []
  6. self._choices = {0: None}
  7. self._is_valid = False
  8. self._resolution = None
  9. self.threads = threads
  10. for thread in threads:
  11. try:
  12. self._list.append(thread.poll)
  13. self._choices[thread.poll.pk] = thread.poll
  14. except Poll.DoesNotExist:
  15. pass
  16. self._list.sort(key=lambda choice: choice.thread_id)
  17. @property
  18. def polls(self):
  19. return self._list
  20. def is_merge_conflict(self):
  21. return len(self._list) > 1
  22. def get_available_resolutions(self):
  23. resolutions = [(0, _("Delete all polls"))]
  24. for poll in self._list:
  25. resolutions.append((poll.pk, poll.question))
  26. return resolutions
  27. def set_resolution(self, resolution):
  28. try:
  29. resolution_clean = int(resolution)
  30. except (TypeError, ValueError):
  31. return
  32. if resolution_clean in self._choices:
  33. self._resolution = self._choices[resolution_clean]
  34. self._is_valid = True
  35. def is_valid(self):
  36. return self._is_valid
  37. def get_resolution(self):
  38. return self._resolution or None