def service_create(request): title = request.POST.get('title', None) short_description = request.POST.get('short_description', None) long_description = request.POST.get('long_description', None) duration = request.POST.get('duration', None) price = request.POST.get('price', None) if title is None: msg = {'message': 'body parameter "title" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif short_description is None: msg = {'message': 'body parameter "short_description" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif long_description is None: msg = {'message': 'body parameter "long_description" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif duration is None: msg = {'message': 'body parameter "duration" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif price is None: msg = {'message': 'body parameter "price" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) service = ServiceSerializer( data={ 'title': title, 'short_description': short_description, 'long_description': long_description, 'duration': duration, 'price': price, }) if service.is_valid(): service.save() return JsonResponse(service.data, status=status.HTTP_201_CREATED) return JsonResponse(service.errors, status=status.HTTP_400_BAD_REQUEST)
def service_create(request): image_url = request.POST.getlist('image_url') if len(image_url) == 0: image_url = request.POST.getlist('image_url[]') title = request.POST.get('title', None) short_description = request.POST.get('short_description', None) long_description = request.POST.get('long_description', None) duration = request.POST.get('duration', None) price = request.POST.get('price', None) vip_per = request.POST.get('vip_per', '1') nor_per = request.POST.get('nor_per', '1') minus = request.POST.get('minus', '0') if title is None: msg = {'message': 'body parameter "title" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif short_description is None: msg = {'message': 'body parameter "short_description" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif long_description is None: msg = {'message': 'body parameter "long_description" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif duration is None: msg = {'message': 'body parameter "duration" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif price is None: msg = {'message': 'body parameter "price" should be given'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) elif not str(minus).isnumeric(): msg = {'message': 'body parameter "minus" should be an integer'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) try: vip_per = float(vip_per) nor_per = float(nor_per) except ValueError: msg = { 'message': 'body parameter "vip_per" or "nor_per" should be a float number' } return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) try: for url in image_url: Image.objects.get(url=url) for url in image_url: Image.increaseUsage(url, 1) except Image.DoesNotExist: msg = {'message': 'The image_url "' + url + '" is not exist'} return JsonResponse(msg, status=status.HTTP_400_BAD_REQUEST) service = ServiceSerializer( data={ 'title': title, 'image_url': image_url, 'short_description': short_description, 'long_description': long_description, 'duration': duration, 'price': price, 'vip_per': vip_per, 'nor_per': nor_per, 'minus': minus }) if service.is_valid(): service.save() return JsonResponse(service.data) return JsonResponse(service.errors, status=status.HTTP_400_BAD_REQUEST)