예제 #1
0
def submit_submission(code, author, **kwargs):
    activity = Activity.query.filter_by(code=code).first()
    if not activity:
        raise InvalidUsage.item_not_found()
    if 'submitted_file' in kwargs and kwargs['submitted_file'] is not None:
        uploaded_file = kwargs.pop('submitted_file')
        if uploaded_file.filename == '':
            raise InvalidUsage.no_files_found()
        filename_original, file_extension = os.path.splitext(
            secure_filename(uploaded_file.filename))
        filename = '{}{}'.format(uuid.uuid4().hex, file_extension)
        uploaded_file.save(
            os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename))
        file_url = '/uploads/{}'.format(filename)
        file_size = os.path.getsize(
            os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename))
        file_object = File(filename=filename,
                           url=file_url,
                           filemime=uploaded_file.mimetype,
                           filename_original=uploaded_file.filename,
                           filesize=file_size)
        file_object.save()
    else:
        raise InvalidUsage.no_files_found()
    file_type = kwargs.get('file_type')
    if not file_type:
        if file_extension in ['.png', '.jpg', '.gif', '.jpeg']:
            file_type = 'image'
        elif file_extension in ['.mp4']:
            file_type = 'video'
    submission = Submission(author=author,
                            file=file_object,
                            activity=activity,
                            file_type=file_type).save()
    return submission
예제 #2
0
def update_field(field_id, **kwargs):
    field = Field.query.filter(
        Field.id == field_id, Field.author_id == current_user.profile.id).first()
    if not field:
        raise InvalidUsage.field_not_found()

    if 'file' in kwargs and kwargs['file'] is not None:
        uploaded_file = kwargs.pop('file')
        if uploaded_file.filename == '':
            raise InvalidUsage.no_files_found()
        filename_original, file_extension = os.path.splitext(
            secure_filename(uploaded_file.filename))
        check_file_extension_for_type(type, file_extension)
        filename = '{}{}'.format(uuid.uuid4().hex, file_extension)
        uploaded_file.save(os.path.join(app.root_path,
                                        app.config['UPLOAD_FOLDER'], filename))
        file_url = '/uploads/{}'.format(filename)
        file_size = os.path.getsize(os.path.join(app.root_path,
                                                 app.config['UPLOAD_FOLDER'], filename))
        file_object = File(filename=filename, url=file_url,
                           filemime=uploaded_file.mimetype, filename_original=uploaded_file.filename, filesize=file_size)
        file_object.save()

        field.update(file=file_object, **kwargs)
    else:
        field.update(**kwargs)
    field.save()
    return field
예제 #3
0
def create_field(label, space_id, type, **kwargs):
    space = Space.get_by_id(space_id)
    if not space:
        raise InvalidUsage.project_not_found()
    if 'file' in kwargs and kwargs['file'] is not None:
        uploaded_file = kwargs.pop('file')
        if uploaded_file.filename == '':
            raise InvalidUsage.no_files_found()
        filename_original, file_extension = os.path.splitext(
            secure_filename(uploaded_file.filename))
        check_file_extension_for_type(type, file_extension)
        filename = '{}{}'.format(uuid.uuid4().hex, file_extension)
        uploaded_file.save(os.path.join(app.root_path,
                                        app.config['UPLOAD_FOLDER'], filename))
        file_url = '/uploads/{}'.format(filename)
        file_size = os.path.getsize(os.path.join(app.root_path,
                                                 app.config['UPLOAD_FOLDER'], filename))
        file_object = File(filename=filename, url=file_url,
                           filemime=uploaded_file.mimetype, filename_original=uploaded_file.filename, filesize=file_size)
        file_object.save()

    field = None
    try:
        if type == 'position':
            field = Position(label=label, space=space,
                             author=current_user.profile, **kwargs)
        elif type == 'text':
            field = Text(label=label, space=space,
                         author=current_user.profile, ** kwargs)
        elif type == 'number':
            field = Number(label=label, space=space,
                           author=current_user.profile, **kwargs)
        elif type == 'audio':
            field = Audio(label=label, space=space,
                          author=current_user.profile, file=file_object, ** kwargs)
        elif type == 'video':
            field = Video(label=label, space=space,
                          author=current_user.profile, file=file_object, ** kwargs)
        elif type == 'videosphere':
            field = VideoSphere(label=label, space=space,
                                author=current_user.profile, file=file_object, ** kwargs)
        elif type == 'image':
            field = Image(label=label, space=space,
                          author=current_user.profile, file=file_object, ** kwargs)
        elif type == 'photosphere':
            field = PhotoSphere(label=label, space=space,
                                author=current_user.profile, file=file_object, ** kwargs)
        elif type == 'link':
            field = Link(label=label, space=space,
                         author=current_user.profile, **kwargs)
        elif type == 'color':
            field = Color(label=label, space=space,
                          author=current_user.profile, **kwargs)
        else:
            raise Exception(("Field type - {} not found").format(type))
            return
        if 'parent_id' in kwargs:
            parent_field = Field.get_by_id(kwargs['parent_id'])
            if parent_field:
                field.parent = parent_field
        field.save()

    except IntegrityError:
        db.session.rollback()
        raise InvalidUsage.no_files_found()
    except Exception as e:
        print(e)
        raise InvalidUsage.field_error()
    return field
예제 #4
0
def create_field(label, project_name, type, **kwargs):
    project = Project.query.filter_by(slug=project_name).first()
    if not project:
        raise InvalidUsage.project_not_found()
    field = None
    try:
        if type == 'position':
            field = Position(label=label,
                             project=project,
                             author=project.author,
                             **kwargs)
        elif type == 'text':
            field = Text(label=label,
                         project=project,
                         author=project.author,
                         **kwargs)
        elif type == 'number':
            field = Number(label=label,
                           project=project,
                           author=project.author,
                           **kwargs)
        elif type == 'audio':
            field = Audio(label=label,
                          project=project,
                          author=project.author,
                          **kwargs)
        elif type == 'video':
            field = Video(label=label,
                          project=project,
                          author=project.author,
                          **kwargs)
        elif type == 'videosphere':
            field = VideoSphere(label=label,
                                project=project,
                                author=project.author,
                                **kwargs)
        elif type == 'image':
            field = Image(label=label,
                          project=project,
                          author=project.author,
                          **kwargs)
        elif type == 'photosphere':
            field = PhotoSphere(label=label,
                                project=project,
                                author=project.author,
                                **kwargs)
        else:
            raise Exception(("Field type - {} not found").format(type))
            return
        if 'parent_id' in kwargs:
            parent_field = Field.get_by_id(kwargs['parent_id'])
            if parent_field:
                field.parent = parent_field
        field.save()

    except IntegrityError:
        db.session.rollback()
        raise InvalidUsage.no_files_found()
    except Exception as e:
        print(e)
        raise InvalidUsage.field_error()
    return field