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
|
2016-05-26 22:44:10 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
import calendar
|
2016-09-04 11:14:09 +02:00
|
|
|
import random
|
2016-09-21 15:39:01 +02:00
|
|
|
from datetime import timedelta
|
2014-08-19 12:54:22 +02:00
|
|
|
|
2012-07-11 17:39:20 +02:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import User
|
2016-06-10 23:53:29 +02:00
|
|
|
from django.template import loader, Context
|
|
|
|
from django.core import mail
|
|
|
|
from django.conf import settings
|
2016-07-10 14:19:19 +02:00
|
|
|
from django.utils import timezone
|
2016-05-26 22:44:10 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2016-06-10 23:53:29 +02:00
|
|
|
|
2016-07-10 13:19:10 +02:00
|
|
|
|
2016-06-10 23:53:29 +02:00
|
|
|
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
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-06-04 17:57:06 +02:00
|
|
|
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")
|
2016-07-08 00:18:58 +02:00
|
|
|
tokens = models.TextField("Graine(s) du tirage", blank=True)
|
2016-06-07 23:07:28 +02:00
|
|
|
active = models.BooleanField("Tirage actif", default=False)
|
2016-07-08 00:18:58 +02:00
|
|
|
enable_do_tirage = models.BooleanField("Le tirage peut être lancé",
|
2016-08-26 05:28:04 +02:00
|
|
|
default=False)
|
2016-06-04 17:57:06 +02:00
|
|
|
|
|
|
|
def date_no_seconds(self):
|
2016-09-10 19:15:43 +02:00
|
|
|
return self.fermeture.astimezone(timezone.get_current_timezone()) \
|
|
|
|
.strftime('%d %b %Y %H:%M')
|
2016-06-04 17:57:06 +02:00
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "%s - %s" % (self.title, self.date_no_seconds())
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-06-07 22:36:43 +02:00
|
|
|
class Salle(models.Model):
|
2016-07-09 21:19:37 +02:00
|
|
|
name = models.CharField("Nom", max_length=300)
|
2016-06-07 22:36:43 +02:00
|
|
|
address = models.TextField("Adresse")
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
def __str__(self):
|
2013-09-05 22:20:52 +02:00
|
|
|
return self.name
|
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-08-26 05:28:04 +02:00
|
|
|
@python_2_unicode_compatible
|
|
|
|
class CategorieSpectacle(models.Model):
|
2016-08-26 06:06:45 +02:00
|
|
|
name = models.CharField('Nom', max_length=100, unique=True)
|
2016-08-26 05:28:04 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = "Catégorie"
|
|
|
|
|
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-06-07 22:36:43 +02:00
|
|
|
class Spectacle(models.Model):
|
|
|
|
title = models.CharField("Titre", max_length=300)
|
2016-08-26 05:28:04 +02:00
|
|
|
category = models.ForeignKey(CategorieSpectacle, blank=True, null=True)
|
2016-06-07 22:36:43 +02:00
|
|
|
date = models.DateTimeField("Date & heure")
|
2013-09-05 22:20:52 +02:00
|
|
|
location = models.ForeignKey(Salle)
|
2016-08-26 05:28:04 +02:00
|
|
|
vips = models.TextField('Personnalités', blank=True)
|
2016-06-07 22:36:43 +02:00
|
|
|
description = models.TextField("Description", blank=True)
|
|
|
|
slots_description = models.TextField("Description des places", blank=True)
|
2016-08-26 05:28:04 +02:00
|
|
|
image = models.ImageField('Image', blank=True, null=True,
|
|
|
|
upload_to='imgs/shows/')
|
|
|
|
ext_link = models.CharField('Lien vers le site du spectacle', blank=True,
|
|
|
|
max_length=500)
|
2016-06-07 22:34:23 +02:00
|
|
|
price = models.FloatField("Prix d'une place")
|
2016-06-07 22:36:43 +02:00
|
|
|
slots = models.IntegerField("Places")
|
|
|
|
tirage = models.ForeignKey(Tirage)
|
2016-06-10 02:00:50 +02:00
|
|
|
listing = models.BooleanField("Les places sont sur listing")
|
2016-07-10 14:19:19 +02:00
|
|
|
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"
|
2016-08-26 05:28:04 +02:00
|
|
|
ordering = ("date", "title",)
|
2012-07-11 17:39:20 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
def __repr__(self):
|
2016-05-26 22:44:10 +02:00
|
|
|
return "[%s]" % self
|
2012-07-11 17:39:20 +02:00
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
def timestamp(self):
|
|
|
|
return "%d" % calendar.timegm(self.date.utctimetuple())
|
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
def date_no_seconds(self):
|
2016-09-10 19:15:43 +02:00
|
|
|
return self.date.astimezone(timezone.get_current_timezone()) \
|
|
|
|
.strftime('%d %b %Y %H:%M')
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "%s - %s, %s, %.02f€" % (self.title, self.date_no_seconds(),
|
|
|
|
self.location, self.price)
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-06-10 23:53:29 +02:00
|
|
|
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})
|
2016-07-10 13:19:10 +02:00
|
|
|
mail_tot = mail.EmailMessage(
|
|
|
|
mail_object, mail_body,
|
2016-06-10 23:53:29 +02:00
|
|
|
settings.RAPPEL_FROM, [member.email],
|
|
|
|
[], headers={'Reply-To': settings.RAPPEL_REPLY_TO})
|
|
|
|
mails_to_send.append(mail_tot)
|
|
|
|
# On envoie les mails
|
2016-06-10 23:55:57 +02:00
|
|
|
connection = mail.get_connection()
|
2016-06-10 23:53:29 +02:00
|
|
|
connection.send_messages(mails_to_send)
|
2016-07-10 14:19:19 +02:00
|
|
|
# On enregistre le fait que l'envoi a bien eu lieu
|
|
|
|
self.rappel_sent = timezone.now()
|
|
|
|
self.save()
|
2016-06-10 23:53:29 +02:00
|
|
|
# On renvoie la liste des destinataires
|
|
|
|
return members.values()
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-08-26 05:28:04 +02:00
|
|
|
|
|
|
|
class Quote(models.Model):
|
|
|
|
spectacle = models.ForeignKey(Spectacle)
|
|
|
|
text = models.TextField('Citation')
|
|
|
|
author = models.CharField('Auteur', max_length=200)
|
|
|
|
|
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
PAYMENT_TYPES = (
|
2016-07-10 10:46:45 +02:00
|
|
|
("cash", "Cash"),
|
2016-07-09 21:19:37 +02:00
|
|
|
("cb", "CB"),
|
2016-07-10 10:46:45 +02:00
|
|
|
("cheque", "Chèque"),
|
|
|
|
("autre", "Autre"),
|
2013-09-05 22:20:52 +02:00
|
|
|
)
|
2012-07-11 17:39:20 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-07-09 12:52:53 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-06-07 22:36:43 +02:00
|
|
|
class Participant(models.Model):
|
2016-06-04 17:57:06 +02:00
|
|
|
user = models.ForeignKey(User)
|
|
|
|
choices = models.ManyToManyField(Spectacle,
|
2016-07-09 22:31:56 +02:00
|
|
|
through="ChoixSpectacle",
|
|
|
|
related_name="chosen_by")
|
2016-06-04 17:57:06 +02:00
|
|
|
attributions = models.ManyToManyField(Spectacle,
|
2016-07-09 22:31:56 +02:00
|
|
|
through="Attribution",
|
|
|
|
related_name="attributed_to")
|
2016-07-09 12:52:53 +02:00
|
|
|
paid = models.BooleanField("A payé", default=False)
|
|
|
|
paymenttype = models.CharField("Moyen de paiement",
|
2016-07-09 22:31:56 +02:00
|
|
|
max_length=6, choices=PAYMENT_TYPES,
|
|
|
|
blank=True)
|
2016-06-04 17:57:06 +02:00
|
|
|
tirage = models.ForeignKey(Tirage)
|
2016-07-25 02:51:19 +02:00
|
|
|
choicesrevente = models.ManyToManyField(Spectacle,
|
2016-09-03 05:21:27 +02:00
|
|
|
related_name="revente",
|
|
|
|
blank=True)
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-07-09 12:52:53 +02:00
|
|
|
def __str__(self):
|
2016-07-15 20:01:45 +02:00
|
|
|
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-09 22:31:56 +02:00
|
|
|
|
2016-07-15 00:02:56 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-06-07 22:36:43 +02:00
|
|
|
class ChoixSpectacle(models.Model):
|
2012-07-11 17:39:20 +02:00
|
|
|
participant = models.ForeignKey(Participant)
|
2016-06-04 17:57:06 +02:00
|
|
|
spectacle = models.ForeignKey(Spectacle, related_name="participants")
|
2012-07-11 17:39:20 +02:00
|
|
|
priority = models.PositiveIntegerField("Priorité")
|
2016-06-04 17:57:06 +02:00
|
|
|
double_choice = models.CharField("Nombre de places",
|
2016-07-09 22:31:56 +02:00
|
|
|
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)
|
|
|
|
|
2016-07-15 20:01:45 +02:00
|
|
|
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",)
|
2013-09-05 22:20:52 +02:00
|
|
|
unique_together = (("participant", "spectacle",),)
|
2012-07-11 17:39:20 +02:00
|
|
|
verbose_name = "voeu"
|
2013-09-05 22:20:52 +02:00
|
|
|
verbose_name_plural = "voeux"
|
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-07-10 04:49:24 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-06-07 22:36:43 +02:00
|
|
|
class Attribution(models.Model):
|
2013-09-05 22:20:52 +02:00
|
|
|
participant = models.ForeignKey(Participant)
|
2016-06-04 17:57:06 +02:00
|
|
|
spectacle = models.ForeignKey(Spectacle, related_name="attribues")
|
2016-05-26 22:44:10 +02:00
|
|
|
given = models.BooleanField("Donnée", default=False)
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-05-26 22:44:10 +02:00
|
|
|
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-09-05 02:29:49 +02:00
|
|
|
seller = models.ForeignKey(Participant,
|
2016-09-05 03:10:06 +02:00
|
|
|
related_name="original_shows",
|
|
|
|
verbose_name="Vendeur")
|
|
|
|
soldTo = models.ForeignKey(Participant, blank=True, null=True,
|
|
|
|
verbose_name="Vendue à")
|
2016-07-22 22:48:09 +02:00
|
|
|
|
2016-09-21 15:39:01 +02:00
|
|
|
notif_sent = models.BooleanField("Notification envoyée",
|
|
|
|
default=False)
|
|
|
|
tirage_done = models.BooleanField("Tirage effectué",
|
|
|
|
default=False)
|
|
|
|
|
2016-07-22 22:48:09 +02:00
|
|
|
def get_expiration_time(self):
|
2016-09-21 15:39:01 +02:00
|
|
|
# L'acheteur doit être connu au plus 12h avant le spectacle
|
|
|
|
remaining_time = (self.attribution.spectacle.date
|
|
|
|
- self.date - timedelta(hours=13))
|
|
|
|
# Au minimum, on attend 2 jours avant le tirage
|
|
|
|
delay = min(remaining_time, timedelta(days=2))
|
|
|
|
# On a aussi 1h pour changer d'avis
|
|
|
|
return self.date + delay + timedelta(hours=1)
|
2016-07-22 22:48:09 +02:00
|
|
|
expiration_time = property(get_expiration_time)
|
|
|
|
|
|
|
|
def get_shotgun(self):
|
2016-09-21 15:39:01 +02:00
|
|
|
# Soit on a dépassé le délai du tirage, soit il reste peu de
|
|
|
|
# temps avant le spectacle
|
|
|
|
# On se laisse 5min de marge pour cron
|
|
|
|
return (timezone.now() > self.expiration_time + timedelta(minutes=5) or
|
|
|
|
(self.attribution.spectacle.date <= timezone.now() +
|
|
|
|
timedelta(days=1))) and (timezone.now() >= self.date +
|
|
|
|
timedelta(minutes=15))
|
2016-07-22 22:48:09 +02:00
|
|
|
shotgun = property(get_shotgun)
|
|
|
|
|
|
|
|
def __str__(self):
|
2016-09-05 02:29:49 +02:00
|
|
|
return "%s -- %s" % (self.seller,
|
2016-07-23 22:21:30 +02:00
|
|
|
self.attribution.spectacle.title)
|
2016-07-29 01:50:08 +02:00
|
|
|
|
2016-09-05 03:10:06 +02:00
|
|
|
class Meta:
|
|
|
|
verbose_name = "Revente"
|
|
|
|
|
2016-07-29 01:50:08 +02:00
|
|
|
def send_notif(self):
|
|
|
|
inscrits = self.attribution.spectacle.revente.select_related('user')
|
|
|
|
|
|
|
|
mails_to_send = []
|
|
|
|
mail_object = "%s" % (self.attribution.spectacle)
|
|
|
|
for participant in inscrits:
|
|
|
|
mail_body = render_template('mail-revente.txt', {
|
|
|
|
'user': participant.user,
|
|
|
|
'spectacle': self.spectacle,
|
|
|
|
'revente': self})
|
|
|
|
mail_tot = mail.EmailMessage(
|
|
|
|
mail_object, mail_body,
|
2016-09-21 15:39:01 +02:00
|
|
|
settings.REVENTE_FROM, [participant.user.email],
|
|
|
|
[], headers={'Reply-To': settings.REVENTE_REPLY_TO})
|
|
|
|
mails_to_send.append(mail_tot)
|
|
|
|
|
|
|
|
connection = mail.get_connection()
|
|
|
|
connection.send_messages(mails_to_send)
|
|
|
|
self.notif_sent = True
|
|
|
|
|
|
|
|
def mail_shotgun(self):
|
|
|
|
inscrits = self.attribution.spectacle.revente.select_related('user')
|
|
|
|
|
|
|
|
mails_to_send = []
|
|
|
|
mail_object = "%s" % (self.attribution.spectacle)
|
|
|
|
for participant in inscrits:
|
|
|
|
mail_body = render_template('mail-shotgun.txt', {
|
|
|
|
'user': participant.user,
|
|
|
|
'spectacle': self.spectacle,
|
|
|
|
'mail': self.attribution.participant.user.email})
|
|
|
|
mail_tot = mail.EmailMessage(
|
|
|
|
mail_object, mail_body,
|
|
|
|
settings.REVENTE_FROM, [participant.user.email],
|
2016-07-29 01:50:08 +02:00
|
|
|
[], headers={'Reply-To': settings.REVENTE_REPLY_TO})
|
|
|
|
mails_to_send.append(mail_tot)
|
|
|
|
|
|
|
|
connection = mail.get_connection()
|
|
|
|
connection.send_messages(mails_to_send)
|
2016-09-21 15:39:01 +02:00
|
|
|
self.notif_sent = True
|
2016-09-04 11:14:09 +02:00
|
|
|
|
|
|
|
def tirage(self):
|
|
|
|
inscrits = self.interested
|
|
|
|
spectacle = self.attribution.spectacle
|
2016-09-05 02:29:49 +02:00
|
|
|
seller = self.seller
|
2016-09-04 11:14:09 +02:00
|
|
|
if inscrits.exists():
|
2016-09-19 16:08:12 +02:00
|
|
|
winner = random.choice(inscrits.all())
|
2016-09-04 11:14:09 +02:00
|
|
|
self.soldTo = winner
|
|
|
|
mail_buyer = """Bonjour,
|
|
|
|
|
|
|
|
Tu as été tiré-e au sort pour racheter une place pour %s le %s (%s) à %0.02f€.
|
|
|
|
Tu peux contacter le vendeur à l'adresse %s.
|
|
|
|
|
|
|
|
Chaleureusement,
|
|
|
|
Le BdA""" % (spectacle.title, spectacle.date_no_seconds(),
|
2016-09-05 02:29:49 +02:00
|
|
|
spectacle.location, spectacle.price, seller.email)
|
2016-09-04 11:14:09 +02:00
|
|
|
|
|
|
|
mail.send_mail("BdA-Revente : %s" % spectacle.title,
|
|
|
|
mail_buyer, "bda@ens.fr", [winner.user.email],
|
|
|
|
fail_silently=False)
|
|
|
|
mail_seller = """Bonjour,
|
|
|
|
La personne tirée au sort pour racheter ta place pour %s est %s.
|
|
|
|
Tu peux le/la contacter à l'adresse %s.
|
|
|
|
|
|
|
|
Chaleureusement,
|
|
|
|
Le BdA""" % (spectacle.title, winner.user.get_full_name(), winner.user.email)
|
|
|
|
|
|
|
|
mail.send_mail("BdA-Revente : %s" % spectacle.title,
|
2016-09-05 02:29:49 +02:00
|
|
|
mail_seller, "bda@ens.fr", [seller.email],
|
2016-09-04 11:14:09 +02:00
|
|
|
fail_silently=False)
|
2016-09-21 15:39:01 +02:00
|
|
|
self.tirage_done = True
|