diff --git a/endpoints/api.py b/endpoints/api.py index 8f93bd0d8..e3f2dab82 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -1,12 +1,11 @@ import logging import stripe -import re import requests import urlparse import json from flask import request, make_response, jsonify, abort -from flask.ext.login import login_required, current_user, logout_user +from flask.ext.login import current_user, logout_user from flask.ext.principal import identity_changed, AnonymousIdentity from functools import wraps from collections import defaultdict @@ -41,7 +40,8 @@ def api_login_required(f): if not current_user.is_authenticated(): abort(401) - if current_user and current_user.db_user() and current_user.db_user().organization: + if (current_user and current_user.db_user() and + current_user.db_user().organization): abort(401) return f(*args, **kwargs) @@ -144,7 +144,7 @@ def convert_user_to_organization(): def change_user_details(): user = current_user.db_user() - user_data = request.get_json(); + user_data = request.get_json() try: if 'password' in user_data: @@ -264,7 +264,7 @@ def get_matching_entities(prefix): if permission.can(): try: organization = model.get_organization(organization_name) - except: + except model.InvalidOrganizationException: pass if organization: @@ -272,7 +272,7 @@ def get_matching_entities(prefix): users = model.get_matching_users(prefix, organization) - def team_view(team): + def entity_team_view(team): result = { 'name': team.name, 'kind': 'team', @@ -291,20 +291,20 @@ def get_matching_entities(prefix): return user_json - team_data = [team_view(team) for team in teams] + team_data = [entity_team_view(team) for team in teams] user_data = [user_view(user) for user in users] return jsonify({ 'results': team_data + user_data }) -def team_view(orgname, t): - view_permission = ViewTeamPermission(orgname, t.name) - role = model.get_team_org_role(t).name +def team_view(orgname, team): + view_permission = ViewTeamPermission(orgname, team.name) + role = model.get_team_org_role(team).name return { - 'id': t.id, - 'name': t.name, - 'description': t.description, + 'id': team.id, + 'name': team.name, + 'description': team.description, 'can_view': view_permission.can(), 'role': role } @@ -317,8 +317,9 @@ def create_organization_api(): existing = None try: - existing = model.get_organization(org_data['name']) or model.get_user(org_data['name']) - except: + existing = (model.get_organization(org_data['name']) or + model.get_user(org_data['name'])) + except model.InvalidOrganizationException: pass if existing: @@ -329,8 +330,8 @@ def create_organization_api(): return error_resp try: - organization = model.create_organization(org_data['name'], org_data['email'], - current_user.db_user()) + model.create_organization(org_data['name'], org_data['email'], + current_user.db_user()) return make_response('Created', 201) except model.DataModelException as ex: error_resp = jsonify({ @@ -345,15 +346,13 @@ def create_organization_api(): def get_organization(orgname): permission = OrganizationMemberPermission(orgname) if permission.can(): - user = current_user.db_user() - - def org_view(o, teams): + def org_view(org, teams): admin_org = AdministerOrganizationPermission(orgname) is_admin = admin_org.can() return { - 'name': o.username, - 'email': o.email if is_admin else '', - 'gravatar': compute_hash(o.email), + 'name': org.username, + 'email': org.email if is_admin else '', + 'gravatar': compute_hash(org.email), 'teams': {t.name : team_view(orgname, t) for t in teams}, 'is_admin': is_admin } @@ -372,7 +371,7 @@ def get_organization(orgname): @api_login_required def get_organization_members(orgname): permission = AdministerOrganizationPermission(orgname) - if permission.can(): + if permission.can(): try: org = model.get_organization(orgname) except model.InvalidOrganizationException: @@ -385,9 +384,10 @@ def get_organization_members(orgname): members = model.get_organization_members_with_teams(org) for member in members: if not member.user.username in members_dict: - members_dict[member.user.username] = {'username': member.user.username, 'teams': []} + members_dict[member.user.username] = {'username': member.user.username, + 'teams': []} - members_dict[member.user.username]['teams'].append(member.team.name) + members_dict[member.user.username]['teams'].append(member.team.name) return jsonify({'members': members_dict}) @@ -416,9 +416,9 @@ def get_organization_private_allowed(orgname): abort(403) -def member_view(m): +def member_view(member): return { - 'username': m.username + 'username': member.username } @@ -430,25 +430,25 @@ def update_organization_team(orgname, teamname): if edit_permission.can(): team = None - json = request.get_json() + details = request.get_json() is_existing = False try: team = model.get_organization_team(orgname, teamname) is_existing = True - except: + except model.InvalidTeamException: # Create the new team. - description = json['description'] if 'description' in json else '' - role = json['role'] if 'role' in json else 'member' + description = details['description'] if 'description' in details else '' + role = details['role'] if 'role' in details else 'member' org = model.get_organization(orgname) team = model.create_team(teamname, org, role, description) if is_existing: - if 'description' in json: - team.description = json['description'] + if 'description' in details: + team.description = details['description'] team.save() - if 'role' in json: - team = model.set_team_org_permission(team, json['role'], + if 'role' in details: + team = model.set_team_org_permission(team, details['role'], current_user.db_user().username) resp = jsonify(team_view(orgname, team)) @@ -479,12 +479,10 @@ def get_organization_team_members(orgname, teamname): edit_permission = AdministerOrganizationPermission(orgname) if view_permission.can(): - user = current_user.db_user() team = None - try: team = model.get_organization_team(orgname, teamname) - except: + except model.InvalidTeamException: abort(404) members = model.get_organization_team_members(team.id) @@ -508,7 +506,7 @@ def update_organization_team_member(orgname, teamname, membername): # Find the team. try: team = model.get_organization_team(orgname, teamname) - except: + except model.InvalidTeamException: abort(404) # Find the user. @@ -542,23 +540,23 @@ def delete_organization_team_member(orgname, teamname, membername): @api_login_required def create_repo_api(): owner = current_user.db_user() - json = request.get_json() - namespace_name = json['namespace'] if 'namespace' in json else owner.username + req = request.get_json() + namespace_name = req['namespace'] if 'namespace' in req else owner.username permission = CreateRepositoryPermission(namespace_name) if permission.can(): - repository_name = json['repository'] - visibility = json['visibility'] + repository_name = req['repository'] + visibility = req['visibility'] existing = model.get_repository(namespace_name, repository_name) if existing: return make_response('Repository already exists', 400) - visibility = json['visibility'] + visibility = req['visibility'] repo = model.create_repository(namespace_name, repository_name, owner, visibility) - repo.description = json['description'] + repo.description = req['description'] repo.save() return jsonify({ @@ -610,7 +608,7 @@ def list_repos_api(): try: limit = int(limit) if limit else None - except: + except TypeError: limit = None include_public = include_public == 'true' @@ -710,7 +708,7 @@ def get_repo_api(namespace, repository): organization = None try: organization = model.get_organization(namespace) - except: + except model.InvalidOrganizationException: pass permission = ReadRepositoryPermission(namespace, repository) @@ -774,18 +772,6 @@ def get_repo_builds(namespace, repository): abort(403) # Permissions denied - -@app.route('/api/filedrop/', methods=['POST']) -@api_login_required -def get_filedrop_url(): - mime_type = request.get_json()['mimeType'] - (url, file_id) = user_files.prepare_for_drop(mime_type) - return jsonify({ - 'url': url, - 'file_id': file_id - }) - - @app.route('/api/repository//build/', methods=['POST']) @api_login_required @parse_repository_name @@ -813,6 +799,17 @@ def request_repo_build(namespace, repository): abort(403) # Permissions denied +@app.route('/api/filedrop/', methods=['POST']) +@api_login_required +def get_filedrop_url(): + mime_type = request.get_json()['mimeType'] + (url, file_id) = user_files.prepare_for_drop(mime_type) + return jsonify({ + 'url': url, + 'file_id': file_id + }) + + def role_view(repo_perm_obj): return { 'role': repo_perm_obj.role.name, @@ -1276,7 +1273,7 @@ def org_invoices_api(orgname): if not organization.stripe_id: abort(404) - invoices = stripe.Invoice.all(customer=organization.stripe_id, count=12) + invoices = stripe.Invoice.all(customer=organization.stripe_id, count=12) return jsonify({ 'invoices': [invoice_view(i) for i in invoices.data] })