def query_org_list(cls, ids): data_list = Org.select().where(Org.id.in_(ids)) rs = [] for obj in data_list: obj_dict = Org.to_dict(obj, BASE_FIELDS) rs.append(obj_dict) return rs
def org_list(cls, offset=0, page_size=30, transfer_to_dict=True): org_list = Org.select().offset(offset).limit(page_size) rs = [] n = 0 for obj in org_list: if transfer_to_dict: obj_dict = Org.to_dict(obj) else: obj_dict = obj rs.append(obj_dict) n = n + 1 return rs, n == page_size
def new_org(cls, name, parent): data_item = Org(name=name, parent=parent) with db: data_item.save(force_insert=True) if parent and data_item.id: oo: OrgOrg = OrgOrg(org_id=data_item.id, parent=parent) oo.save(force_insert=True) oo_list = OrgOrg.select().where(OrgOrg.org_id == parent) for oo in oo_list: OrgOrg(org_id=data_item.id, parent=oo.parent).save(force_insert=True) return data_item.id
def update_org(cls, org_id, params): _params = {p: params[p] for p in params if p in Role.field_names()} with db: if 'parent' in params and params['parent']: o: Org = Org.select().where(Org.id == org_id).first() if o.parent != params['parent']: OrgOrg.delete().where(OrgOrg.org_id == org_id) oo: OrgOrg = OrgOrg(org_id=org_id, parent=params['parent']) oo.save(force_insert=True) oo_list = OrgOrg.select().where( OrgOrg.org_id == params['parent']) for oo in oo_list: OrgOrg(org_id=org_id, parent=oo.parent).save(force_insert=True) Org.update(**_params).where(Org.id == org_id).execute()
def auth_user_detail(cls, account_id) -> dict: acc: Accounts = Accounts.select(Accounts).where( Accounts.id == account_id).first() rs = AuthUser.select().where(AuthUser.acc_id == account_id) auth_user_dict = { 'acc': Accounts.to_dict( acc, BASE_FIELDS + [ "id", "password", "fuzzy_id", "login_token", "login_updated_at", "pin" ]) } if rs: au: AuthUser = rs[0] org: Org = Org.select().where(Org.id == au.org_id).first() auth_user_dict['org'] = { 'base': Org.to_dict(org, BASE_FIELDS), 'parent': { 'id': 0, 'name': '无' } } if org.parent: porg: Org = Org.select().where(Org.id == org.parent).first() auth_user_dict['org']['parent'] = porg ext_org_list = Org.select(Org).join( UserOrgExtend, on=(UserOrgExtend.org_id == Org.id)).where( UserOrgExtend.acc_id == au.acc_id) if ext_org_list: ext_org = auth_user_dict['org']['ext'] = [] for org in ext_org_list: ext_org.append(Org.to_dict(org, BASE_FIELDS)) auth_user_dict['au'] = { 'oid': au.org_id, 'rfid': au.ref_id, 'rid': au.role_id, 't': au.type } auth_user_dict['role'] = cls._get_roles(au) return auth_user_dict
def org_list(self, page): offset = int(page) * self.max_page_size org_list, has_next = AuthDao.org_list(offset, self.max_page_size, transfer_to_dict=False) org_dict_list = [Org.to_dict(o, BASE_FIELDS) for o in org_list] parent_map = {r['id']: r for r in org_dict_list} _ids = [] for o in org_dict_list: if o['parent'] in parent_map: o['p'] = parent_map[o['parent']] else: _ids.append(o['parent']) if _ids: p_list = AuthDao.query_org_list(_ids) p = {r['id']: r for r in p_list} for o in org_dict_list: if o['parent'] in p: o['p'] = p[o['parent']] return org_dict_list, has_next