def query_user_list_by_keyword(cls, keyword, offset=0, limit=100): if keyword: return Accounts.select().where( Accounts.name.startswith(keyword) | Accounts.fuzzy_id.startswith(keyword)).limit(limit).offset( offset) else: return Accounts.select().limit(limit).offset(offset)
def account_list(cls, pin=None, _type=None, offset=0, page_size=30): if pin and _type: account_list = Accounts.select(Accounts, AuthUser).join( AuthUser, on=(Accounts.id == AuthUser.acc_id), attr='auth').where(Accounts.pin == pin, AuthUser.type == _type).order_by( Accounts.created_at.desc()).offset( offset).limit(page_size) elif _type: account_list = Accounts.select(Accounts, AuthUser).join( AuthUser, on=(Accounts.id == AuthUser.acc_id), attr='auth').where(AuthUser.type == _type).order_by( Accounts.created_at.desc()).offset(offset).limit(page_size) elif pin: account_list = Accounts.select().where( Accounts.pin == pin).order_by( Accounts.created_at.desc()).offset(offset).limit(page_size) else: account_list = Accounts.select(Accounts).order_by( Accounts.created_at.desc()).offset(offset).limit(page_size) # print("account_list:", account_list) rs = [] n = 0 acc_ids = [] for acc in account_list: acc_dict = Accounts.to_dict(acc) if hasattr(acc, 'auth') and acc.auth: auth_user_dict = AuthUser.to_dict(acc.auth, BASE_FIELDS) acc_dict['auth'] = auth_user_dict else: acc_ids.append(acc.id) if not acc.fuzzy_id: acc_dict['fuzzy_id'] = obfuscate_id(acc.id) rs.append(acc_dict) n = n + 1 au_list = AuthUser.select().where(AuthUser.acc_id.in_(acc_ids)) au_dict = {} if au_list: for au in au_list: au_dict[au.acc_id] = AuthUser.to_dict(au) for acc_dict in rs: if acc_dict['id'] in au_dict: acc_dict['auth'] = au_dict[acc_dict['id']] uoe_list = UserOrgExtend.select().where( UserOrgExtend.acc_id == acc_dict['id']) if uoe_list: acc_dict['extorgs'] = [uoe.org_id for uoe in uoe_list] ure_list = UserRoleExtend.select().where( UserRoleExtend.acc_id == acc_dict['id']) if ure_list: acc_dict['extroles'] = [ure.role_id for ure in ure_list] acc_dict.pop('id') return rs, n == page_size
def auth_user_role_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] 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) role_list = [] role_obj_list = Role.select() for r in role_obj_list: role_list.append(Role.to_dict(r, BASE_FIELDS)) auth_user_dict['roles'] = role_list return auth_user_dict
def account_by_passwd(cls, name, passwd): account_list = Accounts.select().where( (Accounts.name == name) | (Accounts.mobile_no == name), Accounts.password == passwd) print("account_list:", account_list) for acc in account_list: print("acc id:", acc.id) # return object_to_dict(acc) return acc return None
def default_guest_account(cls): guest: Accounts = Accounts.select().where( Accounts.name == "guest").first() if guest: if not guest.fuzzy_id: fuzzy_id = obfuscate_id(guest.id) guest.fuzzy_id = fuzzy_id with db: Accounts.update(fuzzy_id=fuzzy_id).where( Accounts.id == guest.id).execute() return guest
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 update_account(cls, acc_id, name, nickname, password, org_id, role_id, _type, extorgs, extroles, now, envelop_user_lambda: Callable[..., Tuple[str, dict]]): with db: update_params = dict(name=name, nickname=nickname, login_updated_at=now) if password: update_params['password'] = password user_token = None user_ext_dict = {} au_params = {} au: AuthUser = AuthUser.select().where( AuthUser.acc_id == acc_id).first() if not au: ur: UReference = UReference() ur.save(force_insert=True) au = AuthUser(acc_id=acc_id, org_id=org_id, role_id=role_id, ref_id=ur.id, type=_type) au.save(force_insert=True) if au.role_id != role_id: au_params['role_id'] = role_id if au.org_id != org_id: au_params['org_id'] = org_id if au.type != _type: au_params['type'] = _type if au_params: AuthUser.update(**au_params).where( AuthUser.acc_id == acc_id).execute() if extorgs: uoe_list = UserOrgExtend.select().where( UserOrgExtend.acc_id == acc_id, UserOrgExtend.org_id.not_in(extorgs)) del_uoe_ids = [uoe.org_id for uoe in uoe_list] if del_uoe_ids: UserOrgExtend.delete().where( UserOrgExtend.acc_id == acc_id, UserOrgExtend.org_id.in_(del_uoe_ids)) exc_uoe_list = UserOrgExtend.select().where( UserOrgExtend.acc_id == acc_id, UserOrgExtend.org_id.in_(extorgs)) exc_ext_org_ids = [uoe.org_id for uoe in exc_uoe_list] insert_ext_org_ids = extorgs if exc_ext_org_ids: insert_ext_org_ids = [ oid for oid in extorgs if oid not in exc_ext_org_ids ] for oid in insert_ext_org_ids: UserOrgExtend(acc_id=acc_id, org_id=oid).save(force_insert=True) if extroles: ure_list = UserRoleExtend.select().where( UserRoleExtend.acc_id == acc_id, UserRoleExtend.role_id.not_in(extroles)) del_ure_ids = [ure.role_id for ure in ure_list] if del_ure_ids: UserRoleExtend.delete().where( UserRoleExtend.acc_id == acc_id, UserRoleExtend.role_id.in_(del_ure_ids)) exc_ure_list = UserOrgExtend.select().where( UserOrgExtend.acc_id == acc_id, UserOrgExtend.org_id.in_(extorgs)) exc_ext_role_ids = [ure.role_id for ure in exc_ure_list] insert_ext_role_ids = extroles if exc_ext_role_ids: insert_ext_role_ids = [ rid for rid in extroles if rid not in exc_ext_role_ids ] for rid in insert_ext_role_ids: UserRoleExtend(acc_id=acc_id, role_id=rid).save(force_insert=True) if envelop_user_lambda: acc: Accounts = Accounts.select().where( Accounts.id == acc_id).first() user_token, user_ext_dict = envelop_user_lambda(acc) update_params['fuzzy_id'] = user_ext_dict['id'] update_params['login_token'] = user_token Accounts.update(**update_params).where( Accounts.id == acc_id).execute() return user_token, user_ext_dict
def check_user_only_by_name(cls, username): return Accounts.select().where(Accounts.name == username).exists()
def check_user(cls, username, mobile_no): return Accounts.select().where((Accounts.name == username) | ( Accounts.mobile_no == mobile_no)).exists()
def account_by_id(cls, pk_id) -> Accounts: return Accounts.select().where(Accounts.id == pk_id).first()
def account_by_name(cls, name): account_list = Accounts.select().where((Accounts.name == name) | (Accounts.mobile_no == name)) for acc in account_list: return acc return None