def post(self): ''' Create an address ''' arguments = request.get_json(force=True) district = arguments.get('district').strip() postal = arguments.get('postal').strip() or None country = arguments.get('country').strip() or None address_line_1 = arguments.get('address1').strip() or None address_line_2 = arguments.get('address1').strip() or None if not address_line_1: return abort(400, 'Address cannot be empty!') try: address = Address( district=district, postal_code=postal, country=country, address_line_1=address_line_1, address_line_2=address_line_2 ) if address.save_address(): return {'message': 'Address created successfully!'}, 201 return abort(409, message='Address already exists!') except Exception as e: abort(400, message='Failed to create new address -> {}'.format(e))
def post(self): ''' Create a company ''' arguments = request.get_json(force=True) name = arguments.get('name').strip() district = arguments.get('district').strip() or None postal = arguments.get('postal').strip() or None country = arguments.get('country').strip() or None tech_person_name_string = arguments.get( 'techPersonName').strip() or None tech_person_email = arguments.get('techPersonEmail').strip() or None address_line_1 = arguments.get('address1').strip() or None address_line_2 = arguments.get('address2').strip() or None legal_person_name_str = arguments.get( 'legalPersonName').strip() or None legal_person_email = arguments.get('legalPersonEmail').strip() or None tech_person_name = tech_person_name_string.split() legal_person_name = legal_person_name_str.split() if not name: return abort(400, 'Name cannot be empty!') try: address = Address( district=district, postal_code=postal, country=country, address_line_1=address_line_1, address_line_2=address_line_2 ) tech_person = Person(tech_person_name[0], tech_person_name[-1]) legal_person = Person(legal_person_name[0], legal_person_name[-1]) tech_contact = Contact(email=tech_person_email) legal_contact = Contact(email=legal_person_email) if not address.save_address(): address = Address.query.filter_by( address_line_1=address.address_line_1, active=True).first() if not tech_person.save_person(): tech_person = Person.query.filter_by( full_name=tech_person_name_string).first() if not legal_person.save_person(): legal_person = Person.query.filter_by( full_name=legal_person_name_str).first() if not tech_contact.save_contact(): tech_contact = Contact.query.filter_by( email=tech_person_email).first() if not legal_contact.save_contact(): legal_contact = Contact.query.filter_by( email=legal_person_email).first() tech_contact_person = ContactPerson( person=tech_person, contact=tech_contact) legal_contact_person = ContactPerson( person=legal_person, contact=legal_contact) if not tech_contact_person.save_contact_person(): tech_contact_person = ContactPerson.query.filter_by( person=tech_person, contact=tech_contact).first() if not legal_contact_person.save_contact_person(): legal_contact_person = ContactPerson.query.filter_by( person=legal_person, contact=legal_contact).first() company = Company( name=name, address=address, legal_person=legal_contact_person, tech_person=tech_contact_person ) if company.save_company(): return {'message': 'Company created successfully!'}, 201 return abort(409, message='Company already exists!') except Exception as e: abort(400, message='Failed to create new company -> {}'.format(e))