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())
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)