Home project: create it when user tries to GET it.

This commit is contained in:
2016-06-14 13:26:53 +02:00
parent 3980133100
commit b4faf2245e
10 changed files with 269 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
def setup_app(app, url_prefix):
from . import texture_libs
from . import texture_libs, home_project
texture_libs.setup_app(app, url_prefix=url_prefix)
home_project.setup_app(app, url_prefix=url_prefix)

View File

@@ -0,0 +1,92 @@
import logging
from bson import ObjectId
from eve.methods.put import put_internal
from eve.methods.get import get
from flask import Blueprint, g, current_app
from werkzeug import exceptions as wz_exceptions
from application.modules import projects
from application import utils
from application.utils import authentication, authorization
blueprint = Blueprint('blender_cloud.home_project', __name__)
log = logging.getLogger(__name__)
# Users with any of these roles will get a home project.
HOME_PROJECT_USERS = {u'subscriber', u'demo'}
def create_home_project(user_id):
"""Creates a home project for the given user."""
log.info('Creating home project for user %s', user_id)
project = projects.create_new_project(project_name='Home',
user_id=ObjectId(user_id),
overrides={'category': 'home'})
# Re-validate the authentication token, so that the put_internal call sees the
# new group created for the project.
authentication.validate_token()
# There are a few things in the on_insert_projects hook we need to adjust.
# Ensure that the project is private, even for admins.
project['permissions']['world'] = []
# Set up the correct node types. No need to set permissions for them,
# as the inherited project permissions are fine.
from manage_extra.node_types.group import node_type_group
from manage_extra.node_types.asset import node_type_asset
from manage_extra.node_types.text import node_type_text
project['node_types'] = [
node_type_group,
node_type_asset,
node_type_text,
]
result, _, _, status = put_internal('projects', utils.remove_private_keys(project),
_id=project['_id'])
if status != 200:
log.error('Unable to update home project %s for user %s: %s',
project['_id'], user_id, result)
raise wz_exceptions.InternalServerError('Unable to update home project')
project.update(result)
return project
@blueprint.route('/home-project')
@authorization.require_login(require_roles={u'subscriber', u'demo'})
def home_project():
user_id = g.current_user['user_id']
roles = g.current_user.get('roles', ())
log.debug('Possibly creating home project for user %s with roles %s', user_id, roles)
if not HOME_PROJECT_USERS.intersection(roles):
log.debug('User %s is not a subscriber, not creating home project.', user_id)
return 'No home project', 404
resp, _, _, status, _ = get('projects', category=u'home', user=user_id)
if status != 200:
return utils.jsonify(resp), status
if resp['_items']:
project = resp['_items'][0]
else:
log.debug('Home project for user %s not found', user_id)
project = create_home_project(user_id)
return utils.jsonify(project), status
def has_home_project(user_id):
"""Returns True iff the user has a home project."""
proj_coll = current_app.data.driver.db['projects']
return proj_coll.count({'user': user_id, 'category': 'home'}) > 0
def setup_app(app, url_prefix):
app.register_blueprint(blueprint, url_prefix=url_prefix)

View File

@@ -194,7 +194,7 @@ def after_inserting_project(project, db_user):
abort_with_error(500)
def _create_new_project(project_name, user_id, overrides):
def create_new_project(project_name, user_id, overrides):
"""Creates a new project owned by the given user."""
log.info('Creating new project "%s" for user %s', project_name, user_id)
@@ -242,7 +242,7 @@ def create_project(overrides=None):
project_name = request.form['project_name']
user_id = g.current_user['user_id']
project = _create_new_project(project_name, user_id, overrides)
project = create_new_project(project_name, user_id, overrides)
# Return the project in the response.
return jsonify(project, status=201, headers={'Location': '/projects/%s' % project['_id']})
@@ -306,6 +306,7 @@ def project_manage_users():
user = users_collection.find_one({'_id': target_user_id},
{'username': 1, 'email': 1,
'full_name': 1})
user['_status'] = 'OK'
return jsonify(user)

View File

@@ -7,7 +7,7 @@ from flask import Blueprint, current_app, g, request
from werkzeug import exceptions as wz_exceptions
from application.utils import authorization
from application.modules import local_auth
from application.modules import local_auth, users
blueprint = Blueprint('service', __name__)
log = logging.getLogger(__name__)

View File

@@ -4,7 +4,8 @@ import json
import logging
import urllib
from flask import g, current_app, Blueprint, make_response
from flask import g, current_app, Blueprint
from werkzeug.exceptions import Forbidden
from eve.utils import parse_request
from eve.methods.get import get

View File

@@ -0,0 +1,28 @@
node_type_text = {
'name': 'text',
'description': 'Text',
'parent': ['group', 'project'],
'dyn_schema': {
'content': {
'type': 'string',
'required': True,
'minlength': 3,
'maxlength': 90000,
},
'shared_slug': {
'type': 'string',
'required': False,
},
'syntax': { # for syntax highlighting
'type': 'string',
'required': False,
},
'node_expires': {
'type': 'datetime',
'required': False,
},
},
'form_schema': {
'shared_slug': {'visible': False},
}
}

View File

@@ -569,7 +569,8 @@ projects_schema = {
'film',
'assets',
'software',
'game'
'game',
'home',
],
'required': True,
},