views.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Good, Image
  5. from .serializers import GoodSerializer, ImageSerializer
  6. from django.db.models import Count
  7. import json
  8. from rest_framework.decorators import api_view, permission_classes,authentication_classes
  9. from rest_framework.permissions import IsAdminUser,IsAdminUser
  10. from rest_framework_simplejwt.authentication import JWTAuthentication
  11. from django.db.models import Q
  12. @csrf_exempt
  13. @api_view(['GET'])
  14. def good_list(request,):
  15. skip = int(request.GET.get("skip",0))
  16. limit = int(request.GET.get("limit",50))
  17. popular = int(request.GET.get("pupular",0))
  18. text = request.GET.get("text","")
  19. orderBy = request.GET.get("orderBy","_id")
  20. try:
  21. if text:
  22. goods = Good.objects.filter(Q(name__contains = text) | Q(description__contains = text) ).order_by(orderBy)[skip:skip+limit]
  23. elif popular:
  24. goods = Good.objects.annotate(order_count=Count('orderGoods')).order_by("order_count")[skip:skip+limit]
  25. else:
  26. goods = Good.objects.all().order_by(orderBy)[skip:skip+limit]
  27. except Exception as e:
  28. return JsonResponse({"errors":[{"message":"Не вірні дані запиту"}]}, safe=False)
  29. serializer = GoodSerializer(goods, many=True)
  30. return JsonResponse({"data":serializer.data}, safe=False)
  31. @csrf_exempt
  32. @api_view(['GET'])
  33. def good_detail(request,_id):
  34. try:
  35. good = Good.objects.get(_id = _id)
  36. except Exception as e:
  37. return JsonResponse({"errors":[{"message":"Товар не знайдено"}]}, safe=False)
  38. if good:
  39. serializer = GoodSerializer(good)
  40. return JsonResponse({"data":serializer.data}, safe=False)
  41. else:
  42. return JsonResponse({"data":{}}, safe=False)
  43. @csrf_exempt
  44. @api_view(['GET'])
  45. @authentication_classes([JWTAuthentication])
  46. @permission_classes((IsAdminUser, ))
  47. def good_delete(request,_id):
  48. try:
  49. good = Good.objects.get(_id = _id)
  50. except Exception as e:
  51. return JsonResponse({"errors":[{"message":"Товар не знайдено"}]}, safe=False)
  52. if good:
  53. good.delete()
  54. return JsonResponse({"data":{"_id":_id}}, safe=False)
  55. return JsonResponse({"data":{}}, safe=False)
  56. @csrf_exempt
  57. @api_view(['POST'])
  58. @authentication_classes([JWTAuthentication])
  59. @permission_classes((IsAdminUser, ))
  60. def good_upsert(request):
  61. try:
  62. _id = request.POST.get('_id',None)
  63. name = request.POST.get("name", "")
  64. price = request.POST.get("price", "").replace(" ","")
  65. description = request.POST.get("description", "")
  66. amount = request.POST.get("amount","").replace(" ","")
  67. images = json.loads(request.POST.get("images", "[]"))
  68. categories = json.loads(request.POST.get("categories", "[]"))
  69. if not price.isdigit() or not amount.isdigit():
  70. return JsonResponse({"errors":[{"message":"Не вірні дані"}]}, safe=False)
  71. except:
  72. return JsonResponse({"errors":[{"message":"Не вірні дані"}]}, safe=False)
  73. if _id :
  74. try:
  75. good = Good.objects.get(_id = _id)
  76. except Exception as e:
  77. return JsonResponse({"errors":[{"message":"Не вірні дані запиту"}]}, safe=False)
  78. good.description = description
  79. good.name = name
  80. good.price = price
  81. good.amount = amount
  82. else:
  83. good = Good.objects.create(name=name,price=price,amount=amount,description=description)
  84. good.images.clear()
  85. for image in images:
  86. good.images.add(image["_id"])
  87. good.categories.clear()
  88. for category in categories:
  89. good.categories.add(category["_id"])
  90. if good:
  91. good.save()
  92. serializer = GoodSerializer(good)
  93. return JsonResponse({"data":serializer.data}, safe=False)
  94. else:
  95. return JsonResponse({"data":{}}, safe=False)
  96. @csrf_exempt
  97. def image_upsert(request):
  98. file = request.FILES.get("photo" , None)
  99. if file:
  100. image = Image.objects.create(url =file )
  101. image.save()
  102. serializer = ImageSerializer(image)
  103. print(serializer.data)
  104. return JsonResponse({"data":serializer.data}, safe=False)
  105. else:
  106. return JsonResponse({"data":{}}, safe=False)