Ejemplo n.º 1
0
    def post(self):
        res = request.get_json(force=True)
        errors = Address.validate_input(res)
        if errors:
            return errors, BAD_REQUEST

        data = res['data']['attributes']

        addr = Address.create(
            uuid=uuid.uuid4(),
            user=auth.current_user,
            country=data['country'],
            city=data['city'],
            post_code=data['post_code'],
            address=data['address'],
            phone=data['phone'])

        return generate_response(addr.json(), CREATED)
Ejemplo n.º 2
0
    def patch(self, address_uuid):
        try:
            obj = Address.get(Address.user == auth.current_user,
                              Address.uuid == address_uuid)
        except Address.DoesNotExist:
            return None, NOT_FOUND

        res = request.get_json(force=True)

        errors = Address.validate_input(res)
        if errors:
            return errors, BAD_REQUEST

        data = res['data']['attributes']

        country = data.get('country')
        city = data.get('city')
        post_code = data.get('post_code')
        address = data.get('address')
        phone = data.get('phone')

        if country and country != obj.country:
            obj.country = country

        if city and city != obj.city:
            obj.city = city

        if post_code and post_code != obj.post_code:
            obj.post_code = post_code

        if address and address != obj.address:
            obj.address = address

        if phone and phone != obj.phone:
            obj.phone = phone

        obj.save()

        return generate_response(obj.json(), OK)