Beispiel #1
0
def add_pet():
    """Add a pet to the list"""
    form = AddPetForm()
    if form.validate_on_submit():
        # name = form.name.data
        # species = form.species.data
        # photo_url = form.photo_url.data
        # age = form.age.data
        # notes = form.notes.data
        # available = form.available.data

        # pet = Pet(name = name, species = species, photo_url = photo_url, age = age, notes = notes, available = available)

        data = {
            k: v
            for k, v in form.data.items() if k != 'csrf_token' and k != 'file'
        }
        new_pet = Pet(**data)

        if form.file.data.filename != '':
            filename = images.save(form.file.data)
            # if filename != None:
            new_pet.photo_url = f'static/{filename}'
        else:
            new_pet.photo_url = Pet.image_url(new_pet)

        db.session.add(new_pet)
        db.session.commit()
        flash(f'{new_pet.name} is added.')
        return redirect('/')
    else:
        return render_template('add_pet.html', form=form)