def post(self, party_guid=None): if party_guid: raise BadRequest('Unexpected party id in Url.') data = PartyListResource.parser.parse_args() party = Party.create( data.get('party_name'), data.get('phone_no'), data.get('party_type_code'), # Nullable fields email=data.get('email'), first_name=data.get('first_name'), phone_ext=data.get('phone_ext')) if not party: raise InternalServerError('Error: Failed to create party') # If no address data is provided do not create an address. if (data.get('suite_no') or data.get('address_line_1') or data.get('address_line_2') or data.get('city') or data.get('sub_division_code') or data.get('post_code')): address = Address.create(suite_no=data.get('suite_no'), address_line_1=data.get('address_line_1'), address_line_2=data.get('address_line_2'), city=data.get('city'), sub_division_code=data.get('sub_division_code'), post_code=data.get('post_code')) party.address.append(address) party.save() return party
def put(self, party_guid): data = PartyResource.parser.parse_args() existing_party = Party.find_by_party_guid(party_guid) if not existing_party: raise NotFound('Party not found.') current_app.logger.info(f'Updating {existing_party} with {data}') for key, value in data.items(): if key in ['party_type_code']: continue # non-editable fields from put setattr(existing_party, key, value) # We are now allowing parties to be created without an address if (data.get('suite_no') or data.get('address_line_1') or data.get('address_line_2') or data.get('city') or data.get('sub_division_code') or data.get('post_code')): # and check that we are changing the address if len(existing_party.address) == 0: address = Address.create() existing_party.address.append(address) for key, value in data.items(): setattr(existing_party.address[0], key, value) existing_party.save() return existing_party