Ejemplo n.º 1
0
def exploracaos_update(request):
    gid = request.matchdict['id']
    if not gid:
        raise badrequest_exception({
            'error': error_msgs['gid_obligatory']
        })

    try:
        body = request.json_body
        msgs = validate_entities(body)
        if len(msgs) > 0:
            raise badrequest_exception({'error': msgs})

        e = request.db.query(Exploracao).filter(Exploracao.gid == gid).one()

        u_id = body.get('utente').get('id')
        if not u_id:
            u = Utente.create_from_json(body['utente'])
            # TODO:660 validate utente
            request.db.add(u)
        elif e.utente_rel.gid != u_id:
            u_filter = Utente.gid == u_id
            u = request.db.query(Utente).filter(u_filter).one()
        else:
            u = e.utente_rel

        validatorUtente = Validator(UTENTE_SCHEMA)
        msgs = validatorUtente.validate(request.json_body['utente'])
        if len(msgs) > 0:
            raise badrequest_exception({'error': msgs})
        e.utente_rel = u
        u.update_from_json(request.json_body['utente'])
        request.db.add(u)

        if _tipo_actividade_changes(e, request.json_body):
            request.db.delete(e.actividade)
            del e.actividade

        lic_nro_sequence = Exploracao.LIC_NRO_SEQUENCE_FIRST
        for lic in e.licencias:
            lic_nro_sequence = calculate_lic_nro(lic.lic_nro, lic_nro_sequence)
        e.update_from_json(request.json_body, lic_nro_sequence)

        request.db.add(e)
        request.db.commit()
    except(MultipleResultsFound, NoResultFound):
        raise badrequest_exception({'error': error_msgs['no_gid'], 'gid': gid})
    except ValueError as ve:
        log.error(ve)
        raise badrequest_exception({'error': error_msgs['body_not_valid']})
    except ValidationException as val_exp:
        if u:
            request.db.refresh(u)
        if e:
            request.db.refresh(e)
        raise badrequest_exception(val_exp.msgs)

    return e
Ejemplo n.º 2
0
def exploracaos_create(request):
    try:
        body = request.json_body
        exp_id = body.get('exp_id')
    except ValueError as ve:
        log.error(ve)
        raise badrequest_exception({'error': error_msgs['body_not_valid']})

    msgs = validate_entities(body)
    if len(msgs) > 0:
        raise badrequest_exception({'error': msgs})

    e = request.db.query(Exploracao).filter(Exploracao.exp_id == exp_id).first()
    if e:
        raise badrequest_exception({'error': error_msgs['exploracao_already_exists']})

    u_filter = Utente.nome == body.get('utente').get('nome')
    u = request.db.query(Utente).filter(u_filter).first()
    if not u:
        validatorUtente = Validator(UTENTE_SCHEMA)
        msgs = validatorUtente.validate(body['utente'])
        if len(msgs) > 0:
            raise badrequest_exception({'error': msgs})
        u = Utente.create_from_json(body['utente'])
        request.db.add(u)
    try:
        e = Exploracao.create_from_json(body)
    except ValidationException as val_exp:
        if u:
            request.db.refresh(u)
        if e:
            request.db.refresh(e)
        raise badrequest_exception(val_exp.msgs)
    e.utente_rel = u
    request.db.add(e)
    request.db.commit()
    return e
Ejemplo n.º 3
0
def validate_entities(body):
    import re
    validatorExploracao = Validator(EXPLORACAO_SCHEMA)
    validatorExploracao.add_rule('EXP_ID_FORMAT', {'fails': lambda v: v and (not re.match('^\d{4}-\d{3}$', v))})
    validatorExploracao.add_rule('ACTIVITY_NOT_NULL', {'fails': activity_fail})

    msgs = validatorExploracao.validate(body)

    validatorFonte = Validator(FONTE_SCHEMA)
    for fonte in body.get('fontes'):
        msgs = msgs + validatorFonte.validate(fonte)

    validatorLicencia = Validator(LICENCIA_SCHEMA)
    validatorLicencia.add_rule('LIC_NRO_FORMAT', {'fails': lambda v: v and (not re.match('^\d{4}-\d{3}-\d{3}$', v))})
    for lic in body.get('licencias'):
        msgs = msgs + validatorLicencia.validate(lic)

    return msgs
Ejemplo n.º 4
0
 def validate(self, json):
     validator = Validator(actividades_schema.ActividadeSchema['Reses'])
     return validator.validate(json)
Ejemplo n.º 5
0
 def validate(self, json):
     validator = Validator(actividades_schema.ActividadeSchema['Abastecimento'])
     return validator.validate(json)
Ejemplo n.º 6
0
 def validate(self, json):
     validator = Validator(actividades_schema.ActividadeSchema['Producção de energia'])
     return validator.validate(json)
Ejemplo n.º 7
0
 def validate(self, json):
     validator = Validator(actividades_schema.ActividadeSchema['Piscicultura'])
     return validator.validate(json)
Ejemplo n.º 8
0
 def validate(self, json):
     validator = Validator(actividades_schema.ActividadeSchema['Indústria'])
     return validator.validate(json)
Ejemplo n.º 9
0
 def validate(self, json):
     validator = Validator(actividades_schema.ActividadeSchema['Agricultura-Regadia'])
     return validator.validate(json)