From 1ddbec4b055dad96ee6c716bd378cbfbd20a10e4 Mon Sep 17 00:00:00 2001 From: Jonas 12t Date: Sun, 24 Oct 2021 16:53:03 +0400 Subject: [PATCH] refactor qrcode pour product & price --- DjangoFiles/Administration/admin_tenant.py | 1 - DjangoFiles/BaseBillet/models.py | 61 ++++++++++--------- DjangoFiles/PaiementStripe/views.py | 4 +- .../templates/html5up-dimension/index.html | 2 +- DjangoFiles/QrcodeCashless/views.py | 61 +++++++++++-------- 5 files changed, 69 insertions(+), 60 deletions(-) diff --git a/DjangoFiles/Administration/admin_tenant.py b/DjangoFiles/Administration/admin_tenant.py index 4fd8fb8..722396b 100644 --- a/DjangoFiles/Administration/admin_tenant.py +++ b/DjangoFiles/Administration/admin_tenant.py @@ -120,7 +120,6 @@ class ConfigurationAdmin(SingletonModelAdmin): ('Adhésions', { 'fields': ( 'adhesion_obligatoire', - 'cadeau_adhesion', ), }), ('Paiements', { diff --git a/DjangoFiles/BaseBillet/models.py b/DjangoFiles/BaseBillet/models.py index f65f785..f7d1868 100644 --- a/DjangoFiles/BaseBillet/models.py +++ b/DjangoFiles/BaseBillet/models.py @@ -157,7 +157,6 @@ class Product(models.Model): def __str__(self): return f"{self.name}" - ''' def get_id_product_stripe(self): configuration = Configuration.get_solo() if configuration.stripe_api_key and not self.id_product_stripe: @@ -180,6 +179,7 @@ class Product(models.Model): ) self.id_product_stripe = product.id self.save() + return self.id_product_stripe elif self.id_product_stripe: @@ -187,35 +187,9 @@ class Product(models.Model): else: return False - def get_id_price_stripe(self): - configuration = Configuration.get_solo() - if configuration.stripe_api_key and not self.id_price_stripe: - if configuration.stripe_mode_test: - stripe.api_key = configuration.stripe_test_api_key - else: - stripe.api_key = configuration.stripe_api_key - - price = stripe.Price.create( - unit_amount=int("{0:.2f}".format(self.prix).replace('.', '')), - currency="eur", - product=self.get_id_product_stripe(), - ) - - self.id_price_stripe = price.id - self.save() - return self.id_price_stripe - - elif self.id_price_stripe: - return self.id_price_stripe - else: - return False - def reset_id_stripe(self): - self.id_price_stripe = None self.id_product_stripe = None self.save() - ''' - class Price(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True, db_index=True) @@ -247,7 +221,35 @@ class Price(models.Model): return range(self.max_per_user + 1) def __str__(self): - return f"{self.name}" + return f"{self.product.name} {self.name}" + + + def get_id_price_stripe(self): + configuration = Configuration.get_solo() + if configuration.stripe_api_key and not self.id_price_stripe: + if configuration.stripe_mode_test: + stripe.api_key = configuration.stripe_test_api_key + else: + stripe.api_key = configuration.stripe_api_key + + price = stripe.Price.create( + unit_amount=int("{0:.2f}".format(self.prix).replace('.', '')), + currency="eur", + product=self.product.get_id_product_stripe(), + ) + + self.id_price_stripe = price.id + self.save() + return self.id_price_stripe + + elif self.id_price_stripe: + return self.id_price_stripe + else: + return False + + def reset_id_stripe(self): + self.id_price_stripe = None + self.save() class Event(models.Model): @@ -366,7 +368,8 @@ class LigneArticle(models.Model): uuid = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4) datetime = models.DateTimeField(auto_now=True) - product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True) + price = models.ForeignKey(Price, on_delete=models.CASCADE) + qty = models.SmallIntegerField() reservation = models.ForeignKey(Reservation, on_delete=models.CASCADE, blank=True, null=True) diff --git a/DjangoFiles/PaiementStripe/views.py b/DjangoFiles/PaiementStripe/views.py index ecf77fd..5988583 100644 --- a/DjangoFiles/PaiementStripe/views.py +++ b/DjangoFiles/PaiementStripe/views.py @@ -64,7 +64,7 @@ class creation_paiement_stripe(): def _total(self): total = 0 for ligne in self.liste_ligne_article: - total += float(ligne.qty) * float(ligne.product.prix) + total += float(ligne.qty) * float(ligne.price.prix) return total def _paiement_stripe_db(self): @@ -96,7 +96,7 @@ class creation_paiement_stripe(): ligne: LigneArticle line_items.append( { - "price": f"{ligne.product.get_id_price_stripe()}", + "price": f"{ligne.price.get_id_price_stripe()}", "quantity": int(ligne.qty), } ) diff --git a/DjangoFiles/QrcodeCashless/templates/html5up-dimension/index.html b/DjangoFiles/QrcodeCashless/templates/html5up-dimension/index.html index 7a332a7..fb0b65a 100644 --- a/DjangoFiles/QrcodeCashless/templates/html5up-dimension/index.html +++ b/DjangoFiles/QrcodeCashless/templates/html5up-dimension/index.html @@ -264,7 +264,7 @@ {% for tarif in tarifs_adhesion %} {% endfor %} diff --git a/DjangoFiles/QrcodeCashless/views.py b/DjangoFiles/QrcodeCashless/views.py index 3758fe3..a389dfc 100644 --- a/DjangoFiles/QrcodeCashless/views.py +++ b/DjangoFiles/QrcodeCashless/views.py @@ -10,7 +10,7 @@ from rest_framework.generics import get_object_or_404 from django.views import View from rest_framework import status -from BaseBillet.models import Configuration, Product, LigneArticle +from BaseBillet.models import Configuration, Product, LigneArticle, Price from PaiementStripe.models import Paiement_stripe from PaiementStripe.views import creation_paiement_stripe from QrcodeCashless.models import CarteCashless @@ -19,6 +19,7 @@ from django.db.models.signals import pre_save from django.dispatch import receiver import logging + logger = logging.getLogger(__name__) @@ -114,7 +115,7 @@ class index_scan(View): request, self.template_name, { - 'tarifs_adhesion': Product.objects.filter(categorie_article=Product.ADHESION).order_by('-prix'), + 'tarifs_adhesion': Price.objects.filter(product__categorie_article=Product.ADHESION), 'adhesion_obligatoire': configuration.adhesion_obligatoire, 'history': json_reponse.get('history'), 'carte_resto': configuration.carte_restaurant, @@ -143,7 +144,7 @@ class index_scan(View): return HttpResponse('Forbidden', status=status.HTTP_403_FORBIDDEN) else: return HttpResponse("Serveur non disponible. Merci de revenir ultérieurement.", - status=status.HTTP_503_SERVICE_UNAVAILABLE) + status=status.HTTP_503_SERVICE_UNAVAILABLE) def post(self, request, uuid): carte = check_carte_local(uuid) @@ -157,37 +158,43 @@ class index_scan(View): montant_recharge = data.get('montant_recharge') # c'est un paiement - if ( pk_adhesion or montant_recharge ) and data.get('email'): + if (pk_adhesion or montant_recharge) and data.get('email'): # montant_recharge = data.get('montant_recharge') ligne_articles = [] metadata = {} metadata['recharge_carte_uuid'] = str(carte.uuid) + if montant_recharge: - art, created = Product.objects.get_or_create( - name="Recharge Carte", - prix=1, + product, created = Product.objects.get_or_create( + name=f"Recharge Carte {carte.detail.origine.name} v{carte.detail.generation}", categorie_article=Product.RECHARGE_CASHLESS, + img=carte.detail.img, + ) + + price, created = Price.objects.get_or_create( + product=product, + name=f"{montant_recharge}€", + prix=int(montant_recharge), ) ligne_article_recharge = LigneArticle.objects.create( - article=art, - qty=montant_recharge, - carte=carte, - ) - ligne_articles.append(ligne_article_recharge) - - - metadata['recharge_carte_montant'] = str(montant_recharge) - - if pk_adhesion: - art_adhesion = Product.objects.get(pk=data.get('pk_adhesion')) - ligne_article_recharge = LigneArticle.objects.create( - article=art_adhesion, + price=price, qty=1, carte=carte, ) ligne_articles.append(ligne_article_recharge) - metadata['pk_adhesion'] = str(art_adhesion.pk) + + metadata['recharge_carte_montant'] = str(montant_recharge) + + if pk_adhesion: + price_adhesion = Price.objects.get(pk=data.get('pk_adhesion')) + ligne_article_recharge = LigneArticle.objects.create( + price=price_adhesion, + qty=1, + carte=carte, + ) + ligne_articles.append(ligne_article_recharge) + metadata['pk_adhesion'] = str(price_adhesion.pk) if len(ligne_articles) > 0: new_paiement_stripe = creation_paiement_stripe( @@ -262,17 +269,17 @@ def changement_paid_to_valid(sender, instance: Paiement_stripe, update_fields=No data_pour_serveur_cashless = {'uuid_commande': paiementStripe.uuid} for ligne_article in paiementStripe.lignearticle_set.all(): - if ligne_article.carte : + if ligne_article.carte: data_pour_serveur_cashless['uuid'] = ligne_article.carte.uuid - if ligne_article.product.categorie_article == Product.RECHARGE_CASHLESS : - data_pour_serveur_cashless['recharge_qty'] = float(ligne_article.qty) + if ligne_article.price.product.categorie_article == Product.RECHARGE_CASHLESS: + data_pour_serveur_cashless['recharge_qty'] = ligne_article.price.prix - if ligne_article.product.categorie_article == Product.ADHESION : - data_pour_serveur_cashless['tarif_adhesion'] = ligne_article.product.prix + if ligne_article.price.product.categorie_article == Product.ADHESION: + data_pour_serveur_cashless['tarif_adhesion'] = ligne_article.price.prix # si il y a autre chose que uuid_commande : - if len(data_pour_serveur_cashless) > 1 : + if len(data_pour_serveur_cashless) > 1: sess = requests.Session() configuration = Configuration.get_solo() r = sess.post(