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

View file

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