symposion_app/pinaxcon/raffle/models.py
2019-01-22 08:16:38 +13:00

86 lines
No EOL
2.8 KiB
Python

from django.db import models
from pinaxcon.raffle.mixins import PrizeMixin, RaffleMixin
class Raffle(RaffleMixin, models.Model):
"""
Stores a single Raffle object, related to one or many
:model:`pinaxcon_registrasion.Product`, which is usually a raffle ticket,
but can be set to tickets or other products for door prizes.
"""
description = models.CharField(max_length=255)
products = models.ManyToManyField('registrasion.Product')
hidden = models.BooleanField(default=True)
def __str__(self):
return self.description
class Prize(PrizeMixin, models.Model):
"""
Stores a Prize for a given :model:`pinaxcon_raffle.Raffle`.
Once `winning_ticket` has been set to a :model:`pinaxcon_raffle.DrawnTicket`
object, no further changes are permitted unless the object is explicitely
unlocked.
"""
description = models.CharField(max_length=255)
raffle = models.ForeignKey('pinaxcon_raffle.Raffle', related_name='prizes')
order = models.PositiveIntegerField()
winning_ticket = models.OneToOneField(
'pinaxcon_raffle.DrawnTicket', null=True,
blank=True, related_name='+', on_delete=models.PROTECT
)
class Meta:
unique_together = ('raffle', 'order')
def __str__(self):
return f"{self.order}. Prize: {self.description}"
class PrizeAudit(models.Model):
"""
Stores an audit event for changes to a particular :model:`pinaxcon_raffle.Prize`.
"""
reason = models.CharField(max_length=255)
prize = models.ForeignKey('pinaxcon_raffle.Prize', related_name='audit_events')
user = models.ForeignKey('auth.User')
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-timestamp',)
def __str__(self):
return self.reason
class Draw(models.Model):
"""
Stores a draw for a given :model:`pinaxcon_raffle.Raffle`, along with audit fields
for the creating :model:`auth.User` and the creation timestamp.
"""
raffle = models.ForeignKey('pinaxcon_raffle.Raffle', related_name='draws')
drawn_by = models.ForeignKey('auth.User')
drawn_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.raffle}: {self.drawn_time}"
class DrawnTicket(models.Model):
"""
Stores the result of a ticket draw, along with the corresponding
:model:`pinaxcon_raffle.Draw`, :model:`pinaxcon_raffle.Prize` and the
:model:`registrasion.commerce.LineItem` from which it was generated.
"""
ticket = models.CharField(max_length=255)
draw = models.ForeignKey('pinaxcon_raffle.Draw')
prize = models.ForeignKey('pinaxcon_raffle.Prize')
lineitem = models.ForeignKey('registrasion.LineItem')
def __str__(self):
return f"{self.ticket}: {self.draw.raffle}"