Fix a bunch of errors that pylint was complaining about.
This commit is contained in:
parent
14263de7f8
commit
41c92deb0d
1 changed files with 58 additions and 61 deletions
111
endpoints/api.py
111
endpoints/api.py
|
@ -1,12 +1,11 @@
|
||||||
import logging
|
import logging
|
||||||
import stripe
|
import stripe
|
||||||
import re
|
|
||||||
import requests
|
import requests
|
||||||
import urlparse
|
import urlparse
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from flask import request, make_response, jsonify, abort
|
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 flask.ext.principal import identity_changed, AnonymousIdentity
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
@ -41,7 +40,8 @@ def api_login_required(f):
|
||||||
if not current_user.is_authenticated():
|
if not current_user.is_authenticated():
|
||||||
abort(401)
|
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)
|
abort(401)
|
||||||
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
@ -144,7 +144,7 @@ def convert_user_to_organization():
|
||||||
def change_user_details():
|
def change_user_details():
|
||||||
user = current_user.db_user()
|
user = current_user.db_user()
|
||||||
|
|
||||||
user_data = request.get_json();
|
user_data = request.get_json()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if 'password' in user_data:
|
if 'password' in user_data:
|
||||||
|
@ -264,7 +264,7 @@ def get_matching_entities(prefix):
|
||||||
if permission.can():
|
if permission.can():
|
||||||
try:
|
try:
|
||||||
organization = model.get_organization(organization_name)
|
organization = model.get_organization(organization_name)
|
||||||
except:
|
except model.InvalidOrganizationException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if organization:
|
if organization:
|
||||||
|
@ -272,7 +272,7 @@ def get_matching_entities(prefix):
|
||||||
|
|
||||||
users = model.get_matching_users(prefix, organization)
|
users = model.get_matching_users(prefix, organization)
|
||||||
|
|
||||||
def team_view(team):
|
def entity_team_view(team):
|
||||||
result = {
|
result = {
|
||||||
'name': team.name,
|
'name': team.name,
|
||||||
'kind': 'team',
|
'kind': 'team',
|
||||||
|
@ -291,20 +291,20 @@ def get_matching_entities(prefix):
|
||||||
|
|
||||||
return user_json
|
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]
|
user_data = [user_view(user) for user in users]
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'results': team_data + user_data
|
'results': team_data + user_data
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def team_view(orgname, t):
|
def team_view(orgname, team):
|
||||||
view_permission = ViewTeamPermission(orgname, t.name)
|
view_permission = ViewTeamPermission(orgname, team.name)
|
||||||
role = model.get_team_org_role(t).name
|
role = model.get_team_org_role(team).name
|
||||||
return {
|
return {
|
||||||
'id': t.id,
|
'id': team.id,
|
||||||
'name': t.name,
|
'name': team.name,
|
||||||
'description': t.description,
|
'description': team.description,
|
||||||
'can_view': view_permission.can(),
|
'can_view': view_permission.can(),
|
||||||
'role': role
|
'role': role
|
||||||
}
|
}
|
||||||
|
@ -317,8 +317,9 @@ def create_organization_api():
|
||||||
existing = None
|
existing = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
existing = model.get_organization(org_data['name']) or model.get_user(org_data['name'])
|
existing = (model.get_organization(org_data['name']) or
|
||||||
except:
|
model.get_user(org_data['name']))
|
||||||
|
except model.InvalidOrganizationException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if existing:
|
if existing:
|
||||||
|
@ -329,7 +330,7 @@ def create_organization_api():
|
||||||
return error_resp
|
return error_resp
|
||||||
|
|
||||||
try:
|
try:
|
||||||
organization = model.create_organization(org_data['name'], org_data['email'],
|
model.create_organization(org_data['name'], org_data['email'],
|
||||||
current_user.db_user())
|
current_user.db_user())
|
||||||
return make_response('Created', 201)
|
return make_response('Created', 201)
|
||||||
except model.DataModelException as ex:
|
except model.DataModelException as ex:
|
||||||
|
@ -345,15 +346,13 @@ def create_organization_api():
|
||||||
def get_organization(orgname):
|
def get_organization(orgname):
|
||||||
permission = OrganizationMemberPermission(orgname)
|
permission = OrganizationMemberPermission(orgname)
|
||||||
if permission.can():
|
if permission.can():
|
||||||
user = current_user.db_user()
|
def org_view(org, teams):
|
||||||
|
|
||||||
def org_view(o, teams):
|
|
||||||
admin_org = AdministerOrganizationPermission(orgname)
|
admin_org = AdministerOrganizationPermission(orgname)
|
||||||
is_admin = admin_org.can()
|
is_admin = admin_org.can()
|
||||||
return {
|
return {
|
||||||
'name': o.username,
|
'name': org.username,
|
||||||
'email': o.email if is_admin else '',
|
'email': org.email if is_admin else '',
|
||||||
'gravatar': compute_hash(o.email),
|
'gravatar': compute_hash(org.email),
|
||||||
'teams': {t.name : team_view(orgname, t) for t in teams},
|
'teams': {t.name : team_view(orgname, t) for t in teams},
|
||||||
'is_admin': is_admin
|
'is_admin': is_admin
|
||||||
}
|
}
|
||||||
|
@ -385,7 +384,8 @@ def get_organization_members(orgname):
|
||||||
members = model.get_organization_members_with_teams(org)
|
members = model.get_organization_members_with_teams(org)
|
||||||
for member in members:
|
for member in members:
|
||||||
if not member.user.username in members_dict:
|
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)
|
||||||
|
|
||||||
|
@ -416,9 +416,9 @@ def get_organization_private_allowed(orgname):
|
||||||
abort(403)
|
abort(403)
|
||||||
|
|
||||||
|
|
||||||
def member_view(m):
|
def member_view(member):
|
||||||
return {
|
return {
|
||||||
'username': m.username
|
'username': member.username
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -430,25 +430,25 @@ def update_organization_team(orgname, teamname):
|
||||||
if edit_permission.can():
|
if edit_permission.can():
|
||||||
team = None
|
team = None
|
||||||
|
|
||||||
json = request.get_json()
|
details = request.get_json()
|
||||||
is_existing = False
|
is_existing = False
|
||||||
try:
|
try:
|
||||||
team = model.get_organization_team(orgname, teamname)
|
team = model.get_organization_team(orgname, teamname)
|
||||||
is_existing = True
|
is_existing = True
|
||||||
except:
|
except model.InvalidTeamException:
|
||||||
# Create the new team.
|
# Create the new team.
|
||||||
description = json['description'] if 'description' in json else ''
|
description = details['description'] if 'description' in details else ''
|
||||||
role = json['role'] if 'role' in json else 'member'
|
role = details['role'] if 'role' in details else 'member'
|
||||||
|
|
||||||
org = model.get_organization(orgname)
|
org = model.get_organization(orgname)
|
||||||
team = model.create_team(teamname, org, role, description)
|
team = model.create_team(teamname, org, role, description)
|
||||||
|
|
||||||
if is_existing:
|
if is_existing:
|
||||||
if 'description' in json:
|
if 'description' in details:
|
||||||
team.description = json['description']
|
team.description = details['description']
|
||||||
team.save()
|
team.save()
|
||||||
if 'role' in json:
|
if 'role' in details:
|
||||||
team = model.set_team_org_permission(team, json['role'],
|
team = model.set_team_org_permission(team, details['role'],
|
||||||
current_user.db_user().username)
|
current_user.db_user().username)
|
||||||
|
|
||||||
resp = jsonify(team_view(orgname, team))
|
resp = jsonify(team_view(orgname, team))
|
||||||
|
@ -479,12 +479,10 @@ def get_organization_team_members(orgname, teamname):
|
||||||
edit_permission = AdministerOrganizationPermission(orgname)
|
edit_permission = AdministerOrganizationPermission(orgname)
|
||||||
|
|
||||||
if view_permission.can():
|
if view_permission.can():
|
||||||
user = current_user.db_user()
|
|
||||||
team = None
|
team = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
team = model.get_organization_team(orgname, teamname)
|
team = model.get_organization_team(orgname, teamname)
|
||||||
except:
|
except model.InvalidTeamException:
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
members = model.get_organization_team_members(team.id)
|
members = model.get_organization_team_members(team.id)
|
||||||
|
@ -508,7 +506,7 @@ def update_organization_team_member(orgname, teamname, membername):
|
||||||
# Find the team.
|
# Find the team.
|
||||||
try:
|
try:
|
||||||
team = model.get_organization_team(orgname, teamname)
|
team = model.get_organization_team(orgname, teamname)
|
||||||
except:
|
except model.InvalidTeamException:
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
# Find the user.
|
# Find the user.
|
||||||
|
@ -542,23 +540,23 @@ def delete_organization_team_member(orgname, teamname, membername):
|
||||||
@api_login_required
|
@api_login_required
|
||||||
def create_repo_api():
|
def create_repo_api():
|
||||||
owner = current_user.db_user()
|
owner = current_user.db_user()
|
||||||
json = request.get_json()
|
req = request.get_json()
|
||||||
namespace_name = json['namespace'] if 'namespace' in json else owner.username
|
namespace_name = req['namespace'] if 'namespace' in req else owner.username
|
||||||
|
|
||||||
permission = CreateRepositoryPermission(namespace_name)
|
permission = CreateRepositoryPermission(namespace_name)
|
||||||
if permission.can():
|
if permission.can():
|
||||||
repository_name = json['repository']
|
repository_name = req['repository']
|
||||||
visibility = json['visibility']
|
visibility = req['visibility']
|
||||||
|
|
||||||
existing = model.get_repository(namespace_name, repository_name)
|
existing = model.get_repository(namespace_name, repository_name)
|
||||||
if existing:
|
if existing:
|
||||||
return make_response('Repository already exists', 400)
|
return make_response('Repository already exists', 400)
|
||||||
|
|
||||||
visibility = json['visibility']
|
visibility = req['visibility']
|
||||||
|
|
||||||
repo = model.create_repository(namespace_name, repository_name, owner,
|
repo = model.create_repository(namespace_name, repository_name, owner,
|
||||||
visibility)
|
visibility)
|
||||||
repo.description = json['description']
|
repo.description = req['description']
|
||||||
repo.save()
|
repo.save()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
|
@ -610,7 +608,7 @@ def list_repos_api():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
limit = int(limit) if limit else None
|
limit = int(limit) if limit else None
|
||||||
except:
|
except TypeError:
|
||||||
limit = None
|
limit = None
|
||||||
|
|
||||||
include_public = include_public == 'true'
|
include_public = include_public == 'true'
|
||||||
|
@ -710,7 +708,7 @@ def get_repo_api(namespace, repository):
|
||||||
organization = None
|
organization = None
|
||||||
try:
|
try:
|
||||||
organization = model.get_organization(namespace)
|
organization = model.get_organization(namespace)
|
||||||
except:
|
except model.InvalidOrganizationException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
permission = ReadRepositoryPermission(namespace, repository)
|
permission = ReadRepositoryPermission(namespace, repository)
|
||||||
|
@ -774,18 +772,6 @@ def get_repo_builds(namespace, repository):
|
||||||
abort(403) # Permissions denied
|
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/<path:repository>/build/', methods=['POST'])
|
@app.route('/api/repository/<path:repository>/build/', methods=['POST'])
|
||||||
@api_login_required
|
@api_login_required
|
||||||
@parse_repository_name
|
@parse_repository_name
|
||||||
|
@ -813,6 +799,17 @@ def request_repo_build(namespace, repository):
|
||||||
abort(403) # Permissions denied
|
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):
|
def role_view(repo_perm_obj):
|
||||||
return {
|
return {
|
||||||
'role': repo_perm_obj.role.name,
|
'role': repo_perm_obj.role.name,
|
||||||
|
|
Reference in a new issue