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
def _transmogrify_contacts(now_app, now_sub, mms_now_sub): for c in now_sub.contacts: emailValidator = re.compile(r'[^@]+@[^@]+\.[^@]+') now_party_appt = None if c.type == 'Individual' and c.contacttype and c.ind_lastname and c.ind_firstname and c.ind_phonenumber: now_party = Party( party_name=c.ind_lastname, first_name=c.ind_firstname, party_type_code='PER', phone_no=c.ind_phonenumber[:3] + "-" + c.ind_phonenumber[3:6] + "-" + c.ind_phonenumber[6:], email=c.email if c.email and emailValidator.match(c.email) else None, ) now_party_mine_party_appt_type = MinePartyAppointmentType.find_by_mine_party_appt_type_code( _map_contact_type(c.contacttype)) now_party_appt = app_models.NOWPartyAppointment( mine_party_appt_type_code=now_party_mine_party_appt_type. mine_party_appt_type_code, mine_party_appt_type=now_party_mine_party_appt_type, party=now_party) if c.type == 'Organization' and c.contacttype and c.org_legalname and c.dayphonenumber: now_party = Party( party_name=c.org_legalname, party_type_code='ORG', phone_no=c.dayphonenumber[:3] + "-" + c.dayphonenumber[3:6] + "-" + c.dayphonenumber[6:], phone_ext=c.dayphonenumberext, email=c.email if c.email and emailValidator.match(c.email) else None, ) now_party_mine_party_appt_type = MinePartyAppointmentType.find_by_mine_party_appt_type_code( _map_contact_type(c.contacttype)) now_party_appt = app_models.NOWPartyAppointment( mine_party_appt_type_code=now_party_mine_party_appt_type. mine_party_appt_type_code, mine_party_appt_type=now_party_mine_party_appt_type, party=now_party) if now_party_appt: validPostalCode = re.compile(r"\s*([a-zA-Z]\s*\d\s*){3}$") post_code = c.mailingaddresspostalzip.replace( " ", "") if c.mailingaddresspostalzip and validPostalCode.match( c.mailingaddresspostalzip.replace(" ", "")) else None if c.mailingaddressline1 and c.mailingaddresscity and c.mailingaddressprovstate and c.mailingaddresscountry: now_address = Address( address_line_1=c.mailingaddressline1, address_line_2=c.mailingaddressline2, city=c.mailingaddresscity, sub_division_code=c.mailingaddressprovstate.replace( " ", ""), post_code=post_code, address_type_code='CAN' if c.mailingaddresscountry == 'Canada' else 'USA') now_party_appt.party.address.append(now_address) now_app.contacts.append(now_party_appt) return
def test_party_model_validate_post_code(): with pytest.raises(AssertionError) as e: Address(suite_no='123', address_line_1='Foo', address_line_2='Bar', city='Baz', sub_division_code='AB', post_code='0' * 7) assert 'post_code must not exceed 6 characters' in str(e.value) with pytest.raises(AssertionError) as e: Address(suite_no='123', address_line_1='Foo', address_line_2='Bar', city='Baz', sub_division_code='AB', post_code='0' * 6) assert 'Invalid post_code format' in str(e.value)