def get_upload(self, upload_id): """ This method retrieves uploads with given id from DB :return: Upload """ try: alchemy_upload = self.session.query(AlchemyUpload).filter_by(id=upload_id).first() if alchemy_upload is None: new_upload = None else: roles_list = list() for r in alchemy_upload.evidence.owner.roles: permissions_list = list() for p in r.permissions: new_permission = Permission(p.id, p.name, p.description) permissions_list.append(new_permission) new_role = Role(r.id, r.name, r.description, permissions_list) roles_list.append(new_role) new_user = User(alchemy_upload.evidence.owner.id, alchemy_upload.evidence.owner.username, alchemy_upload.evidence.owner.password, alchemy_upload.evidence.owner.first_name, alchemy_upload.evidence.owner.family_name, alchemy_upload.evidence.owner.email, roles_list) new_evidence = Evidence(alchemy_upload.evidence.id, alchemy_upload.evidence.alias, alchemy_upload.evidence.size, new_user) new_upload = Upload(alchemy_upload.id, alchemy_upload.path, alchemy_upload.size, alchemy_upload.type, new_evidence) return new_upload except Exception as e: raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
def get_all_files(self, upload_id): """ This method retrieves all files from DB that belongs to upload_id :return: {File} """ try: alchemy_file_list = self.session.query(AlchemyFile).filter_by(upload_id=upload_id).all() file_list = list() for f in alchemy_file_list: roles_list = list() for r in f.upload.evidence.owner.roles: permission_list = list() for p in r.permissions: new_permission = Permission(p.id, p.name, p.description) permission_list.append(new_permission) new_role = Role(r.id, r.name, r.description, permission_list) roles_list.append(new_role) new_owner = User(f.upload.evidence.owner.id, f.upload.evidence.owner.username, f.upload.evidence.owner.password, f.upload.evidence.owner.first_name, f.upload.evidence.owner.family_name, f.upload.evidence.owner.email, roles_list) new_evidence = Evidence(f.upload.evidence.id, f.upload.evidence.alias, f.upload.evidence.size, new_owner) new_upload = Upload(f.upload.id, f.upload.path, f.upload.size, f.upload.type, new_evidence) new_file = File(f.id, f.path, f.size, f.name, f.hash_md5, f.hash_sha256, new_upload) file_list.append(new_file) return file_list except Exception as e: raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
def get_all_uploads(self, evidence_id): """ This method retrieves all uploads from DB that belongs to evidence_id :return: {Upload} """ try: alchemy_upload_list = self.session.query(AlchemyUpload).filter_by(evidence_id=evidence_id).all() upload_list = list() for u in alchemy_upload_list: roles_list = list() for r in u.evidence.owner.roles: permission_list = list() for p in r.permissions: new_permission = Permission(p.id, p.name, p.description) permission_list.append(new_permission) new_role = Role(r.id, r.name, r.description, permission_list) roles_list.append(new_role) new_user = User(u.evidence.owner.id, u.evidence.owner.username, u.evidence.owner.password, u.evidence.owner.first_name, u.evidence.owner.family_name, u.evidence.owner.email, roles_list) new_evidence = Evidence(u.evidence.id, u.evidence.alias, u.evidence.size, new_user) new_upload = Upload(u.id, u.path, u.size, u.type, new_evidence) upload_list.append(new_upload) return upload_list except Exception as e: raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
def get_all_evidences(self): """ This method retrieves all evidences from DB :return: {Evidence} """ try: alchemy_evidences_list = self.session.query(AlchemyEvidence).all() evidence_list = list() for e in alchemy_evidences_list: roles_list = list() for r in e.owner.roles: permission_list = list() for p in r.permissions: new_permission = Permission(p.id, p.name, p.description) permission_list.append(new_permission) new_role = Role(r.id, r.name, r.description, permission_list) roles_list.append(new_role) new_user = User(e.owner.id, e.owner.username, e.owner.password, e.owner.first_name, e.owner.family_name, e.owner.email, roles_list) new_evidence = Evidence(e.id, e.alias, e.size, new_user) evidence_list.append(new_evidence) return evidence_list except Exception as e: raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
def create_user(self, username, password, first_name, family_name, email, roles): """ This method inserts a new alchemy user in DB :return: User, Boolean """ try: alchemy_user = AlchemyUser(username, password, first_name, family_name, email) alchemy_roles_list = list() for r in roles: alchemy_role = self.alchemy_get_role(r.id) alchemy_roles_list.append(alchemy_role) alchemy_user.roles = alchemy_roles_list self.session.add(alchemy_user) self.session.commit() user = User(alchemy_user.id, alchemy_user.username, alchemy_user.password, alchemy_user.first_name, alchemy_user.family_name, alchemy_user.email, roles) return user, True except Exception: return None, False
def get_user(self, user_id): """ This method retrieves users with given id from DB :return: User """ try: alchemy_user = self.session.query(AlchemyUser).filter_by(id=user_id).first() if alchemy_user is None: new_user = None else: role_list = list() for r in alchemy_user.roles: permissions_list = list() for p in r.permissions: new_permission = Permission(p.id, p.name, p.description) permissions_list.append(new_permission) new_role = Role(r.id, r.name, r.description, permissions_list) role_list.append(new_role) new_user = User(alchemy_user.id, alchemy_user.username, alchemy_user.password, alchemy_user.first_name, alchemy_user.family_name, alchemy_user.email, role_list) return new_user except Exception as e: raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))
def get_all_users(self): """ This method retrieves all users from DB :return: {User} """ try: alchemy_users_list = self.session.query(AlchemyUser).all() user_list = list() for u in alchemy_users_list: role_list = list() for r in u.roles: permission_list = list() for p in r.permissions: new_permission = Permission(p.id, p.name, p.description) permission_list.append(new_permission) role = Role(r.id, r.name, r.description, permission_list) role_list.append(role) new_user = User(u.id, u.username, u.password, u.first_name, u.family_name, u.email, role_list) user_list.append(new_user) return user_list except Exception as e: raise AppException(GenericErrorMessages.DATABASE_ERROR + ': ' + str(e))