本文整理汇总了Python中assembl.models.User.db方法的典型用法代码示例。如果您正苦于以下问题:Python User.db方法的具体用法?Python User.db怎么用?Python User.db使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类assembl.models.User
的用法示例。
在下文中一共展示了User.db方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: put_global_roles_for_user
# 需要导入模块: from assembl.models import User [as 别名]
# 或者: from assembl.models.User import db [as 别名]
def put_global_roles_for_user(request):
user_id = request.matchdict['user_id']
user = User.get_instance(user_id)
session = User.db()
if not user:
raise HTTPNotFound("User id %s does not exist" % (user_id,))
try:
data = json.loads(request.body)
except Exception as e:
raise HTTPBadRequest("Malformed Json")
if not isinstance(data, list):
raise HTTPBadRequest("Not a list")
if data and frozenset((type(x) for x in data)) != frozenset((str,)):
raise HTTPBadRequest("not strings")
roles = set(session.query(Role).filter(name in data).all())
data = set(data)
if len(roles) != len(data):
raise HTTPBadRequest("Not valid roles: %s" % (repr(
data - set((p.name for p in roles))),))
known_gu_roles = session.query(UserRole).join(Role).filter(
user=user).all()
gur_by_role = {gur.role.name: gur for gur in known_gu_roles}
known_roles = set(gur_by_role.keys())
for role in known_roles - roles:
session.query.delete(gur_by_role(role))
for role in roles - known_roles:
session.add(UserRole(user=user, role=role))
return {"added": list(roles - known_roles),
"removed": list(known_roles - roles)}
示例2: get_global_roles_for_user
# 需要导入模块: from assembl.models import User [as 别名]
# 或者: from assembl.models.User import db [as 别名]
def get_global_roles_for_user(request):
user_id = request.matchdict['user_id']
session = User.db()
if not session.query(User).get(user_id):
raise HTTPNotFound("User id %s does not exist" % (user_id,))
rolenames = session.query(Role.name).join(
UserRole).filter(UserRole.user_id == user_id)
return [x[0] for x in rolenames]