示例#1
0
def _create_assets(courses, users):
    course_id = courses[0].id

    def _create_category(visible):
        return Category.create(
            canvas_assignment_id=98765,
            canvas_assignment_name=
            f'Linger on your pale blue eyes (visible={visible})',
            course_id=course_id,
            title=f'Thought of you as my mountain top (visible={visible})',
            visible=visible,
        )

    category_visible = _create_category(True)
    category_hidden = _create_category(False)
    std_commit(allow_test_environment=True)

    assets = []
    for a in _test_assets:
        asset = Asset.create(
            asset_type=a['asset_type'],
            categories=[category_hidden, category_visible],
            course_id=course_id,
            description=None,
            title=a['title'],
            url=a['url'],
            users=[users[0]],
        )
        db.session.add(asset)
        assets.append(asset)
    std_commit(allow_test_environment=True)
    return assets
示例#2
0
 def create_link_submission_asset(self, course, user, category, assignment, submission, link_submission_tracker):
     try:
         existing_submission_asset = link_submission_tracker.get(submission.url, None)
         if existing_submission_asset:
             app.logger.debug(f'Adding new user to existing link asset: user {user.canvas_user_id}, asset {existing_submission_asset.id}.')
             existing_submission_asset.users.append(user)
             db.session.add(existing_submission_asset)
             std_commit()
         else:
             app.logger.info(
                 f'Will create link asset for submission: '
                 f'user {user.canvas_user_id}, submission {submission.id}, assignment {assignment.id}, {_format_course(course)}')
             link_submission_tracker[submission.url] = Asset.create(
                 asset_type='link',
                 canvas_assignment_id=assignment.id,
                 categories=[category],
                 course_id=course.id,
                 source=submission.url,
                 title=submission.url,
                 url=submission.url,
                 users=[user],
                 create_activity=False,
             )
     except Exception as e:
         app.logger.error(
             f'Failed to create link asset for an assignment submission: '
             f'user {user.canvas_user_id}, submission {submission.id}, assignment {assignment.id}, {_format_course(course)}')
         app.logger.exception(e)
示例#3
0
def mock_asset(app, db_session):
    course = Course.create(
        canvas_api_domain='bcourses.berkeley.edu',
        canvas_course_id=randrange(1000000),
    )
    category_hidden = Category.create(
        canvas_assignment_name='Just look into her false colored eyes',
        course_id=course.id,
        title='What a clown (visible=False)',
        canvas_assignment_id=98765,
        visible=False,
    )
    category_visible = Category.create(
        canvas_assignment_name='Just look into her false colored eyes',
        course_id=course.id,
        title='What a clown (visible=True)',
        canvas_assignment_id=98765,
        visible=True,
    )
    course = Course.query.order_by(Course.name).all()[0]
    canvas_user_id = str(randint(1000000, 9999999))
    user = User.create(
        canvas_course_role='Student',
        canvas_course_sections=[],
        canvas_email=f'{canvas_user_id}@berkeley.edu',
        canvas_enrollment_state='active',
        canvas_full_name=f'Student {canvas_user_id}',
        canvas_user_id=canvas_user_id,
        course_id=course.id,
    )
    unique_token = datetime.now().isoformat()
    asset = Asset.create(
        asset_type='link',
        categories=[category_hidden, category_visible],
        course_id=course.id,
        description=None,
        download_url=
        f"s3://{app.config['AWS_S3_BUCKET_FOR_ASSETS']}/asset/{course.id}_{canvas_user_id}_{unique_token}.pdf",
        title=f'Mock Asset created at {unique_token}',
        url=f'https://en.wikipedia.org/wiki/{unique_token}',
        users=[user],
    )
    for test_comment in _get_mock_comments():
        comment = Comment.create(asset=asset,
                                 body=test_comment['body'],
                                 user_id=user.id)
        for reply in test_comment.get('replies', []):
            reply = Comment.create(
                asset=asset,
                body=reply['body'],
                parent_id=comment.id,
                user_id=user.id,
            )
    std_commit(allow_test_environment=True)
    yield asset
    db_session.delete(asset)
    std_commit(allow_test_environment=True)
示例#4
0
 def test_asset_creation_invisible_no_activities(self,
                                                 authorized_user_session):
     asset = Asset.create(
         asset_type='link',
         categories=None,
         course_id=authorized_user_session.course.id,
         title='Riding in a Stutz Bear Cat, Jim',
         url='https://genius.com/The-velvet-underground-sweet-jane-lyrics',
         users=[authorized_user_session.user],
         visible=False,
     )
     activities = Activity.query.filter_by(asset_id=asset.id).all()
     assert len(activities) == 0
示例#5
0
 def test_asset_creation_activity(self, authorized_user_session):
     asset = Asset.create(
         asset_type='link',
         categories=None,
         course_id=authorized_user_session.course.id,
         title='Riding in a Stutz Bear Cat, Jim',
         url='https://genius.com/The-velvet-underground-sweet-jane-lyrics',
         users=[authorized_user_session.user],
     )
     activities = Activity.query.filter_by(asset_id=asset.id).all()
     assert len(activities) == 1
     assert activities[0].activity_type == 'asset_add'
     assert activities[0].course_id == authorized_user_session.course.id
     assert activities[0].object_type == 'asset'
     assert activities[0].object_id == asset.id
     assert activities[0].asset_id == asset.id
     assert activities[0].user_id == authorized_user_session.user.id
示例#6
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())
示例#7
0
 def create_file_submission_assets(self, course, user, category, assignment, submission, file_submission_tracker):
     app.logger.info(
         f'Will create file assets for submission attachments: '
         f'user {user.canvas_user_id}, submission {submission.id}, assignment {assignment.id}, {_format_course(course)}')
     for attachment in getattr(submission, 'attachments', []):
         try:
             if attachment['size'] > 10485760:
                 app.logger.debug('Attachment too large, will not process.')
                 continue
             existing_submission_asset = file_submission_tracker.get(attachment['id'], None)
             if existing_submission_asset:
                 app.logger.debug(f'Adding new user to existing file asset: user {user.canvas_user_id}, asset {existing_submission_asset.id}.')
                 existing_submission_asset.users.append(user)
                 db.session.add(existing_submission_asset)
                 std_commit()
             else:
                 s3_attrs = Asset.upload_to_s3(
                     filename=attachment['filename'],
                     byte_stream=urlopen(attachment['url']).read(),
                     course_id=course.id,
                 )
                 file_submission_tracker[attachment['id']] = Asset.create(
                     asset_type='file',
                     canvas_assignment_id=assignment.id,
                     categories=[category],
                     course_id=course.id,
                     download_url=s3_attrs.get('download_url', None),
                     mime=s3_attrs.get('content_type', None),
                     title=attachment['filename'],
                     users=[user],
                     create_activity=False,
                 )
         except Exception as e:
             app.logger.error(
                 f'Failed to create file asset for an attachment: '
                 f'user {user.canvas_user_id}, submission {submission.id}, assignment {assignment.id}, {_format_course(course)}')
             app.logger.exception(e)