pollmodel.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from datetime import timedelta
  2. from django.db import models
  3. from django.utils import timezone
  4. import base64
  5. try:
  6. import cPickle as pickle
  7. except ImportError:
  8. import pickle
  9. class Poll(models.Model):
  10. forum = models.ForeignKey('Forum')
  11. thread = models.OneToOneField('Thread', primary_key=True)
  12. user = models.ForeignKey('User', null=True, blank=True, on_delete=models.SET_NULL)
  13. user_name = models.CharField(max_length=255, null=True, blank=True)
  14. user_slug = models.SlugField(max_length=255, null=True, blank=True)
  15. start_date = models.DateTimeField()
  16. length = models.PositiveIntegerField(default=0)
  17. question = models.CharField(max_length=255)
  18. max_choices = models.PositiveIntegerField(default=0)
  19. _choices_cache = models.TextField(db_column='choices_cache')
  20. votes = models.PositiveIntegerField(default=0)
  21. vote_changing = models.BooleanField(default=False)
  22. public = models.BooleanField(default=False)
  23. class Meta:
  24. app_label = 'misago'
  25. @property
  26. def end_date(self):
  27. return self.start_date + timedelta(days=self.length)
  28. @property
  29. def over(self):
  30. if not self.length:
  31. return False
  32. return timezone.now() > self.end_date
  33. def make_choices_cache(self):
  34. self.choices_cache = [x for x in self.option_set.all()]
  35. @property
  36. def choices_cache(self):
  37. try:
  38. return self._cache
  39. except AttributeError:
  40. pass
  41. try:
  42. self._cache = pickle.loads(base64.decodestring(self._choices_cache))
  43. except Exception:
  44. self._cache = []
  45. return self._cache
  46. @choices_cache.setter
  47. def choices_cache(self, choices):
  48. choices_cache = []
  49. for choice in choices:
  50. choices_cache.append({
  51. 'id': choice.pk,
  52. 'pk': choice.pk,
  53. 'name': choice.name,
  54. 'votes': choice.votes
  55. })
  56. self._cache = choices_cache
  57. self._choices_cache = base64.encodestring(pickle.dumps(choices_cache, pickle.HIGHEST_PROTOCOL))
  58. def retract_votes(self, votes):
  59. options = self.option_set.all()
  60. options_dict = {}
  61. for option in options:
  62. options_dict[option.pk] = option
  63. for vote in votes:
  64. if vote.option_id in options_dict:
  65. self.votes -= 1
  66. options_dict[vote.option_id].votes -= 1
  67. self.vote_set.filter(id__in=[x.pk for x in votes]).delete()
  68. for option in options:
  69. option.save()
  70. self.choices_cache = options
  71. def make_vote(self, request, options=None):
  72. try:
  73. len(options)
  74. except TypeError:
  75. options = (options, )
  76. for option in self.option_set.all():
  77. if option.pk in options:
  78. self.votes += 1
  79. option.votes += 1
  80. option.save()
  81. self.vote_set.create(
  82. forum_id=self.forum_id,
  83. thread_id=self.thread_id,
  84. option=option,
  85. user=request.user,
  86. user_name=request.user.username,
  87. user_slug=request.user.username_slug,
  88. date=timezone.now(),
  89. ip=request.session.get_ip(request),
  90. agent=request.META.get('HTTP_USER_AGENT'),
  91. )
  92. self.make_choices_cache()
  93. def make_empty_vote(self, request):
  94. self.vote_set.create(
  95. forum_id=self.forum_id,
  96. thread_id=self.thread_id,
  97. user=request.user,
  98. user_name=request.user.username,
  99. user_slug=request.user.username_slug,
  100. date=timezone.now(),
  101. ip=request.session.get_ip(request),
  102. agent=request.META.get('HTTP_USER_AGENT'),
  103. )