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)
def show_add_pet_form(): """ render form to add pet """ form = AddPetForm() if form.validate_on_submit(): pet = Pet() pet.name = form.name.data pet.species = (form.species.data).lower() pet.photo_url = form.photo_url.data pet.age = form.age.data pet.notes = form.notes.data pet.available = form.available.data flash(f"Added {pet.name} to adoptable pets!") db.session.add(pet) db.session.commit() return redirect("/") else: return render_template('add_pet_form.html', form=form)
def add_pet(): form = AddPetForm() if form.validate_on_submit(): pet = Pet() pet.name = form.name.data pet.species = form.species.data pet.photo_url = form.photo_url.data pet.age = form.age.data pet.notes = form.notes.data db.session.add(pet) db.session.commit() flash(f'Added {pet.name}!') return redirect('/') else: return render_template('add_pet.html', form=form)
def add_pet(): """ Add a pet for adoption """ form = AddPetForm() if form.validate_on_submit(): pet = Pet() pet.name = form.name.data pet.species = form.species.data pet.photo_url = form.photo_url.data pet.age = form.age.data pet.notes = form.notes.data db.session.add(pet) db.session.commit() return redirect("/") else: return render_template("add_new_pet.html", form=form)
def add_pet(): """Route that both shows the form to add a new pet and that adds a new pet""" form = AddPetForm() if form.validate_on_submit(): form_data = form.data form_data.pop('csrf_token', None) form_data = form_data new_pet = Pet(**form_data) if not new_pet.photo_url: new_pet.photo_url = None db.session.add(new_pet) db.session.commit() flash(f"Added new pet: {new_pet.name}!") return redirect('/') else: return render_template('add_pet_form.html', form=form)