3
0
Fork 0
mirror of https://github.com/nanoy42/coope synced 2024-11-22 03:13:12 +00:00

Empecher l'utilisateur de consommer s'il a gardé trop de pintes

This commit is contained in:
Yoann Pétri 2018-12-23 18:52:18 +01:00
parent e28f564afa
commit e5456b4d21
7 changed files with 82 additions and 10 deletions

View file

@ -0,0 +1,25 @@
# Generated by Django 2.1 on 2018-12-23 17:30
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('gestion', '0003_historicalpinte'),
]
operations = [
migrations.AlterField(
model_name='pinte',
name='current_owner',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='pintes_owned_currently', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='pinte',
name='previous_owner',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='pintes_owned_previously', to=settings.AUTH_USER_MODEL),
),
]

View file

@ -230,7 +230,7 @@ class Pinte(models.Model):
"""
Stores a physical pinte
"""
current_owner = models.ForeignKey(User, on_delete=models.PROTECT, null=True, default=None, related_name="pinte_owned_currently")
previous_owner = models.ForeignKey(User, on_delete=models.PROTECT, null=True, default=None, related_name="pinte_owned_previously")
current_owner = models.ForeignKey(User, on_delete=models.PROTECT, null=True, default=None, related_name="pintes_owned_currently")
previous_owner = models.ForeignKey(User, on_delete=models.PROTECT, null=True, default=None, related_name="pintes_owned_previously")
last_update_date = models.DateTimeField(auto_now=True)
history = HistoricalRecords()

View file

@ -15,7 +15,7 @@ from decimal import *
from .forms import ReloadForm, RefundForm, ProductForm, KegForm, MenuForm, GestionForm, SearchMenuForm, SearchProductForm, SelectPositiveKegForm, SelectActiveKegForm, PinteForm
from .models import Product, Menu, Keg, ConsumptionHistory, KegHistory, Consumption, MenuHistory, Pinte
from preferences.models import PaymentMethod
from preferences.models import PaymentMethod, GeneralPreferences
@active_required
@login_required
@ -108,6 +108,7 @@ def order(request):
order = json.loads(request.POST["order"])
menus = json.loads(request.POST["menus"])
listPintes = json.loads(request.POST["listPintes"])
gp,_ = GeneralPreferences.objects.get_or_create(pk=1)
if (not order) and (not menus):
return HttpResponse("Pas de commande")
adherentRequired = False
@ -119,14 +120,20 @@ def order(request):
adherentRequired = adherentRequired or menu.adherent_required
if(adherentRequired and not user.profile.is_adherent):
return HttpResponse("N'est pas adhérent et devrait l'être")
# Partie un peu complexe : je libère toutes les pintes de la commande, puis je test
# s'il a trop de pintes non rendues, puis je réalloue les pintes
for pinte in listPintes:
allocate(pinte, None)
if(gp.lost_pintes_allowed and user.profile.nb_pintes >= gp.lost_pintes_allowed):
return HttpResponse("Impossible de réaliser la commande : l'utilisateur a perdu trop de pintes.")
for pinte in listPintes:
allocate(pinte, user)
if(paymentMethod.affect_balance):
if(user.profile.balance < amount):
return HttpResponse("Solde inférieur au prix de la commande")
else:
user.profile.debit += amount
user.save()
for pinte in listPintes:
allocate(pinte, user)
for o in order:
product = get_object_or_404(Product, pk=o["pk"])
quantity = int(o["quantity"])

View file

@ -0,0 +1,23 @@
# Generated by Django 2.1 on 2018-12-23 13:40
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('preferences', '0002_auto_20181221_2151'),
]
operations = [
migrations.AddField(
model_name='generalpreferences',
name='lost_pintes_allowed',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='historicalgeneralpreferences',
name='lost_pintes_allowed',
field=models.PositiveIntegerField(default=0),
),
]

View file

@ -31,6 +31,7 @@ class GeneralPreferences(models.Model):
brewer = models.CharField(max_length=255, blank=True)
grocer = models.CharField(max_length=255, blank=True)
use_pinte_monitoring = models.BooleanField(default=False)
lost_pintes_allowed = models.PositiveIntegerField(default=0)
history = HistoricalRecords()
class Cotisation(models.Model):

View file

@ -5,7 +5,7 @@
<li><a href="#first">Message global</a></li>
<li><a href="#second">Site actif</a></li>
<li><a href="#third">Bureau</a></li>
<li><a href="#fourth">Autre</a></li>
<li><a href="#fourth">Suivi de pintes</a></li>
</ul>
{% endblock %}
@ -83,12 +83,18 @@
<div class="spotlight">
<div class="content">
<header class="major">
<h2>Autre</h2>
<h2>Suivi de pintes</h2>
</header>
<div class="row uniform">
<div class="12u">
{{form.use_pinte_monitoring}}
<label for="{{form.is_active.id_for_label}}">Utiliser le suivi de pinte ?</label>
<label for="{{form.use_pinte_monitoring.id_for_label}}">Utiliser le suivi de pinte ?</label>
</div>
</div>
<div class="row uniform">
<div class="12u">
{{form.lost_pintes_allowed}}
<label for="{{form.lost_pintes_allowed.id_for_label}}">Nombre de pintes non rendues avant d'interdire la consommation (0 pour ne jamais l'interdire, nécessite le suivi de pinte actif)</label>
</div>
</div>
<div class="row uniform">

View file

@ -119,6 +119,13 @@ class Profile(models.Model):
alcohol += consumption.quantity * float(product.deg) * product.volume * 0.79 /10 /1000
return alcohol
@property
def nb_pintes(self):
"""
Return the number of pintes currently owned
"""
return self.user.pintes_owned_currently.count()
def __str__(self):
return str(self.user)
@ -128,9 +135,12 @@ class Profile(models.Model):
tente de retourner l'attribut de l'user associé à l'instance
"""
try:
r = super().__getattr__(name)
r = self.__getattribute__(name)
except AttributeError:
r = getattr(self.user, name)
try:
r = super().__getattr__(name)
except AttributeError:
r = getattr(self.user, name)
return r