gestioCOF/bda/models.py

217 lines
7.6 KiB
Python
Raw Normal View History

2016-07-15 00:02:56 +02:00
# -*- coding: utf-8 -*-
2012-07-11 17:39:20 +02:00
2016-07-15 00:02:56 +02:00
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import calendar
2016-07-22 22:48:09 +02:00
import datetime
2012-07-11 17:39:20 +02:00
from django.db import models
from django.contrib.auth.models import User
from django.template import loader, Context
from django.core import mail
from django.conf import settings
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
def render_template(template_name, data):
tmpl = loader.get_template(template_name)
ctxt = Context(data)
return tmpl.render(ctxt)
2012-07-11 17:39:20 +02:00
@python_2_unicode_compatible
class Tirage(models.Model):
title = models.CharField("Titre", max_length=300)
ouverture = models.DateTimeField("Date et heure d'ouverture du tirage")
fermeture = models.DateTimeField("Date et heure de fermerture du tirage")
token = models.TextField("Graine du tirage", blank=True)
active = models.BooleanField("Tirage actif", default=False)
def date_no_seconds(self):
return self.fermeture.strftime('%d %b %Y %H:%M')
def __str__(self):
return "%s - %s" % (self.title, self.date_no_seconds())
@python_2_unicode_compatible
class Salle(models.Model):
name = models.CharField("Nom", max_length=300)
address = models.TextField("Adresse")
def __str__(self):
return self.name
@python_2_unicode_compatible
class Spectacle(models.Model):
title = models.CharField("Titre", max_length=300)
date = models.DateTimeField("Date & heure")
location = models.ForeignKey(Salle)
description = models.TextField("Description", blank=True)
slots_description = models.TextField("Description des places", blank=True)
price = models.FloatField("Prix d'une place")
slots = models.IntegerField("Places")
priority = models.IntegerField("Priorité", default=1000)
tirage = models.ForeignKey(Tirage)
listing = models.BooleanField("Les places sont sur listing")
rappel_sent = models.DateTimeField("Mail de rappel envoyé", blank=True,
null=True)
2012-07-11 17:39:20 +02:00
class Meta:
verbose_name = "Spectacle"
ordering = ("priority", "date", "title",)
2012-07-11 17:39:20 +02:00
def __repr__(self):
return "[%s]" % self
2012-07-11 17:39:20 +02:00
def timestamp(self):
return "%d" % calendar.timegm(self.date.utctimetuple())
def date_no_seconds(self):
return self.date.strftime('%d %b %Y %H:%M')
def __str__(self):
return "%s - %s, %s, %.02f" % (self.title, self.date_no_seconds(),
self.location, self.price)
def send_rappel(self):
# On récupère la liste des participants
members = {}
for attr in Attribution.objects.filter(spectacle=self).all():
member = attr.participant.user
if member.id in members:
members[member.id].nb_attr = 2
else:
member.nb_attr = 1
members[member.id] = member
# On écrit un mail personnalisé à chaque participant
mails_to_send = []
mail_object = "%s - %s - %s" % (self.title, self.date_no_seconds(),
self.location)
for member in members.values():
mail_body = render_template('mail-rappel.txt', {
'member': member,
'show': self})
mail_tot = mail.EmailMessage(
mail_object, mail_body,
settings.RAPPEL_FROM, [member.email],
[], headers={'Reply-To': settings.RAPPEL_REPLY_TO})
mails_to_send.append(mail_tot)
# On envoie les mails
connection = mail.get_connection()
connection.send_messages(mails_to_send)
# On enregistre le fait que l'envoi a bien eu lieu
self.rappel_sent = timezone.now()
self.save()
# On renvoie la liste des destinataires
return members.values()
PAYMENT_TYPES = (
2016-07-10 10:46:45 +02:00
("cash", "Cash"),
("cb", "CB"),
2016-07-10 10:46:45 +02:00
("cheque", "Chèque"),
("autre", "Autre"),
)
2012-07-11 17:39:20 +02:00
@python_2_unicode_compatible
class Participant(models.Model):
user = models.ForeignKey(User)
choices = models.ManyToManyField(Spectacle,
through="ChoixSpectacle",
related_name="chosen_by")
attributions = models.ManyToManyField(Spectacle,
through="Attribution",
related_name="attributed_to")
paid = models.BooleanField("A payé", default=False)
paymenttype = models.CharField("Moyen de paiement",
max_length=6, choices=PAYMENT_TYPES,
blank=True)
tirage = models.ForeignKey(Tirage)
2016-07-25 02:51:19 +02:00
choicesrevente = models.ManyToManyField(Spectacle,
related_name="revente")
def __str__(self):
return "%s - %s" % (self.user, self.tirage.title)
2012-07-11 17:39:20 +02:00
2015-09-13 18:23:47 +02:00
DOUBLE_CHOICES = (
("1", "1 place"),
("autoquit", "2 places si possible, 1 sinon"),
("double", "2 places sinon rien"),
)
2016-07-15 00:02:56 +02:00
@python_2_unicode_compatible
class ChoixSpectacle(models.Model):
2012-07-11 17:39:20 +02:00
participant = models.ForeignKey(Participant)
spectacle = models.ForeignKey(Spectacle, related_name="participants")
2012-07-11 17:39:20 +02:00
priority = models.PositiveIntegerField("Priorité")
double_choice = models.CharField("Nombre de places",
default="1", choices=DOUBLE_CHOICES,
max_length=10)
2015-09-13 18:23:47 +02:00
def get_double(self):
return self.double_choice != "1"
double = property(get_double)
def get_autoquit(self):
return self.double_choice == "autoquit"
autoquit = property(get_autoquit)
def __str__(self):
return "Vœux de %s pour %s" % (
self.participant.user.get_full_name,
self.spectacle.title)
2012-07-11 17:39:20 +02:00
class Meta:
ordering = ("priority",)
unique_together = (("participant", "spectacle",),)
2012-07-11 17:39:20 +02:00
verbose_name = "voeu"
verbose_name_plural = "voeux"
2016-07-10 04:49:24 +02:00
@python_2_unicode_compatible
class Attribution(models.Model):
participant = models.ForeignKey(Participant)
spectacle = models.ForeignKey(Spectacle, related_name="attribues")
given = models.BooleanField("Donnée", default=False)
def __str__(self):
2016-07-23 22:21:30 +02:00
return "%s -- %s, %s" % (self.participant.user, self.spectacle.title,
self.spectacle.date)
2016-07-22 22:48:09 +02:00
@python_2_unicode_compatible
class SpectacleRevente(models.Model):
2016-07-24 00:48:05 +02:00
attribution = models.OneToOneField(Attribution,
related_name="revente")
2016-07-22 22:48:09 +02:00
date = models.DateTimeField("Date de mise en vente",
default=timezone.now)
2016-07-25 02:51:19 +02:00
interested = models.ManyToManyField(Participant,
related_name="wanted",
2016-07-25 23:03:33 +02:00
blank=True)
2016-07-25 02:51:19 +02:00
soldTo = models.ForeignKey(Participant, blank=True, null=True)
2016-07-22 22:48:09 +02:00
def get_expiration_time(self):
remaining_time = (self.attribution.spectacle.date - self.date)
delay = max(datetime.timedelta(hours=2),
2016-07-23 22:21:30 +02:00
min(remaining_time//2, datetime.timedelta(days=2)))
2016-07-24 00:48:05 +02:00
return self.date + delay + datetime.timedelta(hours=1)
2016-07-22 22:48:09 +02:00
expiration_time = property(get_expiration_time)
def get_shotgun(self):
2016-07-23 22:21:30 +02:00
return timezone.now() > self.expiration_time
2016-07-22 22:48:09 +02:00
shotgun = property(get_shotgun)
2016-07-24 00:48:05 +02:00
def get_cancellable(self):
return timezone.now() < self.date + datetime.timedelta(hours=1)
cancellable = property(get_cancellable)
2016-07-22 22:48:09 +02:00
def __str__(self):
2016-07-23 22:21:30 +02:00
return "%s -- %s" % (self.attribution.participant.user,
self.attribution.spectacle.title)