Example #1
0
 def update(self, prod, response, id):
     #En fonction de la condition on met à jour la valeur du sale et du discount depuis la table myManageSale.productsale
     #retourne le produit json avec les valeurs discount et sale de la bdd
     if prod.quantity < 16:
         ProductSale.objects.filter(tigId=id).update(sale=False,
                                                     discount=0.0)
         new_prod_in_manage_sale = ProductSale.objects.get(tigId=id)
         serializer = ProductSaleSerializer(new_prod_in_manage_sale)
         response['sale'] = serializer.data['sale']
         response['discount'] = serializer.data['discount']
         return response
     elif 16 <= prod.quantity <= 64:
         new_price = round(response['price'] * 0.8, 2)
         ProductSale.objects.filter(tigId=id).update(sale=True,
                                                     discount=new_price)
         new_prod_in_manage_sale = ProductSale.objects.get(tigId=id)
         serializer = ProductSaleSerializer(new_prod_in_manage_sale)
         response['sale'] = serializer.data['sale']
         response['discount'] = serializer.data['discount']
         return response
     else:
         new_price = round(response['price'] * 0.5, 2)
         ProductSale.objects.filter(tigId=id).update(sale=True,
                                                     discount=new_price)
         new_prod_in_manage_sale = ProductSale.objects.get(tigId=id)
         serializer = ProductSaleSerializer(new_prod_in_manage_sale)
         response['sale'] = serializer.data['sale']
         response['discount'] = serializer.data['discount']
         return response
Example #2
0
 def handle(self, *args, **options):
     self.stdout.write('['+time.ctime()+'] Refreshing data...')
     response = requests.get(baseUrl+'products/')
     jsondata = response.json()
     ProductSale.objects.all().delete()
     for product in jsondata:
             serializer = ProductSaleSerializer(data={'tigID':str(product['id'])})
             if serializer.is_valid():
                 serializer.save()
                 self.stdout.write(self.style.SUCCESS('['+time.ctime()+'] Successfully added product id="%s"' % product['id']))
     self.stdout.write('['+time.ctime()+'] Data refresh terminated.')
Example #3
0
 def get(self, request, id, newprice, format=None):
     #si le produit existe déja dans la table, on met à jour le produit avec le nouveau prix (newprice) et le sale = Vrai (True)
     #sinon on créer une nouvelle ligne dans la table avec le discount = nouveau prix (newprice) et le sale = Vrai (True)
     if ProductSale.objects.filter(tigId=id):
         ProductSale.objects.filter(tigId=id).update(discount=newprice,
                                                     sale=True)
         response = requests.get(baseUrl + 'product/' + str(id) + '/')
         product = response.json()
         prod = ProductSale.objects.get(tigId=id)
         serializer = ProductSaleSerializer(prod)
         product['sale'] = serializer.data['sale']
         product['discount'] = serializer.data['discount']
         return Response(product)
     else:
         response = requests.get(baseUrl + 'product/' + str(id) + '/')
         product = response.json()
         serializer = ProductSaleSerializer(
             data={
                 'tigId': str(product['id']),
                 'discount': str(newprice),
                 'sale': str(True)
             })
         if serializer.is_valid():
             serializer.save()
         product['discount'] = serializer.data['discount']
         product['sale'] = serializer.data['sale']
         return Response(product)
Example #4
0
 def get(self,request,id,format=None):
     #si le produit existe le discount = 0 et le sale = Faux (false
     prod = self.get_object(id)
     if prod:
         self.remove(id)
         response = requests.get(baseUrl+'product/'+str(id)+'/')
         product = response.json()
         serializer = ProductSaleSerializer(prod)
         product['discount'] = serializer.data['discount']
         product['sale'] = serializer.data['sale']
         return Response(product)
     else:
         return Response('id not found',status=404)