def create_project(cls, name=None, created=None, update=None, active=True, raiseFlag = True): """ Creates project with given parameters. :param cls: Project :param name: Name of project (unique) :param created: Timestamp of creation :param update: Timestamp of last update :param active: Used to deactivate/delete project (Bool) :param raiseFlag: If True and name is already taken raises DbException """ p = None if (cls.get_project_by_name(name,False) != None): if(raiseFlag): raise DbException(400, "Name is already taken.") return p if active == None: active=True p = Project(name, created, update, active) db.session.add(p) db.session.commit() try: os.mkdir(os.path.dirname(__file__) + '/../../data/module/' + str(p.id)) except FileExistsError: return p return p
def get_project_all(cls, raiseFlag = True): """ Returns all projects. :param cls: Project :param raiseFlag: If True and Project not found raises DbException """ result = cls.query.all() if (result == None) & raiseFlag: raise DbException(404, "Project not found.") return result
def get_project_perm_all(cls, raiseFlag=True): """ Returns all ProjectPerm. :param cls: ProjectPerm :param raiseFlag: If True and permission not found raises DbException. """ perm = cls.query.all() if (perm == None) & raiseFlag: raise DbException(DbException, 404, "Permission not found.") return perm
def get_module_perm_by_id(cls, permId, raiseFlag=True): """ Returns module found by ID. :param cls: ModulePerm :param permId: ID of ModulePerm class :param raiseFlag: If True and permission not found raises DbException """ perm = cls.query.filter_by(id=permId).first() if (perm == None) & raiseFlag: raise DbException(404, "Permission not found.") return perm
def get_module_perm_all_by_module_id(cls, permId, raiseFlag=True): """ Returns all permissions for one module. :param cls: ModulePerm :param permId: ID of Module :param raiseFlag: If True and permission not found raises DbException """ perm = cls.query.filter_by(module_id=permId).all() if (perm == None or perm == []) & raiseFlag: raise DbException(404, "Permission not found.") return perm
def get_project_by_name(cls, projectName, raiseFlag = True): """ Deactivates project found by ID. :param cls: Project :param projectName: :param raiseFlag: If True and Project not found raises DbException """ result = cls.query.filter_by(name=projectName).first() if (result == None) & raiseFlag: raise DbException(404, "Project not found.") return result
def get_project_by_id(cls, projectId, raiseFlag = True): """ Returns project found by ID. :param cls: Project :param projectId: :param raiseFlag: If True and Project not found raises DbException """ result = cls.query.filter_by(id=projectId).first() if (result == None) & raiseFlag: raise DbException(404, "Project not found.") return result
def get_package_by_id(cls, packageId, raiseFlag=True): """ Return package by its id, if raise flag is True, excetion is raised when not found :param cls: :param packageId: :param raiseFlag=True: raise flag, if its True """ package = Package.query.filter_by(id=packageId).first() if (package is None) and raiseFlag: raise DbException(404, "Package with id: %r not found"%(packageId)) return package
def get_project_perm_all_by_project_id(cls, permId, raiseFlag=True): """ Returns all ProjectPerm for one project. :param cls: ProjectPerm :param permId: ID of project :param raiseFlag: If True and permission not found raises DbException. """ perm = cls.query.filter_by(project_id=permId).all() if (perm == None) & raiseFlag: raise DbException(DbException, 404, "Permission not found.") return perm
def get_project_perm_by_id(cls, permId, raiseFlag=True): """ Returns ProjectPerm found by ID. :param cls: ProjectPerm :param permId: :param raiseFlag: If True and permission not found raises DbException. """ perm = cls.query.filter_by(id=permId).first() if (perm == None) & raiseFlag: raise DbException(DbException, 404, "Permission not found.") return perm
def get_module_perm_all(cls, raiseFlag=True): """ Returns all permissions for all modules. :param cls: ModulePerm :param user_id: ID of User :param module_id: ID of Module :param raiseFlag: If True and permission not found raises DbException """ perm = cls.query.all() if (perm == None or perm == []) & raiseFlag: raise DbException(404, "Permission not found.") return perm
def get_module_perm_by_user_and_module_id(cls, user_id, module_id, raiseFlag=True): """ Returns module permission found by user_id and module_id. :param cls: ModulePerm :param user_id: ID of User :param module_id: ID of Module :param raiseFlag: If True and permission not found raises DbException """ perm = cls.query.filter_by(module_id=module_id).filter_by( user_id=user_id).first() if (perm == None) & raiseFlag: raise DbException(404, "Permission not found.") return perm
def get_project_perm_by_user_and_project_id(cls, user_id, project_id, raiseFlag=True): """ Returns ProjectPerm found by user id and project id. :param cls: ProjectPerm :param user_id: :param project_id: :param raiseFlag: If True and permission not found raises DbException. """ perm = cls.query.filter_by(project_id=project_id).filter_by( user_id=user_id).first() if (perm == None) & raiseFlag: raise DbException(DbException, 404, "Permission not found.") return perm
def check_perm_read_include_project_perm(cls, user_id, module_id, raiseFlag=True): """ Checks read permission including project permissions. :param cls: ModulePerm :param user_id: :param module_id: :param raiseFlag: If True and permission not found or insufficient raises DbException """ if ((not ModulePerm.check_perm_read_by_user_and_module_id( user_id, module_id)) or (not ProjectPerm.check_perm_read_by_user_and_project_id( user_id, Module.get_module_by_id(module_id).project_id))): if raiseFlag: raise DbException(405, "Insufficient permission.") return False return True
def give_perm_own_guarded(cls, master_id, user_id, project_id, raiseFlag=True): """ Gives user own permission to project. :param cls: ProjectPerm :param master_id: ID of user giving the permission :param user_id: ID of user geting permission :param project_id: ID of project that will be accessed :param raiseFlag: If True and permission not found raises DbException """ if not (ProjectPerm.check_perm_own_by_user_and_project_id( master_id, project_id)): if raiseFlag: raise DbException(405, "Insufficient permission.") return False return ProjectPerm.give_perm_own_user_project_id_raw( user_id, project_id, raiseFlag)
def give_perm_own_guarded(cls, master_id, user_id, module_id, raiseFlag=True): """ Gives user read permission to module. :param cls: ModulePerm :param master_id: ID of user giving the permission :param user_id: ID of user geting permission :param module_id: ID of module that will be accessed :param raiseFlag: If True and permission not found raises DbException """ if not (ModulePerm.check_perm_own_by_user_and_module_id( master_id, module_id)): if raiseFlag: raise DbException(405, "Insufficient permission.") return False return ModulePerm.give_perm_own_user_module_id_raw( user_id, module_id, raiseFlag)
def update_project_by_id(cls, id, name=None, created=None, update=None, active=None, raiseFlag = True): """ Updates project with given parameters. :param cls: Project :param id: ID of project you want to update :param name: Name of project (unique) :param created: Timestamp of creation :param update: Timestamp of last update (if not set will be now) :param active: Used to deactivate/delete project (Bool) :param raiseFlag: If True and name is already taken raises DbException """ p = cls.get_project_by_id(id,raiseFlag) if p==None: return p changed = 0 if (name != None): checkName = cls.get_project_by_name(name,False) if (checkName != None and checkName.id!=p.id): if(raiseFlag): raise DbException(400, "Name is already taken.") return None p.name = name changed = 1 if (created != None): p.created = created changed = 1 if (active != None): p.active = active changed = 1 if (update != None): p.update = update db.session.commit() else: if (changed == 1): p.update = datetime.datetime.utcnow() db.session.commit() return p