def get_queryset(self): events = Event.for_user(self.request.user) event_id = self.request.GET.get("event") if event_id is None: return Option.objects.filter(event=events) return Option.objects.filter(event=events.get(event_id))
def order(self, request, pk=None): """Permet de créer une commande. Il faut envoyer un dictionaire dont la valeur "billets" correspondt à un tableau contenat les billest """ clean_old_orders.delay() # On récupère l'évenement en s'assurant au passage que l'utilisateur a le droit d'y accéder event = Event.for_user(request.user).get(id=pk) # On récupère le client lié à l'user client = request.user.client # On récupère la commande en cours si il en existe une, sinon on la crée order = client.orders.filter(event=event, status__lt=Order.STATUS_PAYMENT, created_at__gt=timezone.now() - timedelta(minutes=20)).first() or \ Order(event=event, client=client, status=Order.STATUS_SELECT_PRODUCT) order.save() if request.method == 'GET': return Response(OrderSerializer(order).data) # On place le status en sélection de produit order.status = order.STATUS_SELECT_PRODUCT order.save() order.billets.all().delete() # Si il y a un champ billets dans les données renvoyées if "billets" in request.data: # Pour chaque billet dans l'array de billets for billet in request.data['billets']: # Si le billet est update if "id" not in billet: billet_data = BilletSerializer(data=billet, context={"order": order.id}) if billet_data.is_valid(): billet_data.validated_data['order'] = order billet = billet_data.create(billet_data.validated_data) billet.save() ok = order.is_valid() # Si la commande ne répond pas aux règles if not ok: order.destroy_all() return Response("NIQUE TA MERE ESSAYE PAS DE GRUGER", status=400) order.status = order.STATUS_SELECT_PARTICIPANT order.save() return Response(OrderSerializer(order).data)
def options(self, request, pk=None, *args, **kwargs): event = Event.for_user(request.user).get(id=pk) client = request.user.client order = client.orders.get(event=event, status__lt=Order.STATUS_PAYMENT) transaction.atomic() for optionbillet in request.data: if "billet" in optionbillet and optionbillet['billet'].isDigit(): # Récupère le billet et renvoie une erreur si le billet n'est pas dans la commande billet = order.billets.get(id=int(optionbillet['billet'])) else: billet = order.option_billet # Récupère l'option et renvoie une erreur si l' option n'est pas liée au produit du billet option = billet.product.options.get(id=int(optionbillet['option'])) participant = None if "participant" in optionbillet and optionbillet[ 'participant'].isdigit(): participant = billet.participants.get( id=int(optionbillet['participant'])) BilletOption(billet=billet, option=option, amount=int(optionbillet['amount']), participant=participant).save() if not order.is_valid(): transaction.rollback() return Response("NIQUE TA MERE ESSAYE PAS DE GRUGER") order.status = order.STATUS_PAYMENT order.save() return Response(OrderSerializer(order).data)
def get_queryset(self): return Event.for_user(self.request.user)