def createCharacter():
    form = CharacterForm()
    character = Character(userId=form.data['userId'],
                          name=form.data['name'],
                          level=form.data['level'],
                          characterClass=form.data['characterClass'],
                          race=form.data['race'],
                          alignment=form.data['alignment'],
                          proficiencies=form.data['proficiencies'],
                          personality__traits=form.data['traits'],
                          ideals=form.data['ideals'],
                          bonds=form.data['bonds'],
                          flaws=form.data['flaws'],
                          strength=form.data['strength'],
                          dex=form.data['dex'],
                          constitution=form.data['constitution'],
                          intelligence=form.data['intelligence'],
                          wisdom=form.data['wisdom'],
                          charisma=form.data['charisma'],
                          saving__throw__one=form.data['savingThrowOne'],
                          saving__throw__two=form.data['savingThrowTwo'],
                          skill_one=form.data['skillOne'],
                          skill_two=form.data['skillTwo'],
                          skill_three=form.data['skillThree'],
                          skill_four=form.data['skillFour'])
    db.session.add(character)
    db.session.commit()
    return character.to_dict()
Ejemplo n.º 2
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)}