예제 #1
0
def upload_image():
    if "image" not in request.files:
        return {"errors": "image required"}, 400

    image = request.files["image"]

    if not allowed_file(image.filename):
        return {"errors": "file type not permitted"}, 400

    image.filename = get_unique_filename(image.filename)

    upload = upload_file_to_s3(image)

    if "url" not in upload:
        # if the dictionary doesn't have a filename key
        # it means that there was an error when we tried to upload
        # so we send back that error message
        return upload, 400

    url = upload["url"]
    # we can use the
    new_image = Image(user=current_user, url=url)
    db.session.add(new_image)
    db.session.commit()
    return {"url": url}
예제 #2
0
def edit_dog(dog_id):
    dog = Dog.query.filter(Dog.id == dog_id).first()
    if 'image' in request.files:
        image = request.files["image"]

        if not allowed_file(image.filename):
            return {"errors": "file type not permitted"}, 400

        image.filename = get_unique_filename(image.filename)

        upload = upload_file_to_s3(image)

        if "url" not in upload:
            # if the dictionary doesn't have a url key
            # it means that there was an error when we tried to upload
            # so we send back that error message
            return upload, 400
        dog.dog_img = url
    if 'daily_goal' in request.form:
        dog.daily_goal = request.form['daily_goal']
    if "weight" in request.form:
        dog.weight = request.form['weight']
    if "description" in request.form:
        dog.description = request.form['description']
    db.session.commit()
    return {"dog": dog.to_dict()}
예제 #3
0
def upload_image(id):
    if "image" not in request.files:
        return print("no image")

    image = request.files["image"]

    if not allowed_file(image.filename):
        return {"errors": "file type not permitted"}, 400

    image.filename = get_unique_filename(image.filename)

    upload = upload_file_to_s3(image)

    if "url" not in upload:
        # if the dictionary doesn't have a url key
        # it means that there was an error when we tried to upload
        # so we send back that error message
        return upload, 400

    url = upload["url"]
    # flask_login allows us to get the current user from the request
    new_image = MyAWS(spot_id=id,
                      aws_url=url,
                      rss_feed_url="arn:aws:s3:::captain-hagi")
    db.session.add(new_image)
    db.session.commit()

    return {"url": url}
예제 #4
0
def upload_image():
    if "image" not in request.files:
        return {"errors": "image required"}, 400

    image = request.files["image"]

    if not allowed_file(image.filename):
        return {"errors": "file type not permitted"}, 400

    image.filename = get_unique_filename(image.filename)

    upload = upload_file_to_s3(image)

    if "url" not in upload:
        # if the dictionary doesn't have a url key
        # it means that there was an error when we tried to upload
        # so we send back that error message
        return upload, 400

    url = upload["url"]
    # flask_login allows us to get the current user from the request
    remove_image = delete(UserImage).where(
        UserImage.user_id == current_user.id).execution_options(
            synchronize_session="fetch")
    db.session.execute(remove_image)
    db.session.commit()
    new_image = UserImage(user_id=current_user.id, img_url=url)
    db.session.add(new_image)
    db.session.commit()
    return {"url": url}
예제 #5
0
def add_activity():
    url = ''
    if "image" in request.files:
        image = request.files["image"]
        if not allowed_file(image.filename):
            return {"errors": "file type not permitted"}, 400
        image.filename = get_unique_filename(image.filename)

        upload = upload_file_to_s3(image)

        if "url" not in upload:
            # if the dictionary doesn't have a url key
            # it means that there was an error when we tried to upload
            # so we send back that error message
            return upload, 400
        url = upload["url"]
    form = NewActivityForm(request.form)
    form['csrf_token'].data = request.cookies['csrf_token']
    if form.validate():
        actDate = datetime.datetime.strptime(request.form.get("date"),
                                             '%m/%d/%Y, %I:%M:%S %p')
        print(actDate)
        newActivity = DogActivity(dog_id=form.data["dog_id"],
                                  name=form.data['name'],
                                  activityType_id=form.data["activityType_id"],
                                  activity_img=url,
                                  minutes=form.data["minutes"],
                                  route_id=form.data["route_id"],
                                  date=request.form.get("date"))
        db.session.add(newActivity)
        db.session.commit()
        return newActivity.to_dict()
    return {"errors": validation_errors_to_error_messages(form.errors)}, 401
예제 #6
0
def uploadTrackVersion(artistId, projectId, trackId):
    '''
    Posts a new mix version to AWS [X]
    Updates on database [X]
    Updates on AWS [X]
    '''
    user = current_user
    artist = User.query.get(artistId)
    project = Project.query.get(projectId)
    track = Track.query.get(trackId)
    if user.superUser:
        if track:
            if track.projectId == project.id:
                if project.artistId == artist.id:
                    if project.engineerId == user.id:
                        if "file" not in request.files:
                            return {"errors": "file required"}, 400
                        file = request.files["file"]

                        if not allowed_file(file.filename):
                            return {"errors": "file type not permitted"}, 400

                        file.filename = get_unique_filename(file.filename)
                        upload = upload_file_to_s3(file)
                        db.session.flush()
                        if "url" not in upload:
                            return upload, 400
                        url = upload["url"]
                        data = Version(url=upload["url"],
                                       length=3,
                                       trackId=track.id)
                        db.session.add(data)
                        db.session.commit()
                        return data.to_dict()
                    else:
                        return {
                            "Errors":
                            f"{user.firstName} {user.lastName} cannot delete '{project.name}' because they are not on the project"
                        }  # noqa
                else:
                    return {
                        "Errors":
                        f"'{project.name}' does not belong to {artist.firstName} {artist.lastName}"
                    }  # noqa
            else:
                return {
                    "Errors":
                    f"'{track.name}' does not belong to {project.name}"
                }  # noqa
        else:
            return {"Errors": f"Track does not exist!"}
    else:
        return {
            "Errors":
            f"{user.firstName} {user.lastName} is not authorized to perform this action."
        }  # noqa
예제 #7
0
def upload_file():
    if "file" not in request.files:
        return {"errors": "file required"}, 400
    file = request.files["file"]

    if not allowed_file(file.filename):
        return {"errors": "file type not permitted"}, 400

    file.filename = get_unique_filename(file.filename)

    upload = upload_file_to_s3(file)

    if "url" not in upload:
        return upload, 400

    url = upload["url"]
    return {"url": url}
예제 #8
0
def upload_image():

    if "image" not in request.files:
        return {"errors": "image required"}, 400

    image = request.files["image"]

    if not allowed_file(image.filename):
        return {"errors": "file type not permitted"}, 400

    image.filename = get_unique_filename(image.filename)

    upload = upload_file_to_s3(image)

    if "url" not in upload:
        # if the dictionary doesn't have a url key
        # it means that there was an error when we tried to upload
        # so we send back that error message
        return upload, 400

    url = upload["url"]
    name = request.form['name']
    birthday = request.form['birthday']
    weight = int(request.form['weight'])
    description = request.form["description"]
    breed_id = int(request.form['breed_id'])
    # flask_login allows us to get the current user from the request
    new_dog = Dog(user_id=current_user.id,
                  dog_img=url,
                  name=name,
                  breed_id=breed_id,
                  birthday=birthday,
                  weight=weight,
                  description=description)
    db.session.add(new_dog)
    db.session.commit()
    return {"url": url}
예제 #9
0
def add_activity_image(activity_id):
    if "image" not in request.files:
        return {"errors": "image required"}, 400

    image = request.files["image"]

    if not allowed_file(image.filename):
        return {"errors": "file type not permitted"}, 400
    image.filename = get_unique_filename(image.filename)

    upload = upload_file_to_s3(image)

    if "url" not in upload:
        # if the dictionary doesn't have a url key
        # it means that there was an error when we tried to upload
        # so we send back that error message
        return upload, 400

    url = upload["url"]
    updatedActivity = DogActivity.query.filter(
        DogActivity.id == activity_id).first()
    updatedActivity.activity_img = url
    db.session.commit()
    return updatedActivity.to_dict()
예제 #10
0
def create_character():

    form = CharacterForm()
    form['csrf_token'].data = request.cookies['csrf_token']
    # if form.validate_on_submit():
    if form.validate_on_submit():
        #  s3 image helper functions. Steps: test if image is in files,
        #  grab image, check if it is required type,
        #  give it a unique name so aws does not overwrite,
        #  then upload to aws, returning any errors if upload fails.

        if "image" not in request.files:
            return {"errors": ["image required"]}
        image = request.files["image"]
        if not allowed_file(image.filename):
            return {"errors": ["file type not permitted"]}
        image.filename = get_unique_filename(image.filename)
        upload = upload_file_to_s3(image)
        if "url" not in upload:
            return {"errors": [upload['errors']]}
        url = upload["url"]
        #  create a new instance of the character class
        character = Character()
        # populate character instance with data from form & aws url
        character = Character(userId=form.data['userId'],
                              name=form.data['name'],
                              level=form.data['level'],
                              race=form.data['race'],
                              characterClass=form.data['characterClass'],
                              subclass=form.data['subclass'],
                              hitpoints=form.data['hitpoints'],
                              speed=form.data['speed'],
                              imgURL=url,
                              proficiencies=form.data['proficiencies'],
                              background=form.data['background'],
                              alignment=form.data['alignment'],
                              attributes=form.data['attributes'],
                              traits=form.data['traits'],
                              ideals=form.data['ideals'],
                              bonds=form.data['bonds'],
                              flaws=form.data['flaws'],
                              inventory=form.data['inventory'],
                              description=form.data['description'],
                              languages=form.data['languages'],
                              tools=form.data['tools'])
        # Take care of tag creation
        #  grab tags and query to check if they are created already
        if len(form.data['tags']) > 0:
            tagsFormatted = [
                tag.strip() for tag in form.data['tags'].split(",")
            ]
            for tag in tagsFormatted:
                queriedTag = Tag.query.filter(Tag.name == tag).first()
                if (queriedTag):
                    character.tags.append(queriedTag)
                else:
                    tag = Tag(name=tag)
                    character.tags.append(tag)
        classTag = Tag.query.filter(
            Tag.name == form.data['characterClass']).first()
        raceTag = Tag.query.filter(Tag.name == form.data['race']).first()

        character.tags.append(classTag)
        character.tags.append(raceTag)
        # Take care of abilites and ability appending
        for feature in json.loads(form.data['features']):
            feature_to_add = (Ability.query.filter(
                Ability.name == feature['name']).first())
            if (feature_to_add):
                character.abilities.append(feature_to_add)
            else:
                return {"errors": "Failed to add ability"}

        # add and commit character
        db.session.add(character)
        db.session.commit()
        return character.to_dict()
    return {'errors': validation_errors_to_error_messages(form.errors)}