Beispiel #1
0
 def save(self):
     """
     :rtype: VoidEntity
     :return: VoidEntity
     """
     Session.add(self)
     return self
Beispiel #2
0
 def add_role(user_fk, role_fk):
     po = UserRoleHeader(user_fk=user_fk, role_fk=role_fk)
     Session.add(po)
     permits = Session.query(Permission.code).\
         join(RolePermissionHeader).\
         filter(RolePermissionHeader.role_fk == role_fk).all()
     if permits:
         redis.sadd('user_permits:%s' % user_fk, *[e[0] for e in permits])
Beispiel #3
0
 def erase(self):
     old_parent = self.parent
     if old_parent:
         cnt = Session.query(func.count(ProductCategory.id)) \
             .filter(ProductCategory.parent_fk == old_parent.id).one()[0]
         if cnt <= 1:
             old_parent.leaf = True
             Session.add(old_parent)
     super().erase()
Beispiel #4
0
def get_user_permission_list(pk):
    start = Session.query(Permission) \
        .join(RolePermissionHeader, Permission.id == RolePermissionHeader.permission_fk)\
        .join(UserRoleHeader, RolePermissionHeader.role_fk == UserRoleHeader.role_fk) \
        .filter(UserRoleHeader.user_fk == pk) \
        .group_by(Permission.id).cte(recursive=True)
    nested = start.union(Session.query(Permission)
                         .filter(Permission.id == start.c.parent_fk))
    return Session.query(*nested.c, label('expanded', text('True'))).all()
Beispiel #5
0
 def remove_role(user_fk, role_fk):
     po = Session.query(UserRoleHeader)\
         .filter(UserRoleHeader.user_fk == user_fk, UserRoleHeader.role_fk == role_fk).one()
     q1 = Session.query(Permission.code)\
         .join(RolePermissionHeader)\
         .filter(RolePermissionHeader.role_fk == role_fk)
     q2 = Session.query(Permission.code).join(RolePermissionHeader, UserRoleHeader)\
         .filter(UserRoleHeader.user_fk == user_fk, RolePermissionHeader.role_fk != role_fk)
     to_remove = q1.except_(q2).all()
     if to_remove:
         redis.srem('user_permits:%s' % user_fk, *[e[0] for e in to_remove])
     Session.delete(po)
Beispiel #6
0
def get_role_permission_tree(pk):
    checked = ctx.request.args['checked']
    if checked == '1':
        header = aliased(RolePermissionHeader)
        return Session.query(*inspect(Permission).c,
                             case([(header.role_fk.is_(None), False)], else_=True).label('checked')) \
            .outerjoin(header, (header.permission_fk == Permission.id) & (header.role_fk == pk)) \
            .all()
    else:
        start = Session.query(Permission) \
            .join(RolePermissionHeader) \
            .filter(RolePermissionHeader.role_fk == pk) \
            .group_by(Permission.id) \
            .cte(recursive=True)
        nested = start.union(Session.query(Permission)
                             .filter(Permission.id == start.c.parent_fk))
        return Session.query(*nested.c, label('expanded', text('True'))).all()
Beispiel #7
0
 def load(cls, pk):
     """
     :rtype: VoidEntity
     :type pk: str
     :param pk: str
     :return: VoidEntity
     """
     return Session.query(cls).get(pk)
Beispiel #8
0
 def load_by_fkid(fkid):
     """
     :type fkid str
     :rtype list[Attachment]
     :param fkid:
     :return:
     """
     return Session.query(Attachment.id, Attachment.fkid, Attachment.fname, Attachment.upload_date)\
         .filter(Attachment.fkid == fkid).order_by(Attachment.upload_date.desc()).all()
Beispiel #9
0
    def tree(cls, node=None):
        """

        :rtype: list[VoidEntity]
        :type node: str
        :param node:
        :return:
        """
        return Session.query(cls).filter(cls.parent_fk == node).all()
Beispiel #10
0
 def _update_leaf(self):
     clz = self.__class__
     tbl = clz.__table__
     # use execute instead of query, because the session may issue an update command before query method
     if not self.phantom:
         old_parent_fk = Session.execute(select([tbl.c.parent_fk]).where(tbl.c.id == self.id)).first()[0]
         if old_parent_fk == self.parent_fk:
             return
         if old_parent_fk:
             cnt = Session.query(func.count(clz.id)).filter(clz.parent_fk == old_parent_fk).one()[0]
             if cnt <= 1:
                 stmt = tbl.update().values(leaf=True).where((tbl.c.id == old_parent_fk) & (tbl.c.leaf == False))
                 Session.execute(stmt)
     if self.parent_fk:
         stmt = tbl.update().values(leaf=False).where((tbl.c.id == self.parent_fk) & (tbl.c.leaf == True))
         Session.execute(stmt)
Beispiel #11
0
    def set_parent(self, parent=None):
        """

        :param {str|ProductCategory|None} parent: parent_fk or parent; None to remove parent
        :return:
        """
        if parent:
            if isinstance(parent, str):
                if parent == self.id:
                    raise BusinessException('parent should not be oneself')
                parent = self.__class__.load(parent)
                if not parent:
                    raise EntityNotFound('%s#%s' % (self.__class__.name, parent))
            if parent.id == self.id:
                raise BusinessException('parent should not be oneself')
            old_parent = self.parent
            if parent == old_parent:
                return
            if old_parent:
                cnt = Session.query(func.count(ProductCategory.id))\
                    .filter(ProductCategory.parent_fk == old_parent.id).one()[0]
                if cnt <= 1:
                    old_parent.leaf = True
                    Session.add(old_parent)
            parent.leaf = False
            self.parent = parent
            self._update_fullname()
        else:
            old_parent = self.parent
            if old_parent:
                cnt = Session.query(func.count(ProductCategory.id)) \
                    .filter(ProductCategory.parent_fk == old_parent.id).one()[0]
                if cnt <= 1:
                    old_parent.leaf = True
                    Session.add(old_parent)
            self.parent = None
            self._update_fullname()
Beispiel #12
0
def get_permission_list():
    return Session.query(Permission).all()
Beispiel #13
0
def get_duty_chain_list():
    return Session.query(DutyChain).all()
Beispiel #14
0
 def update_permissions(pk, *permissions):
     Session.query(RolePermissionHeader).filter(RolePermissionHeader.role_fk == pk).delete(synchronize_session=False)
     Session.bulk_insert_mappings(RolePermissionHeader, [{'role_fk': pk, 'permission_fk': e} for e in permissions])
Beispiel #15
0
def get_product_category_tree():
    return Session.query(ProductCategory).all()
Beispiel #16
0
def get_organization_tree():
    return Session.query(Organization).all()
Beispiel #17
0
def get_users():
    return Session.query(User).order_by(User.id.asc()).all()
Beispiel #18
0
def get_role_list():
    return Session.query(Role).all()
Beispiel #19
0
def update_role_permission(pk):
    vo = ctx.request.json
    permissions = vo['permissions']
    if permissions:
        Session.query(RolePermissionHeader).filter(RolePermissionHeader.role_fk == pk).delete(synchronize_session=False)
        Session.bulk_insert_mappings(RolePermissionHeader, [{'role_fk': pk, 'permission_fk': e} for e in permissions])
Beispiel #20
0
 def erase(self):
     Session.delete(self)
Beispiel #21
0
def get_user_role_list(pk):
    hdr = aliased(UserRoleHeader)
    rv = Session.query(*inspect(Role).c, case([(hdr.role_fk.is_(None), False)], else_=True).label('checked')) \
        .outerjoin(hdr, (hdr.role_fk == Role.id) & (hdr.user_fk == pk)).all()
    return rv