Add is_org_member to the prototype_view. Fix some line length problems. Fix a bug with using uuid objects as strings.

This commit is contained in:
yackob03 2014-01-21 17:32:49 -05:00
parent 58412ff6b3
commit 7ceb249858
2 changed files with 52 additions and 36 deletions

View file

@ -682,9 +682,9 @@ def delete_prototype_permission(org, uid):
def get_prototype_permission(org, uid): def get_prototype_permission(org, uid):
found = None
try: try:
return PermissionPrototype.get(PermissionPrototype.org == org, PermissionPrototype.uuid == uid) return PermissionPrototype.get(PermissionPrototype.org == org,
PermissionPrototype.uuid == uid)
except PermissionPrototype.DoesNotExist: except PermissionPrototype.DoesNotExist:
return None return None
@ -692,12 +692,17 @@ def get_prototype_permission(org, uid):
def get_prototype_permissions(org): def get_prototype_permissions(org):
ActivatingUser = User.alias() ActivatingUser = User.alias()
DelegateUser = User.alias() DelegateUser = User.alias()
where = PermissionPrototype.select().where(PermissionPrototype.org == org) query = (PermissionPrototype
join1 = where.join(ActivatingUser, JOIN_LEFT_OUTER, on=(ActivatingUser.id == PermissionPrototype.activating_user)) .select()
join2 = join1.join(DelegateUser, JOIN_LEFT_OUTER, on=(DelegateUser.id == PermissionPrototype.delegate_user)) .where(PermissionPrototype.org == org)
join3 = join2.join(Team, JOIN_LEFT_OUTER, on=(Team.id == PermissionPrototype.delegate_team)) .join(ActivatingUser, JOIN_LEFT_OUTER,
join4 = join3.join(Role, JOIN_LEFT_OUTER, on=(Role.id == PermissionPrototype.role)) on=(ActivatingUser.id == PermissionPrototype.activating_user))
return join4 .join(DelegateUser, JOIN_LEFT_OUTER,
on=(DelegateUser.id == PermissionPrototype.delegate_user))
.join(Team, JOIN_LEFT_OUTER,
on=(Team.id == PermissionPrototype.delegate_team))
.join(Role, JOIN_LEFT_OUTER, on=(Role.id == PermissionPrototype.role)))
return query
def update_prototype_permission(org, uid, role_name): def update_prototype_permission(org, uid, role_name):
@ -711,10 +716,12 @@ def update_prototype_permission(org, uid, role_name):
return found return found
def add_prototype_permission(org, role_name, activating_user, delegate_user=None, delegate_team=None): def add_prototype_permission(org, role_name, activating_user,
delegate_user=None, delegate_team=None):
new_role = Role.get(Role.name == role_name) new_role = Role.get(Role.name == role_name)
uid = uuid.uuid4() uid = str(uuid.uuid4())
return PermissionPrototype.create(org=org, uuid=uid, role=new_role, activating_user=activating_user, return PermissionPrototype.create(org=org, uuid=uid, role=new_role,
activating_user=activating_user,
delegate_user=delegate_user, delegate_team=delegate_team) delegate_user=delegate_user, delegate_team=delegate_team)

View file

@ -541,25 +541,25 @@ def change_organization_details(orgname):
abort(403) abort(403)
def prototype_view(p): def prototype_view(proto, org_members):
def user_view(u): if proto.delegate_user:
return { delegate_view = {
'name': u.username, 'name': proto.delegate_user.username,
'is_robot': u.robot, 'is_robot': proto.delegate_user.robot,
'kind': 'user' 'kind': 'user',
'is_org_member': proto.delegate_user.username in org_members,
} }
else:
def team_view(t): delegate_view = {
return { 'name': proto.delegate_team.name,
'name': t.name, 'kind': 'team',
'kind': 'team'
} }
return { return {
'activating_user': user_view(p.activating_user), 'activating_user': user_view(proto.activating_user),
'delegate': user_view(p.delegate_user) if p.delegate_user else team_view(p.delegate_team), 'delegate': delegate_view,
'role': p.role.name, 'role': proto.role.name,
'id': p.uuid 'id': proto.uuid,
} }
@app.route('/api/organization/<orgname>/prototypes', methods=['GET']) @app.route('/api/organization/<orgname>/prototypes', methods=['GET'])
@ -573,7 +573,9 @@ def get_organization_prototype_permissions(orgname):
abort(404) abort(404)
permissions = model.get_prototype_permissions(org) permissions = model.get_prototype_permissions(org)
return jsonify({'prototypes': [prototype_view(p) for p in permissions]}) org_members = model.get_organization_member_set(orgname)
return jsonify({'prototypes': [prototype_view(p, org_members)
for p in permissions]})
abort(403) abort(403)
@ -619,8 +621,10 @@ def create_organization_prototype_permission(orgname):
delegate_teamname = delegate_name if delegate_kind == 'team' else None delegate_teamname = delegate_name if delegate_kind == 'team' else None
activating_user = model.get_user(activating_username) activating_user = model.get_user(activating_username)
delegate_user = model.get_user(delegate_username) if delegate_username else None delegate_user = (model.get_user(delegate_username)
delegate_team = model.get_organization_team(orgname, delegate_teamname) if delegate_teamname else None if delegate_username else None)
delegate_team = (model.get_organization_team(orgname, delegate_teamname)
if delegate_teamname else None)
if not activating_user: if not activating_user:
abort(404) abort(404)
@ -630,14 +634,17 @@ def create_organization_prototype_permission(orgname):
role_name = details['role'] role_name = details['role']
prototype = model.add_prototype_permission(org, role_name, activating_user, delegate_user, delegate_team) prototype = model.add_prototype_permission(org, role_name, activating_user,
delegate_user, delegate_team)
log_prototype_action('create_prototype_permission', orgname, prototype) log_prototype_action('create_prototype_permission', orgname, prototype)
return jsonify(prototype_view(prototype)) org_members = model.get_organization_member_set(orgname)
return jsonify(prototype_view(prototype, org_members))
abort(403) abort(403)
@app.route('/api/organization/<orgname>/prototypes/<prototypeid>', methods=['DELETE']) @app.route('/api/organization/<orgname>/prototypes/<prototypeid>',
methods=['DELETE'])
@api_login_required @api_login_required
def delete_organization_prototype_permission(orgname, prototypeid): def delete_organization_prototype_permission(orgname, prototypeid):
permission = AdministerOrganizationPermission(orgname) permission = AdministerOrganizationPermission(orgname)
@ -658,7 +665,8 @@ def delete_organization_prototype_permission(orgname, prototypeid):
abort(403) abort(403)
@app.route('/api/organization/<orgname>/prototypes/<prototypeid>', methods=['PUT']) @app.route('/api/organization/<orgname>/prototypes/<prototypeid>',
methods=['PUT'])
@api_login_required @api_login_required
def update_organization_prototype_permission(orgname, prototypeid): def update_organization_prototype_permission(orgname, prototypeid):
permission = AdministerOrganizationPermission(orgname) permission = AdministerOrganizationPermission(orgname)
@ -678,9 +686,10 @@ def update_organization_prototype_permission(orgname, prototypeid):
if not prototype: if not prototype:
abort(404) abort(404)
log_prototype_action('modify_prototype_permission', orgname, prototype, original_role = existing.role.name) log_prototype_action('modify_prototype_permission', orgname, prototype,
original_role=existing.role.name)
return jsonify(prototype_view(prototype)) org_members = model.get_organization_member_set(orgname)
return jsonify(prototype_view(prototype, org_members))
abort(403) abort(403)