Accidental refactor, split out legacy.py into separate sumodules and update all call sites.
This commit is contained in:
parent
2109d24483
commit
3efaa255e8
92 changed files with 4458 additions and 4269 deletions
|
@ -3,33 +3,33 @@
|
|||
import logging
|
||||
import json
|
||||
|
||||
from random import SystemRandom
|
||||
from flask import request, abort
|
||||
from flask.ext.login import logout_user
|
||||
from flask.ext.principal import identity_changed, AnonymousIdentity
|
||||
from peewee import IntegrityError
|
||||
|
||||
import features
|
||||
|
||||
from app import app, billing as stripe, authentication, avatar
|
||||
from endpoints.api import (ApiResource, nickname, resource, validate_json_request, request_error,
|
||||
log_action, internal_only, NotFound, require_user_admin, parse_args,
|
||||
query_param, InvalidToken, require_scope, format_date, hide_if, show_if,
|
||||
query_param, InvalidToken, require_scope, format_date, show_if,
|
||||
license_error, require_fresh_login, path_param, define_json_response,
|
||||
RepositoryParamResource)
|
||||
from endpoints.api.subscribe import subscribe
|
||||
from endpoints.common import common_login
|
||||
from endpoints.decorators import anon_allowed
|
||||
from endpoints.api.team import try_accept_invite
|
||||
|
||||
from data import model
|
||||
from data.billing import get_plan
|
||||
from auth.permissions import (AdministerOrganizationPermission, CreateRepositoryPermission,
|
||||
UserAdminPermission, UserReadPermission, SuperUserPermission)
|
||||
from auth.auth_context import get_authenticated_user
|
||||
from auth import scopes
|
||||
from util.useremails import (send_confirmation_email, send_recovery_email, send_change_email, send_password_changed)
|
||||
from util.useremails import (send_confirmation_email, send_recovery_email, send_change_email,
|
||||
send_password_changed)
|
||||
from util.names import parse_single_urn
|
||||
|
||||
import features
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -45,7 +45,7 @@ def user_view(user):
|
|||
'preferred_namespace': not (o.stripe_id is None)
|
||||
}
|
||||
|
||||
organizations = model.get_user_organizations(user.username)
|
||||
organizations = model.organization.get_user_organizations(user.username)
|
||||
|
||||
def login_view(login):
|
||||
try:
|
||||
|
@ -59,7 +59,7 @@ def user_view(user):
|
|||
'metadata': metadata
|
||||
}
|
||||
|
||||
logins = model.list_federated_logins(user)
|
||||
logins = model.user.list_federated_logins(user)
|
||||
|
||||
user_response = {
|
||||
'anonymous': False,
|
||||
|
@ -89,14 +89,14 @@ def user_view(user):
|
|||
return user_response
|
||||
|
||||
|
||||
def notification_view(notification):
|
||||
def notification_view(note):
|
||||
return {
|
||||
'id': notification.uuid,
|
||||
'organization': notification.target.username if notification.target.organization else None,
|
||||
'kind': notification.kind.name,
|
||||
'created': format_date(notification.created),
|
||||
'metadata': json.loads(notification.metadata_json),
|
||||
'dismissed': notification.dismissed
|
||||
'id': note.uuid,
|
||||
'organization': note.target.username if note.target.organization else None,
|
||||
'kind': note.kind.name,
|
||||
'created': format_date(note.created),
|
||||
'metadata': json.loads(note.metadata_json),
|
||||
'dismissed': note.dismissed
|
||||
}
|
||||
|
||||
|
||||
|
@ -238,7 +238,7 @@ class User(ApiResource):
|
|||
log_action('account_change_password', user.username)
|
||||
|
||||
# Change the user's password.
|
||||
model.change_password(user, user_data['password'])
|
||||
model.user.change_password(user, user_data['password'])
|
||||
|
||||
# Login again to reset their session cookie.
|
||||
common_login(user)
|
||||
|
@ -248,36 +248,36 @@ class User(ApiResource):
|
|||
|
||||
if 'invoice_email' in user_data:
|
||||
logger.debug('Changing invoice_email for user: %s', user.username)
|
||||
model.change_invoice_email(user, user_data['invoice_email'])
|
||||
model.user.change_invoice_email(user, user_data['invoice_email'])
|
||||
|
||||
if 'tag_expiration' in user_data:
|
||||
logger.debug('Changing user tag expiration to: %ss', user_data['tag_expiration'])
|
||||
model.change_user_tag_expiration(user, user_data['tag_expiration'])
|
||||
model.user.change_user_tag_expiration(user, user_data['tag_expiration'])
|
||||
|
||||
if 'email' in user_data and user_data['email'] != user.email:
|
||||
new_email = user_data['email']
|
||||
if model.find_user_by_email(new_email):
|
||||
if model.user.find_user_by_email(new_email):
|
||||
# Email already used.
|
||||
raise request_error(message='E-mail address already used')
|
||||
|
||||
if features.MAILING:
|
||||
logger.debug('Sending email to change email address for user: %s',
|
||||
user.username)
|
||||
code = model.create_confirm_email_code(user, new_email=new_email)
|
||||
code = model.user.create_confirm_email_code(user, new_email=new_email)
|
||||
send_change_email(user.username, user_data['email'], code.code)
|
||||
else:
|
||||
model.update_email(user, new_email, auto_verify=not features.MAILING)
|
||||
model.user.update_email(user, new_email, auto_verify=not features.MAILING)
|
||||
|
||||
if ('username' in user_data and user_data['username'] != user.username and
|
||||
features.USER_RENAME):
|
||||
new_username = user_data['username']
|
||||
if model.get_user_or_org(new_username) is not None:
|
||||
if model.user.get_user_or_org(new_username) is not None:
|
||||
# Username already used
|
||||
raise request_error(message='Username is already in use')
|
||||
|
||||
model.change_username(user.id, new_username)
|
||||
model.user.change_username(user.id, new_username)
|
||||
|
||||
except model.InvalidPasswordException, ex:
|
||||
except model.user.InvalidPasswordException, ex:
|
||||
raise request_error(exception=ex)
|
||||
|
||||
return user_view(user)
|
||||
|
@ -291,12 +291,12 @@ class User(ApiResource):
|
|||
user_data = request.get_json()
|
||||
invite_code = user_data.get('invite_code', '')
|
||||
|
||||
existing_user = model.get_nonrobot_user(user_data['username'])
|
||||
existing_user = model.user.get_nonrobot_user(user_data['username'])
|
||||
if existing_user:
|
||||
raise request_error(message='The username already exists')
|
||||
|
||||
try:
|
||||
new_user = model.create_user(user_data['username'], user_data['password'],
|
||||
new_user = model.user.create_user(user_data['username'], user_data['password'],
|
||||
user_data['email'], auto_verify=not features.MAILING)
|
||||
|
||||
# Handle any invite codes.
|
||||
|
@ -306,12 +306,12 @@ class User(ApiResource):
|
|||
# Add the user to the team.
|
||||
try:
|
||||
try_accept_invite(invite_code, new_user)
|
||||
except model.DataModelException:
|
||||
except model.user.DataModelException:
|
||||
pass
|
||||
|
||||
|
||||
if features.MAILING:
|
||||
code = model.create_confirm_email_code(new_user)
|
||||
code = model.user.create_confirm_email_code(new_user)
|
||||
send_confirmation_email(new_user.username, new_user.email, code.code)
|
||||
return {
|
||||
'awaiting_verification': True
|
||||
|
@ -320,9 +320,9 @@ class User(ApiResource):
|
|||
common_login(new_user)
|
||||
return user_view(new_user)
|
||||
|
||||
except model.TooManyUsersException as ex:
|
||||
except model.user.TooManyUsersException as ex:
|
||||
raise license_error(exception=ex)
|
||||
except model.DataModelException as ex:
|
||||
except model.user.DataModelException as ex:
|
||||
raise request_error(exception=ex)
|
||||
|
||||
@resource('/v1/user/private')
|
||||
|
@ -336,7 +336,7 @@ class PrivateRepositories(ApiResource):
|
|||
""" Get the number of private repos this user has, and whether they are allowed to create more.
|
||||
"""
|
||||
user = get_authenticated_user()
|
||||
private_repos = model.get_private_repo_count(user.username)
|
||||
private_repos = model.user.get_private_repo_count(user.username)
|
||||
repos_allowed = 0
|
||||
|
||||
if user.stripe_id:
|
||||
|
@ -396,7 +396,7 @@ def conduct_signin(username_or_email, password):
|
|||
verified = None
|
||||
try:
|
||||
(verified, error_message) = authentication.verify_user(username_or_email, password)
|
||||
except model.TooManyUsersException as ex:
|
||||
except model.user.TooManyUsersException as ex:
|
||||
raise license_error(exception=ex)
|
||||
|
||||
if verified:
|
||||
|
@ -457,15 +457,14 @@ class ConvertToOrganization(ApiResource):
|
|||
# Ensure that the sign in credentials work.
|
||||
admin_username = convert_data['adminUser']
|
||||
admin_password = convert_data['adminPassword']
|
||||
(admin_user, error_message) = authentication.verify_user(admin_username, admin_password)
|
||||
(admin_user, _) = authentication.verify_user(admin_username, admin_password)
|
||||
if not admin_user:
|
||||
raise request_error(reason='invaliduser',
|
||||
message='The admin user credentials are not valid')
|
||||
message='The admin user credentials are not valid')
|
||||
|
||||
# Ensure that the new admin user is the not user being converted.
|
||||
if admin_user.id == user.id:
|
||||
raise request_error(reason='invaliduser',
|
||||
message='The admin user is not valid')
|
||||
raise request_error(reason='invaliduser', message='The admin user is not valid')
|
||||
|
||||
# Subscribe the organization to the new plan.
|
||||
if features.BILLING:
|
||||
|
@ -473,7 +472,7 @@ class ConvertToOrganization(ApiResource):
|
|||
subscribe(user, plan, None, True) # Require business plans
|
||||
|
||||
# Convert the user to an organization.
|
||||
model.convert_user_to_organization(user, admin_user)
|
||||
model.organization.convert_user_to_organization(user, admin_user)
|
||||
log_action('account_convert', user.username)
|
||||
|
||||
# And finally login with the admin credentials.
|
||||
|
@ -583,7 +582,7 @@ class DetachExternal(ApiResource):
|
|||
@nickname('detachExternalLogin')
|
||||
def post(self, servicename):
|
||||
""" Request that the current user be detached from the external login service. """
|
||||
model.detach_external_login(get_authenticated_user(), servicename)
|
||||
model.user.detach_external_login(get_authenticated_user(), servicename)
|
||||
return {'success': True}
|
||||
|
||||
|
||||
|
@ -614,7 +613,7 @@ class Recovery(ApiResource):
|
|||
def post(self):
|
||||
""" Request a password recovery email."""
|
||||
email = request.get_json()['email']
|
||||
code = model.create_reset_password_email_code(email)
|
||||
code = model.user.create_reset_password_email_code(email)
|
||||
send_recovery_email(email, code.code)
|
||||
return 'Created', 201
|
||||
|
||||
|
@ -631,7 +630,8 @@ class UserNotificationList(ApiResource):
|
|||
page = args['page']
|
||||
limit = args['limit']
|
||||
|
||||
notifications = list(model.list_notifications(get_authenticated_user(), page=page, limit=limit + 1))
|
||||
notifications = list(model.notification.list_notifications(get_authenticated_user(), page=page,
|
||||
limit=limit + 1))
|
||||
has_more = False
|
||||
|
||||
if len(notifications) > limit:
|
||||
|
@ -639,7 +639,7 @@ class UserNotificationList(ApiResource):
|
|||
notifications = notifications[0:limit]
|
||||
|
||||
return {
|
||||
'notifications': [notification_view(notification) for notification in notifications],
|
||||
'notifications': [notification_view(note) for note in notifications],
|
||||
'additional': has_more
|
||||
}
|
||||
|
||||
|
@ -665,24 +665,24 @@ class UserNotification(ApiResource):
|
|||
@require_user_admin
|
||||
@nickname('getUserNotification')
|
||||
def get(self, uuid):
|
||||
notification = model.lookup_notification(get_authenticated_user(), uuid)
|
||||
if not notification:
|
||||
note = model.notification.lookup_notification(get_authenticated_user(), uuid)
|
||||
if not note:
|
||||
raise NotFound()
|
||||
|
||||
return notification_view(notification)
|
||||
return notification_view(note)
|
||||
|
||||
@require_user_admin
|
||||
@nickname('updateUserNotification')
|
||||
@validate_json_request('UpdateNotification')
|
||||
def put(self, uuid):
|
||||
notification = model.lookup_notification(get_authenticated_user(), uuid)
|
||||
if not notification:
|
||||
note = model.notification.lookup_notification(get_authenticated_user(), uuid)
|
||||
if not note:
|
||||
raise NotFound()
|
||||
|
||||
notification.dismissed = request.get_json().get('dismissed', False)
|
||||
notification.save()
|
||||
note.dismissed = request.get_json().get('dismissed', False)
|
||||
note.save()
|
||||
|
||||
return notification_view(notification)
|
||||
return notification_view(note)
|
||||
|
||||
|
||||
def authorization_view(access_token):
|
||||
|
@ -733,8 +733,7 @@ class UserAuthorization(ApiResource):
|
|||
@require_user_admin
|
||||
@nickname('deleteUserAuthorization')
|
||||
def delete(self, access_token_uuid):
|
||||
access_token = model.oauth.lookup_access_token_for_user(get_authenticated_user(),
|
||||
access_token_uuid)
|
||||
access_token = model.oauth.lookup_access_token_for_user(get_authenticated_user(), access_token_uuid)
|
||||
if not access_token:
|
||||
raise NotFound()
|
||||
|
||||
|
@ -774,9 +773,8 @@ class StarredRepositoryList(ApiResource):
|
|||
""" List all starred repositories. """
|
||||
page = args['page']
|
||||
limit = args['limit']
|
||||
starred_repos = model.get_user_starred_repositories(get_authenticated_user(),
|
||||
page=page,
|
||||
limit=limit)
|
||||
starred_repos = model.repository.get_user_starred_repositories(get_authenticated_user(),
|
||||
page=page, limit=limit)
|
||||
def repo_view(repo_obj):
|
||||
return {
|
||||
'namespace': repo_obj.namespace_user.username,
|
||||
|
@ -797,11 +795,11 @@ class StarredRepositoryList(ApiResource):
|
|||
req = request.get_json()
|
||||
namespace = req['namespace']
|
||||
repository = req['repository']
|
||||
repo = model.get_repository(namespace, repository)
|
||||
repo = model.repository.get_repository(namespace, repository)
|
||||
|
||||
if repo:
|
||||
try:
|
||||
model.star_repository(user, repo)
|
||||
model.repository.star_repository(user, repo)
|
||||
except IntegrityError:
|
||||
pass
|
||||
|
||||
|
@ -820,10 +818,10 @@ class StarredRepository(RepositoryParamResource):
|
|||
def delete(self, namespace, repository):
|
||||
""" Removes a star from a repository. """
|
||||
user = get_authenticated_user()
|
||||
repo = model.get_repository(namespace, repository)
|
||||
repo = model.repository.get_repository(namespace, repository)
|
||||
|
||||
if repo:
|
||||
model.unstar_repository(user, repo)
|
||||
model.repository.unstar_repository(user, repo)
|
||||
return 'Deleted', 204
|
||||
|
||||
|
||||
|
@ -833,7 +831,7 @@ class Users(ApiResource):
|
|||
@nickname('getUserInformation')
|
||||
def get(self, username):
|
||||
""" Get user information for the specified user. """
|
||||
user = model.get_nonrobot_user(username)
|
||||
user = model.user.get_nonrobot_user(username)
|
||||
if user is None:
|
||||
abort(404)
|
||||
|
||||
|
|
Reference in a new issue