Exemplo n.º 1
0
def create_asset():
    params = request.get_json() or request.form
    asset_type = params.get('type')
    category_id = params.get('categoryId')
    description = params.get('description')
    source = params.get('source')
    url = params.get('url')
    title = params.get('title', url)
    visible = params.get('visible', True)
    if not asset_type or not title:
        raise BadRequestError('Asset creation requires title and type.')

    if asset_type == 'link' and not url:
        raise BadRequestError('Link asset creation requires url.')

    if not current_user.course:
        raise BadRequestError('Course data not found')

    s3_attrs = {}
    if asset_type == 'file':
        file_upload = _get_upload_from_http_post()
        s3_attrs = Asset.upload_to_s3(
            filename=file_upload['name'],
            byte_stream=file_upload['byte_stream'],
            course_id=current_user.course.id,
        )

    asset = Asset.create(
        asset_type=asset_type,
        categories=category_id and [Category.find_by_id(category_id)],
        course_id=current_user.course.id,
        description=description,
        download_url=s3_attrs.get('download_url', None),
        mime=s3_attrs.get('content_type', None),
        source=source,
        title=title,
        url=url,
        users=[User.find_by_id(current_user.get_id())],
        visible=visible,
    )
    return tolerant_jsonify(asset.to_api_json())
Exemplo n.º 2
0
 def test_admin(self, client, fake_auth, authorized_user_id):
     """Cookies for authenticated user."""
     user = User.find_by_id(authorized_user_id)
     fake_auth.login(user.id)
     self._assert_cookie(client=client, user=user)
Exemplo n.º 3
0
 def test_load_admin_user(self, authorized_user_id):
     """Returns authorization record to Flask-Login for recognized UID."""
     loaded_user = User.find_by_id(authorized_user_id)
     assert loaded_user.id == authorized_user_id
Exemplo n.º 4
0
 def test_load_unknown_user(self):
     """Returns None to Flask-Login for unrecognized UID."""
     assert User.find_by_id(unauthorized_user_id) is None
Exemplo n.º 5
0
 def __init__(self, user_id):
     self.user = User.find_by_id(user_id) if user_id else None