Пример #1
0
def favorite_a_project(slug):
    profile = current_user.profile
    project = Project.query.filter_by(slug=slug).first()
    if not project:
        raise InvalidUsage.project_not_found()
    project.favourite(profile)
    project.save()
    return project
Пример #2
0
def update_project(slug, **kwargs):
    project = Project.query.filter_by(
        slug=slug, author_id=current_user.profile.id).first()
    if not project:
        raise InvalidUsage.project_not_found()
    project.update(updatedAt=dt.datetime.utcnow(), **kwargs)
    project.save()
    return project
Пример #3
0
def delete_space_from_project(space_id, project_id):
    space = Space.get_by_id(space_id)
    if not space:
        raise InvalidUsage.space_not_found()
    project = Project.get_by_id(project_id)
    if not project:
        raise InvalidUsage.project_not_found()
    project.remove_space(space)
    project.save()
    return '', 200
Пример #4
0
def remix_project(slug, title, **kwargs):
    project = Project.query.filter_by(slug=slug).first()
    spaces = project.spaces
    if not project:
        raise InvalidUsage.project_not_found()
    db.session.expunge(project)
    make_transient(project)
    project.author_id = current_user.profile.id
    project.author = current_user.profile
    project.title = title
    project.id = None
    project.created_at = None
    project.updated_at = None
    project.slug = slugify(title + "-" + str(uuid.uuid4().hex[:6].upper()))

    for space_item in spaces:
        space = Space.query.filter_by(id=space_item.id).first()
        fields = space.fields
        db.session.expunge(space)
        make_transient(space)
        space.id = None
        space.author = current_user.profile
        db.session.add(space)

        for field_item in fields:
            field = Field.query.filter_by(id=field_item.id).first()
            # accessing the relations seems to keep item in the new field
            if hasattr(field, 'file'):
                file = field.file
            # added since first text in a multi space lesson is not taking
            # the value unless it's accessed, not sure why
            if hasattr(field, 'value'):
                value = field.value
            db.session.expunge(field)
            make_transient(field)
            field.id = None
            # field.space = space
            field.space_id = space.id
            field.author_id = current_user.profile.id
            field.author = current_user.profile
            db.session.add(field)
            db.session.commit()

        project.add_space(space)

    db.session.add(project)
    db.session.commit()
    return project
Пример #5
0
def create_space(project_slug, type):
    project = Project.query.filter_by(
        slug=project_slug, author_id=current_user.profile.id).first()

    if not project:
        raise InvalidUsage.project_not_found()
    if type is None:
        type = 'default'
    space = Space(author=current_user.profile, type=type)
    space.save()

    config = project.theme.config
    space_to_create = next(
        (item for item in config['spaces'] if item['type'] == type), None)
    fields_to_generate = space_to_create['fields']
    generate_fields(space, fields_to_generate)

    project.add_space(space)
    project.save()
    return space
Пример #6
0
def get_project_theme(slug):
    project = Project.query.filter_by(slug=slug).first()
    if not project:
        raise InvalidUsage.project_not_found()
    return project.theme
Пример #7
0
def get_project_by_code(code):
    project = Project.query.filter_by(code=code).first()
    if not project:
        raise InvalidUsage.project_not_found()
    return project
Пример #8
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
Пример #9
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