def get_all(self): repository = ProfileSystemRepository() playload = Paginate().include_paginate_args_playload(request) profiles_systems = repository.get_all(playload) status_result = SetStatusResponseHandler() return status_result.default(profiles_systems)
def get_by_id(self, _id): contract = GetByIdProfileSystemAndDependenciesContract() if not (contract.validate(_id)): return ResultModel('Parametro incorreto.', False, contract.errors).to_dict(), 406 _id = int(_id) repo_prof_sys = ProfileSystemRepository() profile_system_db_result = repo_prof_sys.get_by_id(_id) if not profile_system_db_result['data']['result']['id']: return ResultModel('ID não encontrado.', False, contract.errors).to_dict(), 406 profile_system = profile_system_db_result['data']['result'] repo_prof_permi = ProfilePermissionRepository() _filter = dict(profile_system_id=_id) filter_dto = Paginate().include_paginate_args_playload( request, _filter) profile_permission = repo_prof_permi.get_search_by_params( filter_dto)['data']['result'] repo_user_prof_system = UserProfileSystemRepository() user_profile_system = repo_user_prof_system.get_search_by_params( filter_dto)['data']['result'] data = dict(profile_system=profile_system, profile_permission=profile_permission, user_profile_system=user_profile_system) result = ResultModel('Pesquisa realizada com sucesso.', data, False).to_dict() status_result = SetStatusResponseHandler() return status_result.default(data)
def create(self): contract = CreateProfileSystemContract() playload = request.json if not(contract.validate(playload)): return ResultModel('Problema nos parametros enviados.', False, contract.errors).to_dict(), 406 repository = ProfileSystemRepository() profile_system = repository.create(playload) status_result = SetStatusResponseHandler() return status_result.default(profile_system)
def create(self): contract = CreateProfileSystemAndDependenciesContract() playload = request.json if not (contract.validate(playload)): return ResultModel('Problema nos parametros enviados.', False, contract.errors).to_dict(), 406 system_id = playload.get('system_id') repo_prof_sys = ProfileSystemRepository() profile_system_dto = dict( system_id=system_id, name=playload.get('name_profile_system'), description=playload.get('description_profile_system')) profile_system_db_result = repo_prof_sys.create(profile_system_dto) status_result = SetStatusResponseHandler() if (not profile_system_db_result['data']['result']): return status_result.default(profile_system_db_result) profile_system = profile_system_db_result['data']['result'] profile_system_id = profile_system.get('id') system_permisions_ids = playload.get('system_permisions_ids') dto_multiple_profile_permission = dict( profile_system_id=profile_system_id, system_permisions_ids=system_permisions_ids) repo_prof_permis = ProfilePermissionRepository() prof_permis_db_result = repo_prof_permis.create( dto_multiple_profile_permission) if (not prof_permis_db_result['data']['result']): return status_result.default(prof_permis_db_result) profile_permission = prof_permis_db_result['data']['result'] repo_user_prof_sys = UserProfileSystemRepository() users_ids = playload.get('users_ids') user_profile_system_dto = dict(users_ids=users_ids, profile_system_id=profile_system_id) user_prof_sys_db_result = repo_user_prof_sys.create_multiples( user_profile_system_dto) if (not user_prof_sys_db_result['data']['result']): return status_result.default(user_prof_sys_db_result) user_profile_system = user_prof_sys_db_result['data']['result'] data = dict(profile_system=profile_system, profile_permission=profile_permission, user_profile_system=user_profile_system) result = ResultModel( 'Sucesso na criação do perfil de sistema e suas dependencias.', data, False).to_dict() return status_result.created(result)
def create(self, playload): try: user_id = playload.get('user_id') profile_system_id = playload.get('profile_system_id') user_id_exist = UserRepository().get_by_id(user_id) if not user_id_exist['data']['result']['id']: return ResultModel(f'O ID {user_id} de usuário não existe.', False, True).to_dict() profile_system_id_exist = ProfileSystemRepository().get_by_id( profile_system_id) if not profile_system_id_exist['data']['result']['id']: return ResultModel( f'O ID {profile_system_id} de perfil de sistema não existe.', False, True).to_dict() user_profile_system_exist = UserProfileSystem.query.filter_by( user_id=user_id, profile_system_id=profile_system_id).first() if user_profile_system_exist: return ResultModel(f'Esses dados já foram cadastrados.', False, True).to_dict() user_profile_system = UserProfileSystem(playload) db.session.add(user_profile_system) db.session.commit() data = marshal(user_profile_system, user_profile_system_fields) return ResultModel('Criado com sucesso.', data, False).to_dict() except Exception as e: return ResultModel('Não foi possivel criar.', False, True, str(e)).to_dict()
def create_multiples(self, playload): try: profile_system_id = playload.get('profile_system_id') users_ids = playload.get('users_ids') system_id = playload.get('system_id') exist_profile_system_id = ProfileSystemRepository().get_by_id( profile_system_id) if not exist_profile_system_id['data']['result']['id']: err_exist_ps_id = ResultErrorModel().add_error( 'profile_system_id', f'O ID {profile_system_id} não existe') return ResultModel(f'ID invalido.', False, err_exist_ps_id.errors).to_dict() exist_users = UserRepository().search_multiples_ids( {'ids': users_ids}) if len(exist_users['data']['result']) != len(users_ids): err_user = ResultErrorModel() users_invalid_ids = users_ids.copy() for user in exist_users['data']['result']: if user.get('id') in users_invalid_ids: users_invalid_ids.remove(user.get('id')) for invalid_id in users_invalid_ids: err_user.add_error('system_permision_id', f'O ID {invalid_id} não existe') return ResultModel(f'Dados invalidos.', False, err_user.errors).to_dict() users_is_registered = UserProfileSystem.query\ .filter(UserProfileSystem.user_id.in_(users_ids))\ .filter_by( system_id=system_id, profile_system_id=profile_system_id) if users_is_registered.count(): users_registered = users_is_registered.all() for user_registered in users_registered: if users_ids.__contains__(user_registered.user_id): users_ids.remove(user_registered.user_id) if not users_ids: return ResultModel( f'Usuarios já foram cadastrados anteriormente.', False, True).to_dict() data = [] for user_id in users_ids: new_user_profile_system = UserProfileSystem( dict(profile_system_id=profile_system_id, user_id=user_id, system_id=system_id)) db.session.add(new_user_profile_system) data.append(new_user_profile_system) db.session.flush() db.session.commit() data = marshal(data, user_profile_system_fields) return ResultModel('Sucesso ao inserir usuarios no perfil.', data, False).to_dict() except Exception as e: return ResultModel('Não foi possivel inserir usuarios no perfil.', False, True, str(e)).to_dict()
def get_by_params(self): contract = GetByParamsProfileSystemContract() playload = request.args if not(contract.validate(playload)): return ResultModel('Parametro incorreto.', False, contract.errors).to_dict(), 406 params_filter = {} _id = playload.get('id') name = playload.get('name') system_id = playload.get('system_id') if _id: params_filter['id'] = int(_id) if name: params_filter['name'] = name if system_id: params_filter['system_id'] = int(system_id) params_filter = Paginate().include_paginate_args_playload(request,params_filter) repository = ProfileSystemRepository() profile_system = repository.get_search_by_params(params_filter) status_result = SetStatusResponseHandler() return status_result.default(profile_system)