Files
@ 55814a57b3f0
Branch filter:
Location: symposion_app/symposion/reviews/models.py
55814a57b3f0
12.4 KiB
text/x-python
admin: inline edit section
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | # -*- coding: utf-8 -*-
from datetime import datetime
from decimal import Decimal
from django.db import models
from django.db.models import Q
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from markitup.fields import MarkupField
from symposion.proposals.models import ProposalBase
from symposion.schedule.models import Presentation
class ProposalScoreExpression(object):
def as_sql(self, qn, connection=None):
sql = "((3 * plus_one + plus_zero) - (minus_zero + 3 * minus_one))"
return sql, []
def prepare_database_save(self, unused):
return self
class Votes(object):
PLUS_ONE = "+1"
PLUS_ZERO = "+0"
MINUS_ZERO = u"−0"
MINUS_ONE = u"−1"
CHOICES = [
(PLUS_ONE, u"+1 — Good proposal and I will argue for it to be accepted."),
(PLUS_ZERO, u"+0 — OK proposal, but I will not argue for it to be accepted."),
(MINUS_ZERO, u"−0 — Weak proposal, but I will not argue strongly against acceptance."),
(MINUS_ONE, u"−1 — Serious issues and I will argue to reject this proposal."),
]
VOTES = Votes()
class ReviewAssignment(models.Model):
AUTO_ASSIGNED_INITIAL = 0
OPT_IN = 1
AUTO_ASSIGNED_LATER = 2
NUM_REVIEWERS = 3
ORIGIN_CHOICES = [
(AUTO_ASSIGNED_INITIAL, "auto-assigned, initial"),
(OPT_IN, "opted-in"),
(AUTO_ASSIGNED_LATER, "auto-assigned, later"),
]
proposal = models.ForeignKey(ProposalBase)
user = models.ForeignKey(User)
origin = models.IntegerField(choices=ORIGIN_CHOICES)
assigned_at = models.DateTimeField(default=datetime.now)
opted_out = models.BooleanField(default=False)
@classmethod
def create_assignments(cls, proposal, origin=AUTO_ASSIGNED_INITIAL):
speakers = [proposal.speaker] + list(proposal.additional_speakers.all())
reviewers = User.objects.exclude(
pk__in=[
speaker.user_id
for speaker in speakers
if speaker.user_id is not None
] + [
assignment.user_id
for assignment in ReviewAssignment.objects.filter(
proposal_id=proposal.id)]
).filter(
groups__name="reviewers",
).filter(
Q(reviewassignment__opted_out=False) | Q(reviewassignment=None)
).annotate(
num_assignments=models.Count("reviewassignment")
).order_by(
"num_assignments", "?",
)
num_assigned_reviewers = ReviewAssignment.objects.filter(
proposal_id=proposal.id, opted_out=0).count()
for reviewer in reviewers[:max(0, cls.NUM_REVIEWERS - num_assigned_reviewers)]:
cls._default_manager.create(
proposal=proposal,
user=reviewer,
origin=origin,
)
class ProposalMessage(models.Model):
proposal = models.ForeignKey(ProposalBase, related_name="messages")
user = models.ForeignKey(User)
message = MarkupField()
submitted_at = models.DateTimeField(default=datetime.now, editable=False)
class Meta:
ordering = ["submitted_at"]
class Review(models.Model):
VOTES = VOTES
proposal = models.ForeignKey(ProposalBase, related_name="reviews")
user = models.ForeignKey(User)
# No way to encode "-0" vs. "+0" into an IntegerField, and I don't feel
# like some complicated encoding system.
vote = models.CharField(max_length=2, blank=True, choices=VOTES.CHOICES)
comment = MarkupField()
submitted_at = models.DateTimeField(default=datetime.now, editable=False)
def save(self, **kwargs):
if self.vote:
vote, created = LatestVote.objects.get_or_create(
proposal=self.proposal,
user=self.user,
defaults=dict(
vote=self.vote,
submitted_at=self.submitted_at,
)
)
if not created:
LatestVote.objects.filter(pk=vote.pk).update(vote=self.vote)
self.proposal.result.update_vote(self.vote, previous=vote.vote)
else:
self.proposal.result.update_vote(self.vote)
super(Review, self).save(**kwargs)
def delete(self):
model = self.__class__
user_reviews = model._default_manager.filter(
proposal=self.proposal,
user=self.user,
)
try:
# find the latest review
latest = user_reviews.exclude(pk=self.pk).order_by("-submitted_at")[0]
except IndexError:
# did not find a latest which means this must be the only one.
# treat it as a last, but delete the latest vote.
self.proposal.result.update_vote(self.vote, removal=True)
lv = LatestVote.objects.filter(proposal=self.proposal, user=self.user)
lv.delete()
else:
# handle that we've found a latest vote
# check if self is the lastest vote
if self == latest:
# self is the latest review; revert the latest vote to the
# previous vote
previous = user_reviews.filter(submitted_at__lt=self.submitted_at)\
.order_by("-submitted_at")[0]
self.proposal.result.update_vote(self.vote, previous=previous.vote, removal=True)
lv = LatestVote.objects.filter(proposal=self.proposal, user=self.user)
lv.update(
vote=previous.vote,
submitted_at=previous.submitted_at,
)
else:
# self is not the latest review so we just need to decrement
# the comment count
self.proposal.result.comment_count = models.F("comment_count") - 1
self.proposal.result.save()
# in all cases we need to delete the review; let's do it!
super(Review, self).delete()
def css_class(self):
return {
self.VOTES.PLUS_ONE: "plus-one",
self.VOTES.PLUS_ZERO: "plus-zero",
self.VOTES.MINUS_ZERO: "minus-zero",
self.VOTES.MINUS_ONE: "minus-one",
}[self.vote]
@property
def section(self):
return self.proposal.kind.section.slug
class LatestVote(models.Model):
VOTES = VOTES
proposal = models.ForeignKey(ProposalBase, related_name="votes")
user = models.ForeignKey(User)
# No way to encode "-0" vs. "+0" into an IntegerField, and I don't feel
# like some complicated encoding system.
vote = models.CharField(max_length=2, choices=VOTES.CHOICES)
submitted_at = models.DateTimeField(default=datetime.now, editable=False)
class Meta:
unique_together = [("proposal", "user")]
def css_class(self):
return {
self.VOTES.PLUS_ONE: "plus-one",
self.VOTES.PLUS_ZERO: "plus-zero",
self.VOTES.MINUS_ZERO: "minus-zero",
self.VOTES.MINUS_ONE: "minus-one",
}[self.vote]
class ProposalResult(models.Model):
proposal = models.OneToOneField(ProposalBase, related_name="result")
score = models.DecimalField(max_digits=5, decimal_places=2, default=Decimal("0.00"))
comment_count = models.PositiveIntegerField(default=0)
vote_count = models.PositiveIntegerField(default=0)
plus_one = models.PositiveIntegerField(default=0)
plus_zero = models.PositiveIntegerField(default=0)
minus_zero = models.PositiveIntegerField(default=0)
minus_one = models.PositiveIntegerField(default=0)
accepted = models.NullBooleanField(choices=[
(True, "accepted"),
(False, "rejected"),
(None, "undecided"),
], default=None)
status = models.CharField(max_length=20, choices=[
("accepted", "accepted"),
("rejected", "rejected"),
("undecided", "undecided"),
("standby", "standby"),
], default="undecided")
@classmethod
def full_calculate(cls):
for proposal in ProposalBase.objects.all():
result, created = cls._default_manager.get_or_create(proposal=proposal)
result.comment_count = Review.objects.filter(proposal=proposal).count()
result.vote_count = LatestVote.objects.filter(proposal=proposal).count()
result.plus_one = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.PLUS_ONE
).count()
result.plus_zero = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.PLUS_ZERO
).count()
result.minus_zero = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.MINUS_ZERO
).count()
result.minus_one = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.MINUS_ONE
).count()
result.save()
cls._default_manager.filter(pk=result.pk).update(score=ProposalScoreExpression())
def update_vote(self, vote, previous=None, removal=False):
mapping = {
VOTES.PLUS_ONE: "plus_one",
VOTES.PLUS_ZERO: "plus_zero",
VOTES.MINUS_ZERO: "minus_zero",
VOTES.MINUS_ONE: "minus_one",
}
if previous:
if previous == vote:
return
if removal:
setattr(self, mapping[previous], models.F(mapping[previous]) + 1)
else:
setattr(self, mapping[previous], models.F(mapping[previous]) - 1)
else:
if removal:
self.vote_count = models.F("vote_count") - 1
else:
self.vote_count = models.F("vote_count") + 1
if removal:
setattr(self, mapping[vote], models.F(mapping[vote]) - 1)
self.comment_count = models.F("comment_count") - 1
else:
setattr(self, mapping[vote], models.F(mapping[vote]) + 1)
self.comment_count = models.F("comment_count") + 1
self.save()
model = self.__class__
model._default_manager.filter(pk=self.pk).update(score=ProposalScoreExpression())
class Comment(models.Model):
proposal = models.ForeignKey(ProposalBase, related_name="comments")
commenter = models.ForeignKey(User)
text = MarkupField()
# Or perhaps more accurately, can the user see this comment.
public = models.BooleanField(choices=[(True, "public"), (False, "private")], default=False)
commented_at = models.DateTimeField(default=datetime.now)
class NotificationTemplate(models.Model):
label = models.CharField(max_length=100)
from_address = models.EmailField()
subject = models.CharField(max_length=100)
body = models.TextField()
class ResultNotification(models.Model):
proposal = models.ForeignKey(ProposalBase, related_name="notifications")
template = models.ForeignKey(NotificationTemplate, null=True, blank=True,
on_delete=models.SET_NULL)
timestamp = models.DateTimeField(default=datetime.now)
to_address = models.EmailField()
from_address = models.EmailField()
subject = models.CharField(max_length=100)
body = models.TextField()
def recipients(self):
for speaker in self.proposal.speakers():
yield speaker.email
@property
def email_args(self):
return (self.subject, self.body, self.from_address, self.recipients())
def promote_proposal(proposal):
if hasattr(proposal, "presentation") and proposal.presentation:
# already promoted
presentation = proposal.presentation
else:
presentation = Presentation(
title=proposal.title,
description=proposal.description,
abstract=proposal.abstract,
speaker=proposal.speaker,
section=proposal.section,
proposal_base=proposal,
)
presentation.save()
for speaker in proposal.additional_speakers.all():
presentation.additional_speakers.add(speaker)
presentation.save()
return presentation
def unpromote_proposal(proposal):
if hasattr(proposal, "presentation") and proposal.presentation:
proposal.presentation.delete()
def accepted_proposal(sender, instance=None, **kwargs):
if instance is None:
return
if instance.status == "accepted":
promote_proposal(instance.proposal)
else:
unpromote_proposal(instance.proposal)
post_save.connect(accepted_proposal, sender=ProposalResult)
|