本文整理汇总了Python中assembl.models.User类的典型用法代码示例。如果您正苦于以下问题:Python User类的具体用法?Python User怎么用?Python User使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了User类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: admin_user
def admin_user(request, test_session):
"""A User fixture with R_SYSADMIN role"""
from assembl.models import User, Username, UserRole, Role
u = User(name=u"Mr. Administrator", type="user",
verified=True, last_assembl_login=datetime.utcnow())
u.username_p = "mr_admin_user"
from assembl.models import EmailAccount
account = EmailAccount(email="[email protected]", profile=u, verified=True)
test_session.add(u)
test_session.add(account)
r = Role.get_role(R_SYSADMIN, test_session)
ur = UserRole(user=u, role=r)
test_session.add(ur)
test_session.flush()
uid = u.id
def fin():
print "finalizer admin_user"
# I often get expired objects here, and I need to figure out why
user = test_session.query(User).get(uid)
user_role = user.roles[0]
test_session.delete(user_role)
test_session.delete(account)
test_session.delete(user.username)
test_session.delete(user)
test_session.flush()
request.addfinalizer(fin)
return u
示例2: put_global_roles_for_user
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)}
示例3: verify_password_change_token
def verify_password_change_token(token, max_age=None):
data, valid = verify_data_token(token, max_age=max_age)
if valid == Validity.BAD_HASH:
try:
data = int(data)
except:
return None, Validity.INVALID_FORMAT
user = User.get(data)
if not user:
return None, Validity.DATA_NOT_FOUND
password = user.password.decode('iso-8859-1') if user.password else 'empty'
data, valid = verify_data_token(token, password, max_age)
return user, valid
# Try decoding legacy
try:
id, hash = token.split('e', 1)
id = int(id)
user = User.get(id)
if not user:
return user, Validity.DATA_NOT_FOUND
age = datetime.utcnow() - user.last_login
if age > timedelta(days=3):
return user, Validity.EXPIRED
check = str(id) + user.last_login.isoformat()[:19]
valid = verify_password(
check, hash, HashEncoding.HEX)
if not valid:
return user, Validity.BAD_HASH
return user, Validity.VALID
except:
return None, Validity.INVALID_FORMAT
示例4: add_local_role
def add_local_role(request):
# Do not use check_permissions, this is a special case
ctx = request.context
user_id = request.authenticated_userid
if not user_id:
raise HTTPUnauthorized()
discussion_id = ctx.get_discussion_id()
discussion = Discussion.get(discussion_id)
user_uri = User.uri_generic(user_id)
if discussion_id is None:
raise HTTPBadRequest()
permissions = get_permissions(user_id, discussion_id)
json = request.json_body
if "discussion" not in json:
json["discussion"] = Discussion.uri_generic(discussion_id)
requested_user = json.get('user', None)
if not requested_user:
json['user'] = requested_user = user_uri
elif requested_user != user_uri and P_ADMIN_DISC not in permissions:
raise HTTPUnauthorized()
if P_ADMIN_DISC not in permissions:
if P_SELF_REGISTER in permissions:
json['requested'] = False
json['role'] = R_PARTICIPANT
req_user = User.get_instance(requested_user)
if not discussion.check_authorized_email(req_user):
raise HTTPForbidden()
elif P_SELF_REGISTER_REQUEST in permissions:
json['requested'] = True
else:
raise HTTPUnauthorized()
try:
instances = ctx.create_object("LocalUserRole", json, user_id)
except HTTPClientError as e:
raise e
except Exception as e:
raise HTTPBadRequest(e)
if instances:
first = instances[0]
db = first.db
for instance in instances:
db.add(instance)
db.flush()
# Side effect: materialize subscriptions.
if not first.requested:
# relationship may not be initialized
user = first.user or User.get(first.user_id)
user.get_notification_subscriptions(discussion_id, True)
# Update the user's AgentStatusInDiscussion
user.update_agent_status_subscribe(discussion)
view = request.GET.get('view', None) or 'default'
permissions = get_permissions(
user_id, ctx.get_discussion_id())
return CreationResponse(first, user_id, permissions, view)
示例5: add_local_role
def add_local_role(request):
# Do not use check_permissions, this is a special case
ctx = request.context
user_id = authenticated_userid(request)
if user_id == Everyone:
raise HTTPUnauthorized()
discussion_id = ctx.get_discussion_id()
user_uri = User.uri_generic(user_id)
if discussion_id is None:
raise HTTPBadRequest()
permissions = get_permissions(user_id, discussion_id)
json = request.json_body
if "discussion" not in json:
json["discussion"] = Discussion.uri_generic(discussion_id)
requested_user = json.get('user', None)
if not requested_user:
json['user'] = requested_user = user_uri
elif requested_user != user_uri and P_ADMIN_DISC not in permissions:
raise HTTPUnauthorized()
if P_ADMIN_DISC not in permissions:
if P_SELF_REGISTER in permissions:
json['requested'] = False
json['role'] = R_PARTICIPANT
elif P_SELF_REGISTER_REQUEST in permissions:
json['requested'] = True
else:
raise HTTPUnauthorized()
try:
instances = ctx.create_object("LocalUserRole", json, user_id)
except HTTPClientError as e:
raise e
except Exception as e:
raise HTTPBadRequest(e)
if instances:
first = instances[0]
db = first.db()
for instance in instances:
db.add(instance)
db.flush()
# Side effect: materialize subscriptions.
if not first.requested:
# relationship may not be initialized
user = first.user or User.get(first.user_id)
user.get_notification_subscriptions(discussion_id, True)
view = request.GET.get('view', None) or 'default'
permissions = get_permissions(
user_id, ctx.get_discussion_id())
return Response(
dumps(first.generic_json(view, user_id, permissions)),
location=first.uri_generic(first.id),
status_code=201)
示例6: finish_password_change
def finish_password_change(request):
logged_in = authenticated_userid(request)
if not logged_in:
raise HTTPUnauthorized()
user = User.get(logged_in)
localizer = request.localizer
discussion_slug = request.matchdict.get('discussion_slug', None)
error = None
p1, p2 = (request.params.get('password1', '').strip(),
request.params.get('password2', '').strip())
if p1 != p2:
error = localizer.translate(_('The passwords are not identical'))
elif p1:
user.password_p = p1
return HTTPFound(location=request.route_url(
'home' if discussion_slug else 'discussion_list',
discussion_slug=discussion_slug,
_query=dict(
message=localizer.translate(_(
"Password changed")))))
slug_prefix = "/" + discussion_slug if discussion_slug else ""
return dict(
get_default_context(request),
slug_prefix=slug_prefix, error=error)
示例7: discussion_list_view
def discussion_list_view(request):
user_id = authenticated_userid(request) or Everyone
user = None
if user_id != Everyone:
user = User.get(user_id)
roles = get_roles(user_id)
context = get_default_context(request)
context['discussions'] = []
#Show even anonymous users every discussion one has access to if
#authenticated, so they can login and read them
discussions = discussions_with_access(Authenticated if user_id == Everyone else user_id)
for discussion in discussions:
discussionFrontendUrls = FrontendUrls(discussion)
discussion_context = {
'topic': discussion.topic,
'slug': discussion.slug,
'url': discussionFrontendUrls.get_discussion_url()
}
if user_has_permission(discussion.id, user_id, P_ADMIN_DISC):
discussion_context['admin_url'] = discussionFrontendUrls.get_discussion_edition_url()
discussion_context['permissions_url'] = request.route_url(
'discussion_permissions', discussion_id=discussion.id)
context['discussions'].append(discussion_context)
if R_SYSADMIN in roles:
context['discussions_admin_url'] = request.route_url('discussion_admin')
context['permissions_admin_url'] = request.route_url('general_permissions')
context['user'] = user
return context
示例8: put_discussion_roles_for_user
def put_discussion_roles_for_user(request):
discussion_id = int(request.matchdict['discussion_id'])
discussion = Discussion.get_instance(discussion_id)
if not discussion:
raise HTTPNotFound("Discussion %d does not exist" % (discussion_id,))
user_id = request.matchdict['user_id']
user = User.get_instance(user_id)
if not user:
raise HTTPNotFound("User id %d does not exist" % (user_id,))
try:
data = json.loads(request.body)
except Exception as e:
raise HTTPBadRequest("Malformed Json")
session = Discussion.db()
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(Role.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_lu_roles = session.query(LocalUserRole).join(Role).filter(
user=user, discussion=discussion).all()
lur_by_role = {lur.role.name: lur for lur in known_lu_roles}
known_roles = set(lur_by_role.keys())
for role in known_roles - roles:
session.query.delete(lur_by_role[role])
for role in roles - known_roles:
session.add(LocalUserRole(
user=user, role=role, discussion=discussion))
return {"added": list(roles - known_roles),
"removed": list(known_roles - roles)}
示例9: maybe_merge
def maybe_merge(
backend, details, user=None, other_users=None,
*args, **kwargs):
# If we do not already have a user, see if we're in a situation
# where we're adding an account to an existing user, and maybe
# even merging
request = backend.strategy.request
adding_account = request.session.get("add_account", None)
if adding_account is not None:
del request.session["add_account"]
# current discussion and next?
logged_in = request.authenticated_userid
if logged_in:
logged_in = User.get(logged_in)
if adding_account:
if user and user != logged_in:
# logged_in presumably newer?
logged_in.merge(user)
logged_in.db.delete(user)
logged_in.db.flush()
user = logged_in
else:
forget(request)
if other_users:
if not user:
user = other_users.pop(0)
# Merge other accounts with same verified email
for profile in other_users:
user.merge(profile)
profile.delete()
return {"user": user}
示例10: participant1_user
def participant1_user(request, test_session, discussion):
from assembl.models import User, UserRole, Role
u = User(name=u"A. Barking Loon", type="user")
test_session.add(u)
r = Role.get_role(test_session, R_PARTICIPANT)
ur = UserRole(user=u, role=r)
test_session.add(ur)
u.subscribe(discussion)
test_session.flush()
def fin():
print "finalizer participant1_user"
test_session.delete(u)
test_session.flush()
request.addfinalizer(fin)
return u
示例11: widget_view
def widget_view(request):
# IF_OWNED not applicable for widgets... so far
ctx = request.context
user_id = authenticated_userid(request) or Everyone
permissions = get_permissions(
user_id, ctx.get_discussion_id())
check_permissions(ctx, user_id, permissions, CrudPermissions.READ)
view = (request.matchdict or {}).get('view', None)\
or ctx.get_default_view() or 'default'
json = ctx._instance.generic_json(view, user_id, permissions)
# json['discussion'] = ...
if user_id != Everyone:
user = User.get(user_id)
user_state = ctx._instance.get_user_state(user_id)
json['user'] = user.generic_json(view, user_id, permissions)
json['user_permissions'] = get_permissions(
user_id, ctx._instance.get_discussion_id())
if user_state is not None:
json['user_state'] = user_state
target_id = request.GET.get('target', None)
if target_id:
idea = Idea.get_instance(target_id)
if idea:
json['target'] = idea.generic_json(view, user_id, permissions)
else:
return HTTPNotFound("No idea "+target_id)
return json
示例12: voting_widget_view
def voting_widget_view(request):
user_id = authenticated_userid(request) or Everyone
ctx = request.context
view = (request.matchdict or {}).get('view', None)\
or ctx.get_default_view() or 'default'
widget = ctx._instance
permissions = get_permissions(
user_id, ctx.get_discussion_id())
json = widget.generic_json(view, user_id, permissions)
#json['discussion'] = ...
if user_id != Everyone:
user = User.get(user_id)
json['user'] = user.generic_json(view, user_id, permissions)
json['user_permissions'] = get_permissions(
user_id, widget.get_discussion_id())
user_state = widget.get_user_state(user_id)
if user_state is not None:
json['user_state'] = user_state
target_id = request.GET.get('target', None)
if target_id and Idea.get_database_id(target_id):
json['user_votes_url'] = widget.get_user_votes_url(target_id)
json['voting_urls'] = widget.get_voting_urls(target_id)
json['criteria'] = [idea.generic_json(view, user_id, permissions)
for idea in widget.criteria]
return json
示例13: votes_collection_add_json
def votes_collection_add_json(request):
ctx = request.context
user_id = authenticated_userid(request)
permissions = get_permissions(
user_id, ctx.get_discussion_id())
check_permissions(ctx, user_id, permissions, CrudPermissions.CREATE)
typename = ctx.collection_class.external_typename()
typename = request.json_body.get(
'@type', ctx.collection_class.external_typename())
json = request.json_body
json['voter'] = User.uri_generic(user_id)
try:
instances = ctx.create_object(typename, json, user_id)
except Exception as e:
raise HTTPBadRequest(e)
if instances:
first = instances[0]
db = first.db()
for instance in instances:
db.add(instance)
db.flush()
view = request.GET.get('view', None) or 'default'
return Response(
dumps(first.generic_json(view, user_id, permissions)),
location=first.uri_generic(first.id),
status_code=201)
示例14: set_local_role
def set_local_role(request):
# Do not use check_permissions, this is a special case
ctx = request.context
instance = ctx._instance
user_id = authenticated_userid(request)
if not user_id:
raise HTTPUnauthorized()
discussion_id = ctx.get_discussion_id()
user_uri = User.uri_generic(user_id)
if discussion_id is None:
raise HTTPBadRequest()
permissions = get_permissions(user_id, discussion_id)
json = request.json_body
requested_user = json.get('user', None)
if not requested_user:
json['user'] = requested_user = user_uri
elif requested_user != user_uri and P_ADMIN_DISC not in permissions:
raise HTTPUnauthorized()
if P_ADMIN_DISC not in permissions:
if P_SELF_REGISTER in permissions:
json['requested'] = False
json['role'] = R_PARTICIPANT
elif P_SELF_REGISTER_REQUEST in permissions:
json['requested'] = True
else:
raise HTTPUnauthorized()
updated = instance.update_from_json(json, user_id, ctx)
view = request.GET.get('view', None) or 'default'
if view == 'id_only':
return [updated.uri()]
else:
return updated.generic_json(view, user_id, permissions)
示例15: do_password_change
def do_password_change(request):
localizer = request.localizer
token = request.matchdict.get('ticket')
(verified, user_id) = verify_password_change_token(token, 24)
if not verified:
if not user_id:
raise HTTPBadRequest(localizer.translate(_(
"Wrong password token.")))
else:
return HTTPFound(location=maybe_contextual_route(
request, 'password_change_sent', profile_id=user_id, _query=dict(
sent=True, error=localizer.translate(_(
"This token is expired. "
"Do you want us to send another?")))))
user = User.get(user_id)
headers = remember(request, user_id)
request.response.headerlist.extend(headers)
user.last_login = datetime.utcnow()
slug = request.matchdict.get('discussion_slug', None)
slug_prefix = "/" + slug if slug else ""
return dict(
get_default_context(request),
slug_prefix=slug_prefix,
title=localizer.translate(_('Change your password')))