Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    def test_save(self):
        cust = Customer()
        cust.name = "test name"
        cust.zipcode = 666
        cust.save()

        self.assertEquals(cust.id, 1)