views.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from django.http import HttpResponse, JsonResponse
  2. from django.views.decorators.csrf import csrf_exempt
  3. from rest_framework.parsers import JSONParser
  4. from .models import Order, OrderGood
  5. from goods.models import Good
  6. from .serializers import OrderSerializer
  7. from .permissions import OrderUpsertPermission
  8. import json
  9. from rest_framework.decorators import api_view, permission_classes,authentication_classes
  10. from rest_framework.permissions import IsAdminUser,IsAdminUser
  11. from rest_framework_simplejwt.authentication import JWTAuthentication
  12. from django.db.models import Q
  13. @csrf_exempt
  14. @api_view(['GET'])
  15. @authentication_classes([JWTAuthentication])
  16. @permission_classes((IsAdminUser, ))
  17. def order_list(request):
  18. try:
  19. skip = int(request.GET.get("skip",0))
  20. limit = int(request.GET.get("limit",50))
  21. orderBy = request.GET.get("orderBy","date")
  22. status = int(request.GET.get("status",0))
  23. except:
  24. return JsonResponse({"errors":[{"message":"Невірні параметри запиту"}]}, safe=False)
  25. text = request.GET.get("text","")
  26. try:
  27. if text:
  28. orders = Order.objects.filter(Q(name__contains = text) | Q(email__contains = text) | Q(phoneNumber__contains = text) | Q(surname__contains = text) | Q(_id__contains = text) ).order_by(orderBy)
  29. else:
  30. orders = Order.objects.all().order_by(orderBy)
  31. if status != 0:
  32. orders = orders.filter(status = status)
  33. serializer = OrderSerializer(orders[skip:skip+limit], many=True)
  34. except Exception as e:
  35. return JsonResponse({"errors":[{"message":"Замовлення не знайдено"}]}, safe=False)
  36. return JsonResponse({"data":serializer.data}, safe=False)
  37. @csrf_exempt
  38. @api_view(["GET"])
  39. @permission_classes([IsAdminUser])
  40. @authentication_classes([JWTAuthentication])
  41. def order_detail(request,_id):
  42. try:
  43. order = Order.objects.get(_id = _id)
  44. except Exception as e:
  45. return JsonResponse({"errors":[{"message":"Замовлення не знайдено"}]}, safe=False)
  46. order = Order.objects.get(_id = _id)
  47. if order:
  48. serializer = OrderSerializer(order)
  49. return JsonResponse({"data":serializer.data}, safe=False)
  50. else:
  51. return JsonResponse({"data":{}}, safe=False)
  52. @csrf_exempt
  53. @api_view(['GET'])
  54. @authentication_classes([JWTAuthentication])
  55. @permission_classes((IsAdminUser, ))
  56. def order_delete(request,_id):
  57. try:
  58. order = Order.objects.get(_id = _id)
  59. except Exception as e:
  60. return JsonResponse({"errors":[{"message":"Замовлення не знайдено"}]}, safe=False)
  61. if order:
  62. order.delete()
  63. return JsonResponse({"data":{"_id":_id}}, safe=False)
  64. return JsonResponse({"data":{}}, safe=False)
  65. @csrf_exempt
  66. @api_view(["POST"])
  67. @permission_classes([OrderUpsertPermission, ])
  68. @authentication_classes([JWTAuthentication])
  69. def order_upsert(request):
  70. orderPrice = 0
  71. try:
  72. _id = request.POST.get('_id',None)
  73. name = request.POST.get("name", "-")
  74. orderGoods= json.loads(request.POST.get("orderGoods", "[]"))
  75. email = request.POST.get("email", "")
  76. phoneNumber = request.POST.get("phoneNumber", "-")
  77. if _id:
  78. status = request.POST.get("status", "1")
  79. else:
  80. status = "1"
  81. delivery = request.POST.get("delivery", "-")
  82. address = request.POST.get("address", "-")
  83. surname = request.POST.get("surname", "-")
  84. except:
  85. return JsonResponse({"errors":[{"message":"Не вірні дані"}]}, safe=False)
  86. if len(orderGoods) == 0:
  87. return JsonResponse({"errors":[{"message":"Товари відсутні"}]}, safe=False)
  88. if _id :
  89. order = Order.objects.get(_id = _id)
  90. order.email = email
  91. order.name = name
  92. order.surname = surname
  93. order.address = address
  94. order.delivery = delivery
  95. order.phoneNumber = phoneNumber
  96. else:
  97. order = Order.objects.create(email=email,phoneNumber=phoneNumber,status=status,address=address,delivery=delivery,name = name,surname =surname)
  98. for orderGood in order.orderGoods.all():
  99. if order.status != 4:
  100. orderGood.good.amount += orderGood.count
  101. orderGood.good.save()
  102. order.orderGoods.clear()
  103. for orderGood in orderGoods:
  104. try:
  105. good = Good.objects.get(_id = orderGood.get("good")["_id"])
  106. except:
  107. return JsonResponse({"errors":[{"message":"Товар не знайдено"}]}, safe=False)
  108. count = orderGood.get("count")
  109. if int(good.amount) - int(count) >= 0:
  110. price = int(count) * int(good.price)
  111. orderGoodToSave = OrderGood.objects.create(price = price, count = count, good = good)
  112. orderGoodToSave.save()
  113. order.orderGoods.add(orderGoodToSave)
  114. orderPrice+=int(price)
  115. if int(status) != 4:
  116. good.amount = int(good.amount) - int(count)
  117. good.save()
  118. else:
  119. return JsonResponse({"errors":[{"message":f"{good.name}:Недостатньо товару!"}]}, safe=False)
  120. order.price = orderPrice
  121. order.status = status
  122. if order:
  123. order.save()
  124. serializer = OrderSerializer(order)
  125. return JsonResponse({"data":serializer.data}, safe=False)
  126. else:
  127. return JsonResponse({"data":{}}, safe=False)