示例#1
0
    def post(self):
        ''' Create a employee '''
        arguments = request.get_json(force=True)
        first_name = arguments.get('firstName').strip()
        last_name = arguments.get('lastName').strip()
        department = arguments.get('department').strip() or None
        tel_one = arguments.get('telOne').strip() or None
        tel_two = arguments.get('telTwo').strip() or None
        email = arguments.get('email').strip() or None
        role = arguments.get('role').strip() or None

        if not last_name:
            return abort(400, 'Name cannot be empty!')
        try:
            person = Person(first_name, last_name)
            contact = Contact(email=email, tel_one=tel_one, tel_two=tel_two)
            department = Department.query.filter_by(id=int(department)).first()

            if not person.save_person():
                person = Person.query.filter_by(full_name=(first_name + ' ' +
                                                           last_name)).first()
            if not contact.save_contact():
                contact = Contact.query.filter_by(email=email).first()
            contact_person = ContactPerson(person=person, contact=contact)
            if not contact_person.save_contact_person():
                contact_person = ContactPerson.query.filter_by(
                    person=person, contact=contact).first()

            employee = Employee(contact_person=contact_person,
                                department=department)
            if employee.save_employee():
                return {'message': 'Employee created successfully!'}, 201
            return abort(409, message='Employee already exists!')
        except Exception as e:
            abort(400, message='Failed to create new employee -> {}'.format(e))
示例#2
0
    def post(self):
        ''' Create a person '''
        arguments = request.get_json(force=True)
        first_name = arguments.get('firstName').strip()
        last_name = arguments.get('lastName').strip()

        if not last_name:
            return abort(400, 'Last Name cannot be empty!')
        try:
            person = Person(first_name=first_name, last_name=last_name)
            if person.save_person():
                return {'message': 'Person created successfully!'}, 201
            return abort(409, message='Person already exists!')
        except Exception as e:
            abort(400, message='Failed to create new person -> {}'.format(e))
示例#3
0
    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))