def acquisitionFrameworkHandler(request, *, acquisition_framework, info_role): # Test des droits d'édition du acquisition framework si modification if acquisition_framework.id_acquisition_framework is not None: user_cruved = cruved_scope_for_user_in_module( id_role=info_role.id_role, module_code="METADATA", )[0] af_cruved = acquisition_framework.get_object_cruved( info_role, user_cruved) #verification des droits d'édition pour le acquisition framework if not af_cruved['U']: raise InsufficientRightsError( "User {} has no right in acquisition_framework {}".format( info_role.id_role, acquisition_framework.id_acquisition_framework), 403, ) else: acquisition_framework.id_digitizer = info_role.id_role acquisitionFrameworkSchema = AcquisitionFrameworkSchema() acquisition_framework, errors = acquisitionFrameworkSchema.load( request.get_json(), instance=acquisition_framework) if bool(errors): raise BadRequest(errors) DB.session.add(acquisition_framework) DB.session.commit() return acquisition_framework
def acquisitionFrameworkHandler(request, *, acquisition_framework, info_role): # Test des droits d'édition du acquisition framework si modification if acquisition_framework.id_acquisition_framework is not None: user_cruved = get_scopes_by_action(module_code="META_DATA") #verification des droits d'édition pour le acquisition framework if not acquisition_framework.has_instance_permission(user_cruved['U']): raise InsufficientRightsError( "User {} has no right in acquisition_framework {}".format( g.current_user, acquisition_framework.id_acquisition_framework), 403, ) else: acquisition_framework.id_digitizer = info_role.id_role acquisitionFrameworkSchema = AcquisitionFrameworkSchema(only=[ "cor_af_actor", "cor_volets_sinp", "cor_objectifs", "cor_territories" ], unknown=EXCLUDE) try: acquisition_framework = acquisitionFrameworkSchema.load( request.get_json(), instance=acquisition_framework) except ValidationError as error: log.exception(error) raise BadRequest(error.messages) DB.session.add(acquisition_framework) DB.session.commit() return acquisition_framework
def get_acquisition_framework(info_role, id_acquisition_framework): """ Get one AF with nomenclatures .. :quickref: Metadata; :param id_acquisition_framework: the id_acquisition_framework :param type: int :returns: dict<TAcquisitionFramework> """ acquisitionFrameworkSchema = AcquisitionFrameworkSchema() user_cruved = cruved_scope_for_user_in_module( id_role=info_role.id_role, module_code="METADATA", )[0] acquisitionFrameworkSchema.context = { 'info_role': info_role, 'user_cruved': user_cruved } acquisition_framework = DB.session.query(TAcquisitionFramework).get( id_acquisition_framework) if not acquisition_framework: raise NotFound('Acquisition framework "{}" does not exist'.format( id_acquisition_framework)) return acquisitionFrameworkSchema.jsonify(acquisition_framework)
def get_acquisition_frameworks(info_role): """ Get a simple list of AF without any nested relationships Use for AF select in form Get the GeoNature CRUVED """ only = [] af_list = ( TAcquisitionFramework.query.filter_by_readable().filter_by_params( request.args.to_dict()).options( Load(TAcquisitionFramework).raiseload('*'), # for permission checks: joinedload('creator'), joinedload('cor_af_actor').options( joinedload('role'), joinedload('organism'), ), joinedload('t_datasets').options( joinedload('digitizer'), joinedload('cor_dataset_actor').options( joinedload('role'), joinedload('organism'), ), ), )) if request.args.get('datasets', default=False, type=int): only.extend([ 't_datasets', ]) if request.args.get('creator', default=False, type=int): only.append('creator') af_list = af_list.options(joinedload('creator')) if request.args.get('actors', default=False, type=int): only.extend([ 'cor_af_actor', 'cor_af_actor.nomenclature_actor_role', 'cor_af_actor.organism', 'cor_af_actor.role', ]) af_list = af_list.options( joinedload('cor_af_actor').options( joinedload('nomenclature_actor_role'), ), ) if request.args.get('datasets', default=False, type=int): only.extend([ 't_datasets.cor_dataset_actor', 't_datasets.cor_dataset_actor.nomenclature_actor_role', 't_datasets.cor_dataset_actor.organism', 't_datasets.cor_dataset_actor.role', ]) af_list = af_list.options( joinedload('t_datasets').options( joinedload('cor_dataset_actor').options( joinedload('nomenclature_actor_role'), ), ), ) af_schema = AcquisitionFrameworkSchema(only=only) user_cruved = cruved_scope_for_user_in_module( id_role=info_role.id_role, module_code="METADATA", )[0] af_schema.context = {'user_cruved': user_cruved} return af_schema.jsonify(af_list.all(), many=True)
def get_acquisition_frameworks_list(info_role): """ Get all AF with their datasets Use in metadata module for list of AF and DS Add the CRUVED permission for each row (Dataset and AD) .. :quickref: Metadata; :param info_role: add with kwargs :type info_role: TRole :qparam list excluded_fields: fields excluded from serialization :qparam boolean nested: Default False - serialized relationships. If false: remove add all relationships in excluded_fields """ if current_app.config["CAS_PUBLIC"]["CAS_AUTHENTIFICATION"]: # synchronise the CA and JDD from the MTD WS try: mtd_utils.post_jdd_from_user(id_user=info_role.id_role, id_organism=info_role.id_organisme) except Exception as e: log.error(e) params = request.args.to_dict() params["orderby"] = "acquisition_framework_name" if "selector" not in params: params["selector"] = None user_cruved = cruved_scope_for_user_in_module( id_role=info_role.id_role, module_code="METADATA", )[0] nested_serialization = params.get("nested", False) nested_serialization = True if nested_serialization == "true" else False exclude_fields = [] if "excluded_fields" in params: exclude_fields = params.get("excluded_fields") try: exclude_fields = exclude_fields.split(',') except: raise BadRequest("Malformated parameter 'excluded_fields'") if not nested_serialization: # exclude all relationships from serialization if nested = false exclude_fields = [ db_rel.key for db_rel in inspect(TAcquisitionFramework).relationships ] acquisitionFrameworkSchema = AcquisitionFrameworkSchema( exclude=exclude_fields) acquisitionFrameworkSchema.context = { 'info_role': info_role, 'user_cruved': user_cruved } return acquisitionFrameworkSchema.jsonify(get_metadata_list( info_role, params, exclude_fields).all(), many=True)
def get_acquisition_frameworks(info_role): """ Get a simple list of AF without any nested relationships Use for AF select in form Get the GeoNature CRUVED """ params = request.args.to_dict() exclude_fields = [ db_rel.key for db_rel in inspect(TAcquisitionFramework).relationships ] acquisitionFrameworkSchema = AcquisitionFrameworkSchema( exclude=exclude_fields) return acquisitionFrameworkSchema.jsonify(get_metadata_list( info_role, params, exclude_fields).all(), many=True)
def get_acquisition_framework(info_role, id_acquisition_framework): """ Get one AF with nomenclatures .. :quickref: Metadata; :param id_acquisition_framework: the id_acquisition_framework :param type: int :returns: dict<TAcquisitionFramework> """ af = TAcquisitionFramework.query.get_or_404(id_acquisition_framework) if not af.has_instance_permission(scope=int(info_role.value_filter)): raise Forbidden(f"User {g.current_user} cannot read acquisition " "framework {af.id_acquisition_framework}") exclude = request.args.getlist("exclude") try: af_schema = AcquisitionFrameworkSchema(only=[ 'creator', 'nomenclature_territorial_level', 'nomenclature_financing_type', 'cor_af_actor', 'cor_af_actor.nomenclature_actor_role', 'cor_af_actor.organism', 'cor_af_actor.role', 'cor_volets_sinp', 'cor_objectifs', 'cor_territories', 't_datasets', 't_datasets.creator', 't_datasets.nomenclature_data_type', 't_datasets.cor_dataset_actor', 't_datasets.cor_dataset_actor.nomenclature_actor_role', 't_datasets.cor_dataset_actor.organism', 't_datasets.cor_dataset_actor.role', ], exclude=exclude) except ValueError as e: raise BadRequest(str(e)) user_cruved = cruved_scope_for_user_in_module( id_role=info_role.id_role, module_code="METADATA", )[0] af_schema.context = {'user_cruved': user_cruved} return af_schema.jsonify(af)
def create_acquisition_framework(info_role): """ Post one AcquisitionFramework data .. :quickref: Metadata; """ # create new acquisition_framework return AcquisitionFrameworkSchema().dump( acquisitionFrameworkHandler(request=request, acquisition_framework=TAcquisitionFramework(), info_role=info_role) )
def updateAcquisitionFramework(id_acquisition_framework, info_role): """ Post one AcquisitionFramework data for update acquisition_framework .. :quickref: Metadata; """ acquisition_framework = DB.session.query(TAcquisitionFramework).get(id_acquisition_framework) if not acquisition_framework: return {"message": "not found"}, 404 return AcquisitionFrameworkSchema().dump( acquisitionFrameworkHandler(request=request, acquisition_framework=acquisition_framework, info_role=info_role) )
def updateAcquisitionFramework(id_acquisition_framework, info_role): """ Post one AcquisitionFramework data for update acquisition_framework .. :quickref: Metadata; """ af = TAcquisitionFramework.query.get_or_404(id_acquisition_framework) if not af.has_instance_permission(scope=int(info_role.value_filter)): raise Forbidden(f"User {g.current_user} cannot update " "acquisition framework {af.id_acquisition_framework}") return AcquisitionFrameworkSchema().dump( acquisitionFrameworkHandler(request=request, acquisition_framework=af, info_role=info_role))
def updateAcquisitionFramework(id_acquisition_framework, info_role): """ Post one AcquisitionFramework data for update acquisition_framework .. :quickref: Metadata; """ acquisition_framework = DB.session.query(TAcquisitionFramework).get( id_acquisition_framework) if not acquisition_framework: return {"message": "not found"}, 404 try: return AcquisitionFrameworkSchema().dump( acquisitionFrameworkHandler( request=request, acquisition_framework=acquisition_framework, info_role=info_role)) except Exception as e: # retourne les erreurs levées en erreur 422 return e.args