def test_get_defined_customer_success(self): serializer_data = { 'first_name': 'john', 'last_name': 'Biden', 'TC': '12531835135', 'gsm': '+905531221455' } serializer = CustomerSerializer(data=serializer_data) serializer.is_valid() serializer.save() res = self.client.get(GET_CUSTOMER_URL, serializer_data) self.assertEqual(res.status_code, status.HTTP_200_OK)
def test_customer_exists(self): serializer_data = { 'first_name': 'john', 'last_name': 'Biden', 'TC': '12531835135', 'gsm': '+905531221455' } serializer = CustomerSerializer(data=serializer_data) serializer.is_valid() serializer.save() res = self.client.post(CREATE_CUSTOMER_URL, serializer_data) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
def create(self, request): serializer = CustomerSerializer(data=request.data) if serializer.is_valid(): customer = Customer() customer.name = serializer['name'].value customer.zipcode = serializer['zipcode'].value #Call BOB Api r = requests.get(BOB_API_URL.format(customer.zipcode)) if r.status_code == 200 and r.json()['results']: print("ZipCode found") ret = r.json()['results'][0] customer.street = ret['street'] customer.state = ret['state'] customer.city = ret['city'] else: print("zipCode not found on BOB_API") self.perform_create(customer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def post(self, request): try: # handling form or x-url-encoded-form data input request_body_dict = request.data.dict() except: # handling ray data request_body_dict = request.data serializer = CustomerSerializer(data=request_body_dict) try: if serializer.is_valid(): customer = serializer.save() customer_data = [{ 'id': customer.id, 'first_name': customer.first_name, 'last_name': customer.last_name, 'email': customer.email, 'dob': str(customer.dob) }] return HttpResponse(json.dumps({'data': customer_data}), status=status.HTTP_201_CREATED, content_type='application/json') return HttpResponse(json.dumps({'error': serializer.errors}), status=status.HTTP_400_BAD_REQUEST, content_type='application/json') except IntegrityError as ie: return HttpResponse(json.dumps({'error': str(ie.__cause__)}), status=status.HTTP_409_CONFLICT, content_type='application/json')
def put(self, request, pk, format=None): Customer = self.get_object(pk) serializer = CustomerSerializer(Customer, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, id, format=None): serial = CustomerSerializer(data=request.data) if serial.is_valid(): serial.save() return Response(serial.data, status=status.HTTP_201_CREATED) return Response(serial.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = CustomerSerializer(data=request.data) if serializer.is_valid(): # serializer.save(user=self.get_queryset(request)) serializer.save() stripe_customer = stripe.Customer.create( email=request.user.email, description="My First Test Customer (created for API docs)", ) stripe.Customer.create_source( "cus_IEN4tSHEAoe1ZH", source="tok_mastercard", ) stripe.Charge.create( amount=2000, currency="inr", source="tok_mastercard", description="My First Test Charge (created for API docs)", ) send_email_task.delay() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(status=status.HTTP_400_BAD_REQUEST) return Response({'serializer': serializer})
def put(self, request, id, format=None): cust = self.get_object(id) serial = CustomerSerializer(cust, data=request.data) if serial.is_valid(): serial.save() return Response(serial.data, status=status.HTTP_201.CREATED) return Response(serial.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request): customer_profile = Customer.objects.get(mobile=request.user) serializer = CustomerSerializer(customer_profile, data=request.data) if serializer.is_valid(): serializer.save() response = {"msg": "OK"} status_code = status.HTTP_200_OK return Response(response, status=status_code) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def createCustomer(self, request): try: data = JSONParser().parse(request) serializer = CustomerSerializer(data=data) if serializer.is_valid(): serializer.save() return self.generatePostSuccessResponse(self, serializer) return self.generateFormErrorResponse(self, serializer) except Exception as e: # return self.handleException(self, e) return JsonResponse(e, safe=False)
def put(self, request, pk, format=None): customer_object = self.get_object(pk) serializer = CustomerSerializer(customer_object, data=request.data) ElasticSearchService.store_customer_updation_query( request.data, 'customer', customer_object.id) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def customer_add(request): # /cusAdd httpResponse/ ADD One if request.method == 'POST': customer_data = JSONParser().parser(request) customer_serializer = CustomerSerializer(data=customer_data) if customer_serializer.is_valid(): customer_serializer.save() return JsonResponse(customer_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(customer_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def customer(request): if request.method == 'GET': category = Customer.objects.all() serializer = CustomerSerializer(category, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = CustomerSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format = None): serializer = CustomerSerializer(data = request.data) if serializer.is_valid(): user = User(first_name = serializer.data['first_name'], \ last_name = serializer.data['last_name'], \ username = serializer.data['username'], \ email = serializer.data['email']) user.save() customer = Customer(user = user, city = serializer.data['city']) customer.save() return Response(serializer.data, status = status.HTTP_201_CREATED) return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
def customer_list(request): if request.method == 'GET': customer = Customer.objects.all() serializer = CustomerSerializer(customer, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = CustomerSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def customer(request): if request.method == 'GET': category=Customer.objects.all() serializer=CustomerSerializer(category,many=True) return Response(serializer.data) elif request.method == 'POST': serializer=CustomerSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data,status=status.HTTP_201_CREATED) return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
def show(request, pk): # find tutorial by pk (id) try: customer = Customer.objects.get(pk=pk) #Get Single Data if request.method == 'GET': customer_serializer = CustomerSerializer(customer) return JsonResponse({ "success": bool(1), "message": "Data Found", "data": customer_serializer.data }) # Update Data elif request.method == 'PUT': customer_data = JSONParser().parse(request) customer_serializer = CustomerSerializer(customer, data=customer_data) if customer_serializer.is_valid(): customer_serializer.save() return JsonResponse({ "success": bool(1), "message": "Data Successfully Update", "data": customer_serializer.data }) return JsonResponse( { "success": bool(0), "message": customer_serializer.errors, "data": [] }, status=status.HTTP_400_BAD_REQUEST) # Delete Data elif request.method == 'DELETE': customer.delete() return JsonResponse( { "success": bool(1), "message": "Customer was deleted successfully!", "data": [] }, status=status.HTTP_204_NO_CONTENT) except Customer.DoesNotExist: return JsonResponse( { "success": bool(0), "message": "Customer does not exist!", "data": [] }, status=status.HTTP_204_NO_CONTENT)
def put(self, request, pk, format = None): customer = self.get_object(pk) self.check_object_permissions(self.request, customer) serializer = CustomerSerializer(data=request.data) if serializer.is_valid(): user = customer.user user.first_name = serializer.data['first_name'] user.last_name = serializer.data['last_name'] user.email = serializer.data['email'] user.save() customer.city = serializer.data['city'] customer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): serializer = CustomerSerializer(data=request.data) if serializer.is_valid(): logger.info('Creating user %s started' % serializer.data['username']) user = User(first_name = serializer.data['first_name'], \ last_name = serializer.data['last_name'], \ username = serializer.data['username'], \ email = serializer.data['email']) user.set_password(serializer.data['password']) user.save() customer = Customer(user=user, city=serializer.data['city'], phone=serializer.data['phone']) customer.save() create_confirmations(customer) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def index(request): #Get Data if request.method == 'GET': customer = Customer.objects.all() customer_serializer = CustomerSerializer(customer, many=True) return JsonResponse( { "success": bool(1), "message": "Data Found", "data": customer_serializer.data }, safe=False) #Store Data elif request.method == 'POST': if request.name == "": return JsonResponse( { "success": bool(0), "message": "name is required", "data": [] }, status=status.HTTP_400_BAD_REQUEST) customer_data = JSONParser().parse(request) customer_serializer = CustomerSerializer(data=customer_data) if customer_serializer.is_valid(): customer_serializer.save() return JsonResponse( { "success": bool(1), "message": "Data Successfuly Insert", "data": customer_serializer.data }, status=status.HTTP_201_CREATED) return JsonResponse( { "success": bool(0), "message": customer_serializer.errors, "data": [] }, status=status.HTTP_400_BAD_REQUEST)
def add_customers(self, request): file = request.FILES['file'] if file: excel_data_df = pandas.read_excel(file, sheet_name='Sheet1') data = excel_data_df.to_dict(orient='record') for i in data: if not math.isnan(i['phone_number']): print('1', type(i['phone_number'])) # if i['mobile_number']: # print('11') # if not math.isnan(i['mobile_number']): # i['mobile_number'] = int(i['mobile_number']) # else: # i['mobile_number'] = ' ' # if i['phone_res']: # print('111') # if not math.isnan(i['phone_res']): # i['phone_res'] = int(i['phone_res']) # else: # i['phone_res'] = ' ' phone_number_exists = Customer.objects.filter( phone_number=i['phone_number']).exists() if not phone_number_exists: print('1111') serializer = CustomerSerializer(data=i) if serializer.is_valid(): print('11111') serializer.save() logger.info( f'Customer Details added : {serializer.data}', ) print('Added value : ', serializer.data) print(serializer.errors) return response.post_success('Data added successfully')
def post(self, request): request_data = request.data.copy() serializer = CustomerSerializer(data=request_data) if serializer.is_valid(): request_data['is_customer'] = True user_serializer = UserSerializer(data=request_data) if user_serializer.is_valid(): serializer.save() user_serializer.save() email = request_data['email'] mail = encrypt(email) url = 'https://food-vendors-app.herokuapp.com/confirmsignup/?mail=' + mail send_mail( 'Confirm Email', 'Click the link below to complete email verification \n' + url, '*****@*****.**', [email], html_message="<h4>Click <a href='" + url + "'>here</a> to complete email verification</h4>", fail_silently=False) return Response(serializer.data, status=201) return Response(user_serializer.errors, status=400) return Response(serializer.errors, status=400)