Exemple #1
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
Exemple #2
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
Exemple #3
0
def get_activity_reactions(classroom_slug, code):
    classroom = Classroom.query.filter_by(slug=classroom_slug).first()
    if not classroom:
        raise InvalidUsage.classroom_not_found()
    activity = Activity.query.filter_by(classroom=classroom, code=code).first()
    if not activity:
        raise InvalidUsage.item_not_found()
    return activity.reactions
Exemple #4
0
def check_file_extension_for_type(type, file_extension):
    if type == 'image' or type == 'photosphere':
        if file_extension not in ['.png', '.jpg', '.jpeg']:
            raise InvalidUsage.invalid_file_type()
    elif type == 'audio':
        if file_extension not in ['.mp3']:
            raise InvalidUsage.invalid_file_type()
    elif type == 'video' or type == 'videosphere':
        if file_extension not in ['.mp4']:
            raise InvalidUsage.invalid_file_type()
Exemple #5
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
Exemple #6
0
def create_classroom(classname, **kwargs):
    try:
        school = current_user.profile.school
        if not school:
            raise InvalidUsage.no_associated_school()
        classroom = Classroom(classname=classname,
                              teacher=current_user.profile,
                              school=school).save()
    except IntegrityError:
        db.session.rollback()
        raise InvalidUsage.item_already_exists()
    return classroom
Exemple #7
0
def toggle_approval_submission(code, submission_id):
    activity = Activity.query.filter_by(code=code).first()
    if not activity:
        raise InvalidUsage.item_not_found()
    submission = Submission.query.filter_by(activity=activity,
                                            id=submission_id).first()
    if not submission:
        raise InvalidUsage.item_not_found()
    new_approved_state = not submission.approved
    submission.update(approved=new_approved_state)
    submission.save()
    return submission
Exemple #8
0
def unfollow_user(username):
    user = User.query.filter_by(username=username).first()
    if not user:
        raise InvalidUsage.user_not_found()
    current_user.profile.unfollow(user.profile)
    current_user.profile.save()
    return user.profile
Exemple #9
0
def create_school(name, country, region):
    try:
        school = School(name=name, country=country, region=region).save()
    except IntegrityError:
        db.session.rollback()
        raise InvalidUsage.item_already_exists()
    return school
Exemple #10
0
def make_project(title, description, theme_slug, tags=None):
    theme = Theme.query.filter_by(slug=theme_slug).first()
    if not theme:
        raise InvalidUsage.theme_not_found()
    project = Project(title=title,
                      description=description,
                      author=current_user.profile,
                      theme=theme)
    if tags is not None:
        for tag in tags:
            mtag = Tags.query.filter_by(tagname=tag).first()
            if not mtag:
                mtag = Tags(tag)
                mtag.save()
            project.add_tag(mtag)

    config = theme.config

    if len(config['spaces']) == 1:
        space = Space(author=current_user.profile)
        space.save()

        fields_to_generate = config['spaces'][0]['fields']
        generate_fields(space, fields_to_generate)
        project.add_space(space)

    project.save()
    return project
Exemple #11
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
Exemple #12
0
def delete_space(space_id):
    space = Space.query.filter(
        Space.id == space_id,
        Space.author_id == current_user.profile.id).first()
    if not space:
        raise InvalidUsage.space_not_found()
    space.delete()
    return '', 200
Exemple #13
0
def update_theme(slug, **kwargs):
    theme = Theme.query.filter_by(slug=slug,
                                  author_id=current_user.profile.id).first()
    if not theme:
        raise InvalidUsage.theme_not_found()
    theme.update(udpatedAt=datetime.datetime.utcnow(), **kwargs)
    theme.save()
    return theme
Exemple #14
0
def get_activity_submissions_by_code(code):
    activity = Activity.query.filter_by(code=code).first()
    if not activity:
        raise InvalidUsage.item_not_found()
    submissions = Submission.query.filter_by(
        activity=activity,
        approved=True).order_by(Submission.created_at.desc()).all()
    return {'activity': activity, 'submissions': submissions}
Exemple #15
0
def unfavorite_a_project(slug):
    profile = current_user.profile
    project = Project.query.filter_by(slug=slug).first()
    if not project:
        raise InvalidUsage.project_not_found()
    project.unfavourite(profile)
    project.save()
    return project
Exemple #16
0
def get_activity_submissions(code, classroom_slug):
    activity = Activity.query.join(
        Activity.classroom).filter(Classroom.slug == classroom_slug).filter(
            Activity.code == code).first()
    if not activity:
        raise InvalidUsage.item_not_found()
    submissions = Submission.query.filter_by(activity=activity).order_by(
        Submission.created_at.desc()).all()
    return {'activity': activity, 'submissions': submissions}
Exemple #17
0
def update_field(field_id, **kwargs):
    field = Field.query.filter(
        Field.id == field_id,
        Field.project.author_id == current_user.profile.id).first()
    if not field:
        raise InvalidUsage.field_not_found()
    field.update(**kwargs)
    field.save()
    return field
Exemple #18
0
def register_user(username, password, email, **kwargs):
    try:
        userprofile = UserProfile(
            User(username, email, password=password, **kwargs).save()).save()
        userprofile.user.token = create_access_token(identity=userprofile.user)
    except IntegrityError:
        db.session.rollback()
        raise InvalidUsage.user_already_registered()
    return userprofile.user
Exemple #19
0
def make_theme(title, description, **kwargs):
    try:
        theme = Theme(title=title,
                      description=description,
                      author=current_user.profile,
                      **kwargs).save()
    except IntegrityError:
        db.session.rollback()
        raise InvalidUsage.theme_already_exists()
    return theme
Exemple #20
0
def create_activity_type(title, instructions, pdf_link, **kwargs):
    try:
        activity_type = ActivityType(title=title,
                                     instructions=instructions,
                                     pdf_link=pdf_link,
                                     **kwargs).save()
    except IntegrityError:
        db.session.rollback()
        raise InvalidUsage.item_already_exists()
    return activity_type
Exemple #21
0
def create_activity(classroom_slug, activity_name, activity_type_id, **kwargs):
    classroom = Classroom.query.filter_by(slug=classroom_slug).first()
    if not classroom:
        raise InvalidUsage.classroom_not_found()
    while True:
        code = secrets.token_hex(3)
        existing_activity = Activity.query.filter_by(code=code).first()
        if not existing_activity:
            break
    try:
        activity = Activity(activity_type_id=activity_type_id,
                            activity_name=activity_name, classroom=classroom, code=code)
        if 'reaction_to_id' in kwargs:
            reaction_to = Activity.get_by_id(kwargs['reaction_to_id'])
            activity.reaction_to = reaction_to
            activity.is_reaction = True
        activity.save()
    except IntegrityError:
        db.session.rollback()
        raise InvalidUsage.item_already_exists()
    return activity
Exemple #22
0
def login_user(userid, password, **kwargs):

    # supports logging in with either email or username
    user = None
    if re.match(r"[^@]+@[^@]+\.[^@]+", userid):
        user = User.query.filter_by(email=userid.lower()).first()
    else:
        user = User.query.filter_by(username=userid.lower()).first()
    if user is not None and user.check_password(password):
        user.token = create_access_token(identity=user, fresh=True)
        return user
    else:
        raise InvalidUsage.user_not_found()
Exemple #23
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
Exemple #24
0
def make_project(title, description, theme_slug, tags=None):
    theme = Theme.query.filter_by(slug=theme_slug).first()
    if not theme:
        raise InvalidUsage.theme_not_found()
    project = Project(title=title,
                      description=description,
                      author=current_user.profile,
                      theme=theme)
    if tags is not None:
        for tag in tags:
            mtag = Tags.query.filter_by(tagname=tag).first()
            if not mtag:
                mtag = Tags(tag)
                mtag.save()
            project.add_tag(mtag)
    project.save()
    return project
Exemple #25
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
Exemple #26
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
Exemple #27
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
Exemple #28
0
def get_space(space_id):
    space = Space.get_by_id(space_id)
    if not space:
        raise InvalidUsage.space_not_found()
    return space
Exemple #29
0
def get_theme(slug):
    theme = Theme.query.filter_by(slug=slug).first()
    if not theme:
        raise InvalidUsage.theme_not_found()
    return theme
Exemple #30
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