Exemplo n.º 1
0
def create_division_types():
    if not request.json:
        abort(400)
    #empty check
    if not all(
            len(val.strip()) >= 1
            for val in request.json.values() if isinstance(val, unicode)):
        return length_require_envelop()

    _result = {'name'} - set(request.json.keys())
    if _result:
        return keys_require_envelop('key : name is required')

    request.json['display_name'] = request.json['name'].strip()
    request.json['name'] = request.json['name'].strip().lower()

    try:
        db_session.add(DivisionTypeSetup(**request.json))
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Exemplo n.º 2
0
def create_division(t_id):
    if not request.json:
        abort(400)
    if not all(
            len(val.strip()) >= 1
            for val in request.json.values() if isinstance(val, unicode)):
        return length_require_envelop()

    _result = {'name', 'code'} - set(request.json.keys())
    if _result:
        return keys_require_envelop('Key: %r is require' % ', '.join(_result))

    request.json['display_name'] = request.json['name'].strip()
    request.json['name'] = request.json['name'].strip().lower()

    #inject the id
    request.json['division_type_id'] = t_id
    try:
        db_session.add(Division(**request.json))
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Exemplo n.º 3
0
def create_training_by_emp(id):
    if not request.json:
        abort(400)
    #check if there is empty field comming up
    if not all(
            len(str(val).strip()) >= 1 for key, val in request.json.items()):
        abort(411)

    #check if there is no registration number and registration body
    result = {'name'} - set(request.json.keys())
    if result:
        return keys_require_envelop('key : "name" is required')
    #clean up the values
    trs = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }
    #insert
    ###################################
    trs['start_date'] = '01-feb-2012'
    trs['end_date'] = '03-feb-2005'
    ##########################
    print(id)
    trs['employee_id'] = id
    try:
        print(id)
        db_session.add(Training(**trs))
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Exemplo n.º 4
0
def create_certification_by_emp(id):
    if not request.json:
        abort(400)
    #check if there is empty field comming up
    if not all(
            len(str(val).strip()) >= 1 for key, val in request.json.items()):
        abort(411)

    #check if there is no registration number and registration body
    result = {'regulatory_body', 'registration_number'} - set(
        request.json.keys())
    if result:
        return keys_require_envelop(
            '"regulatory_body" and "regstration_number" is required')
    #clean up the values
    cert = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }
    #insert
    cert['employee_id'] = id
    ###################################
    cert['expiry_date'] = '04-mar-2012'
    cert['last_renewal_date'] = '03-mar-2017'
    ################################
    try:

        db_session.add(Certification(**cert))
        db_session.commit()
    except IntegrityError as e:
        return record_exists_envelop()
    except Exception as e:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Exemplo n.º 5
0
def create_employee():

    if not request.json:
        abort(400)

    req_fields = {
        'first_name', 'last_name', 'current_sex', 'address_one', 'age',
        'retirement_age', 'employee_type_id', 'employee_category_id',
        'date_of_birth', 'employee_position_id', 'employement_number',
        'employee_agency_id'
    }
    result = req_fields - set(request.json.keys())

    #if there is some value then abort
    if result:

        return keys_require_envelop('Key required %s' % ', '.join(result))

    #if everything is included, check to see if there is any empty values
    # if not all(len(str(val).strip()) >= 1 for key, val in request.json.items()):
    #     abort(411)

    #now clean up the data to insert into database(onyl for strin)
    data = {
        key: val.strip() if isinstance(val, str) else val
        for key, val in request.json.items()
    }

    #for oracle
    #data['date_of_birth'] = '01-feb-2017'
    #data['date_of_commencement'] = '02-feb-2011'
    #now try to insert
    #for raocle

    try:

        emp = Employee(**data)
        db_session.add(emp)
        db_session.commit()
    except IntegrityError as e:

        return record_exists_envelop()
    except Exception as e:
        print(e)
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Exemplo n.º 6
0
 def wrapper(*args, **kwargs):
     fields_db = set(col.name for col in model.__mapper__.columns)
     if not request.json:
         abort(400)
     required_keys = fields_db - set(_exclude) if _exclude else set()
     result = request.json.keys() - required_keys
     if result:
         return extra_keys_envelop('Keys not Accepeted %r' % (', '.join(key for key in result)))
     #check if there are any missing keys
     result = required_keys - request.json.keys()
     if result:
         return keys_require_envelop('Keys required %r' %(' ,'.join(key for key  in result)))
     #check if there are any fields emopty
     if not all(len(str(val).strip()) >= 1 for val in request.json.values()):
         return length_require_envelop()
     #everythin is okay return the function
     return func(*args, **kwargs)
Exemplo n.º 7
0
def create_facilities_by_facility_type(f_id):
    if not request.json:
        abort(400)

    #check to see if there is empty fileds
    if not all(len(val.strip()) >= 1 for val in request.json.values()\
                                                if isinstance(val, str)):
        return length_require_envelop()

    #now see if if mandatory fields are not provided

    _required_keys = {
        'district_id', 'province_id', 'region_id', 'agency_id',
        'facility_name', 'facility_code'
    }

    _remaining = _required_keys - set(request.json.keys())
    if _remaining:
        return keys_require_envelop('Missing Keys %s' % ', '.join(_remaining))

    #now change facility display_name
    request.json['facility_display_name'] = request.json[
        'facility_name'].strip()
    request.json['facility_name'] = request.json['facility_name'].lower(
    ).strip()

    #now inject the facility type id
    request.json['facility_type_id'] = f_id

    #now initiate the session
    try:
        db_session.add(Facility(**request.json))
        db_session.commit()
    except IntegrityError:

        return record_exists_envelop()
    except Exception:

        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Exemplo n.º 8
0
def create_division_positions_by_meta():
    if not request.json:
        abort(400)

    _required_fields = ['fac_div_id', 'position_title', 'position_capacity']
    if not any((item in request.json.keys()) for item in _required_fields):
        return keys_require_envelop()

    position_capacity = int(request.json['position_capacity'])
    position_title = request.json['position_title']
    fac_div_id = int(request.json['fac_div_id'])
    try:
        divposmeta = DivisionPositionMeta(**request.json)
        db_session.add(divposmeta)
        #now get the total present count of the table
        obj = db_session.query(DivisionPositionMeta).order_by(
            DivisionPositionMeta.id.desc()).first()
        c = obj.id + 1
        #for the capacity , addd the new divisionposition
        for i in range(position_capacity):
            db_session.add(
                DivisionPosition(fac_div_id=fac_div_id,
                                 position_title=position_title,
                                 div_pos_code=''.join(
                                     [str(fac_div_id), position_title,
                                      str(c)]),
                                 div_pos_name=''.join(
                                     [str(fac_div_id), position_title,
                                      str(c)])))

            #now increa the value of c
            c += 1
        db_session.commit()
    except IntegrityError:
        raise
    except Exception:
        raise
    else:
        return record_created_envelop(request.json)
Exemplo n.º 9
0
def create_facility_division(f_id):
    if not request.json:
        abort(400)
    if 'fac_div_name' not in request.json:
        return keys_require_envelop()

    if not all(
            len(val.strip()) >= 1
            for val in request.json.values() if isinstance(val, unicode)):
        return length_require_envelop()

    #inject the value into the json
    request.json['facility_id'] = f_id

    try:
        db_session.add(FacilityDivision(**request.json))
        db_session.commit()
    except IntegrityError:

        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)
Exemplo n.º 10
0
def create_agencytypes():
    """
    This is used to create a new agency type.
    Call this api to create a new agency type.
    ---
    tags:
      - Create a Agency Type
    parameters:
      - name: language
        in: path
        type: string
        required: true
        description: The language name
      - name: size
        in: query
        type: integer
        description: size of awesomeness
    responses:
      500:
        description: Error The language is not awesome!
      200:
        description: A language with its awesomeness
        schema:
          id: awesome
          properties:
            language:
              type: string
              description: The language name
              default: Lua
            features:
              type: array
              description: The awesomeness list
              items:
                type: string
              default: ["perfect", "simple", "lovely"]

    """
    if not request.json:
        abort(400)
    #check for the empty fields
    if not all(
            len(val.strip()) >= 1
            for val in request.json.values() if isinstance(val, str)):
        return length_require_envelop()
    #now check if there is any extra keys
    from_db = set(request.json.keys()) - {'name', 'code', 'del_flag'}
    if from_db:
        return extra_keys_envelop('Extra keys : %s' % ', '.join(from_db))

    required_keys = {'name', 'code'} - set(request.json.keys())
    if required_keys:
        return keys_require_envelop('Keys required : %s' %
                                    ', '.join(required_keys))

    #if everythin is fine
    if 'name' in request.json:
        request.json['display_name'] = request.json['name'].strip()
        request.json['name'] = request.json['name'].lower().strip()

    if 'code' in request.json:
        request.json['code'] = request.json['code'].strip()

    try:
        db_session.add(AgencyType(**request.json))
        db_session.commit()
    except IntegrityError:
        return record_exists_envelop()
    except Exception:
        return fatal_error_envelop()
    else:
        return record_created_envelop(request.json)