def pet_add(): """"Add a pet""" form = PetForm() if form.validate_on_submit(): name = form.name.data species = form.species.data age = form.age.data if form.age.data else None img_url = form.img_url.data if form.img_url.data else None notes = form.notes.data if form.notes.data else None available = form.available.data if form.available.data else None pet = Pet( name=name, species=species, age=age, img_url=img_url, notes=notes, available=available, ) db.session.add(pet) db.session.commit() return redirect('/') else: return render_template("add_pet.html", form=form)
def new_pet(): """ form to add new pet & validation to add to database """ form = PetForm() if form.validate_on_submit(): # ===original method below==== # 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) # method to loop over dictionary so that it updates if new fields get added data = {k: v for k, v in form.data.items() if k != "csrf_token"} new_pet = Pet(**data) # the input data looks like === new_pet = Pet(name=form.name.data, age=form.age.data, ...) db.session.add(new_pet) db.session.commit() flash(f'{new_pet.name} added', 'alert alert-success') return redirect('/') else: return render_template('new_pet_form.html', form=form)
def add_pets_route(): form = PetForm() if form.validate_on_submit(): name = form.name.data species = form.species.data if form.photo_url.data: photo_url = form.photo_url.data else: photo_url = 'https://st4.depositphotos.com/14953852/24787/v/600/depositphotos_247872612-stock-illustration-no-image-available-icon-vector.jpg' age = form.age.data notes = form.notes.data available = bool(form.available.data) db.session.add( Pet(name=name, species=species, photo_url=photo_url, age=age, notes=notes, available=available)) db.session.commit() flash(f"Added {name},({species}) has been added") return redirect("/") else: return render_template("new-pets.html", form=form)
def add_pet(): """ Show the add pet-form, and if submitted recieve it as a post request and add it to the database""" form = PetForm() if form.validate_on_submit(): name = form.name.data species = form.species.data photo_url = form.photo_url.data if form.photo_url.data else None age = form.age.data if form.age.data else None notes = form.notes.data if form.notes.data else None new_pet = Pet(name=name, species=species, photo_url=photo_url, age=age, notes=notes) db.session.add(new_pet) db.session.commit() return redirect("/") else: return render_template("add_pet.html", form=form)
def pet_edit_route(pet_id): data_pet = Pet.query.get_or_404(pet_id) form = PetForm(obj=data_pet) if form.validate_on_submit(): name = form.name.data species = form.species.data if form.photo_url.data: photo_url = form.photo_url.data else: photo_url = 'https://st4.depositphotos.com/14953852/24787/v/600/depositphotos_247872612-stock-illustration-no-image-available-icon-vector.jpg' age = form.age.data notes = form.notes.data available = form.available.data data_pet.name = name data_pet.species = species data_pet.photo_url = photo_url data_pet.age = age data_pet.notes = notes data_pet.available = available db.session.add(data_pet) db.session.commit() flash(f"updated {name},({species})") return redirect("/") else: return render_template("new-pets.html", form=form)
def add_pet(): """Pet add form; handle adding""" form = PetForm() # check if it's a post request AND validate the token if form.validate_on_submit(): # Because of this validate_on_submit(), we can get the data directly from the form object name = form.name.data species = form.species.data photo_url = form.photo_url.data age = form.age.data notes = form.notes.data # create the new pet new_pet = Pet(name=name, species=species, photo_url=photo_url, age=age, notes=notes) db.session.add(new_pet) db.session.commit() flash(f"Added new pet: {name}") return redirect("/") else: return render_template("pet_add_form.html", form=form)
def add_pet(): """handle add a pet form.""" form = PetForm() if form.validate_on_submit(): name = form.name.data species = form.species.data photo_url = form.photo_url.data notes = form.notes.data age = form.age.data available = form.available.data photo_upload = upload_file(form.photo_upload.name) pet = Pet(name=name, species=species, photo_url=photo_url, photo_upload=photo_upload, notes=notes, age=age, available=available) db.session.add(pet) db.session.commit() flash(f"{name} is added!") return redirect(f"/{pet.id}") else: return render_template("add.html", form=form)
def add_new_pet(): form = PetForm() if form.validate_on_submit(): data = {key: value for key, value in form.data.items()} if data.get('photo_url') and data.get('photo_file'): flash('Enter a valid URL or upload a file, not both', 'danger') return render_template('add_pet.html', form=form) if data.get('photo_file'): img_name = data['photo_file'].filename.split('.') img_name[0] = data['name'] data['photo_file'].filename = ".".join(img_name) data['photo_file'].save( secure_filename(data['photo_file'].filename)) data['photo_url'] = data['photo_file'].filename data.pop('photo_file') data.pop('csrf_token') new_pet = Pet(**data) db.session.add(new_pet) db.session.commit() flash(f'{new_pet.name} is added to list', 'success') return redirect(url_for('home')) else: return render_template('add_pet.html', form=form)
def add_pet(): """ GET and POST for adding a pet""" form = PetForm() if (form.validate_on_submit()): Pet.add_from_form(form) return redirect('/') return render_template('pet_form.html', form=form)
def show_add_pet_form(): """Route to add a pet""" form = PetForm() if form.validate_on_submit(): pet = create_pet(form) db.session.add(pet) db.session.commit() return redirect('/') else: return render_template('add_pet.html', form=form)
def edit_pet(pet_id): pet = Pet.query.get_or_404(pet_id) form = PetForm(obj=pet) if form.validate_on_submit(): pet.notes = form.notes.data pet.available = form.available.data pet.photo_url = form.photo_url.data db.session.commit() flash(f"{pet.name} updated.") return redirect(url_for('list_pets')) render_template('edit_pet_form.html', form=form)
def add_pets(): form = PetForm() if form.validate_on_submit(): new = Pet(name=form.name.data, species=form.species.data, photo_url=form.photo_url.data, age=form.age.data, notes=form.notes.data) db.session.add(new) db.session.commit() return redirect('/') else: return render_template("add_pets.html", form=form)
def edit_pets(id): pet = Pet.query.get_or_404(id) form = PetForm(obj=pet) if form.validate_on_submit(): 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.commit() return redirect('/') else: return render_template('details.html', form=form)
def display_pet(pet_id): '''GET pet page. POST edits to pet.''' pet = Pet.query.get_or_404(pet_id) form = PetForm(obj=pet) if form.validate_on_submit(): pet.photo_url = form.photo_url.data pet.notes = form.notes.data pet.available = form.available.data db.session.commit() return redirect('/') else: return render_template('pet.html', form=form, pet=pet)
def booking_edit(pet_id, book_id): "return edit booking page" book = Booking.query.get_or_404(book_id) if is_not_renter_book_match(book): return redirect("/") form = PetForm(obj=book) if form.validate_on_submit(): book.startTime = form.start_date.data, book.endTime = form.end_date.data db.session.commit() return redirect(f"/renter") else: return render_template("pet.html", id=pet_id, form=form, editing=True)
def render_owner_pet_new(): form = PetForm() contact = current_user.contact if request.method == 'POST' and form.validate_on_submit(): petname = form.petname.data category = form.category.data age = form.age.data query = "INSERT INTO pets(petname, pcontact, age, category) VALUES ('{}', '{}', '{}', '{}')" \ .format(petname, contact, age, category) db.session.execute(query) db.session.commit() return redirect(url_for('view.render_owner_pet')) return render_template("petNew.html", form=form, username=current_user.username + " owner")
def pet_detail_edit(pet_id): """"You can see and update some details about the pet if you want""" pet = Pet.query.get_or_404(pet_id) form = PetForm(obj=pet) if form.validate_on_submit(): pet.img_url = form.img_url.data if form.img_url.data else pet.img_url pet.notes = form.notes.data if form.notes.data else pet.notes pet.available = form.available.data if form.available.data else pet.available db.session.add(pet) db.session.commit() return redirect('/') else: return render_template("detail_edit_pet.html", pet=pet, form=form)
def show_and_edit_pet(pet_id): pet = Pet.query.get_or_404(pet_id) form = PetForm(obj=pet) if form.validate_on_submit(): pet.photo_url = form.photo_url.data pet.notes = form.notes.data pet.available = form.available.data db.session.commit() flash("IT's updated!") print('passed') return redirect('/') else: print('not validated') return render_template('show-pet.html', pet=pet, form=form)
def display_edit_pet(id): """Show pet info and allow to edit pet""" pet = Pet.query.get_or_404(id) form = PetForm(obj=pet) if form.validate_on_submit(): pet.name = form.name.data pet.species = form.species.data pet.photo_url = form.photo_url.data pet.age = form.age.data pet.available = form.available.data pet.notes = form.notes.data db.session.add(pet) db.session.commit() return redirect('/') else: return render_template('display_pet.html', form=form)
def edit_pet(id): """routes for pet details and edit page""" pet = Pet.query.get_or_404(id) form = PetForm(obj=pet) if form.validate_on_submit(): pet.name = form.name.data pet.species = form.species.data pet.image_url = form.image_url.data pet.age = form.age.data pet.notes = form.notes.data db.session.commit() return redirect('/') else: return render_template("edit_pet_form.html", form=form, pet=pet)
def edit_pet(pet_id): """ View / Edit details for pet """ pet = Pet.query.get_or_404(pet_id) form = PetForm(obj=pet) if form.validate_on_submit(): pet.name = pet.name pet.species = pet.species pet.photo_url = form.photo_url.data pet.notes = form.notes.data pet.available = form.available.data db.session.commit() return redirect('/') else: print(form.validate_on_submit()) print(form.errors) return render_template('edit_pet.html', form=form, pet=pet)
def add_pet(): form = PetForm() 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 pet = Pet(name=name, species=species, photo_url=photo_url, age=age, notes=notes) db.session.add(pet) db.session.commit() flash(f"Added {name}, age {age} to database") return redirect('/') else: return render_template('add.html', form=form)
def add_pet(): """routes for add a pet form""" form = PetForm() if form.validate_on_submit(): name = form.name.data species = form.species.data image_url = form.image_url.data age = form.age.data notes = form.notes.data pet = Pet(name=name, species=species, image_url=image_url, age=age, notes=notes) db.session.add(pet) db.session.commit() return redirect('/') else: return render_template("pet-form.html", form=form)
def add_pet(): """Pet add form; handle adding.""" form = PetForm() if form.validate_on_submit(): name = form.name.data species = form.species.data image = form.image.data age = form.age.data notes = form.notes.data available = form.available.data pet = Pet(name=name, species=species, image=image, age=age,notes=notes,available=available) db.session.add(pet) db.session.commit() flash(f"Added new pet: a ${species} named ${name}.") return redirect('/') else: return render_template('add_pet_form.html', form=form)
def view_pet(pet_id): pet = Pet.query.get_or_404(pet_id) form = PetForm(obj=pet) species = [(pet.species, pet.species) for pet in Pet.query.all()] form.species.choices = species if form.validate_on_submit(): pet.name = form.name.data pet.species = form.species.data pet.photo_url = form.photo_url.data pet.notes = form.notes.data pet.age = form.age.data pet.available = form.available.data db.session.add(pet) db.session.commit() return redirect('/') else: return render_template('edit-pet.html', form=form, pet=pet)
def create_new_pet(): form = PetForm() if form.validate_on_submit(): name = form.name.data speacies = form.speacies.data photo = form.photo.data age = form.age.data notes = form.notes.data pet = Pet(name=name, speacies=speacies, photo=photo, age=age, notes=notes) db.session.add(pet) db.session.commit() return redirect('/') else: return render_template('new_pet.html', form=form)
def render_owner_pet_new(): form = PetForm() contact = current_user.contact if request.method == 'POST' and form.validate_on_submit(): petname = form.petname.data category = form.category.data age = form.age.data query = "INSERT INTO pets(petname, pcontact, age, category) VALUES ('{}', '{}', '{}', '{}')" \ .format(petname, contact, age, category) try: db.session.execute(query) db.session.commit() return redirect(url_for('view.render_owner_pet')) except exc.IntegrityError: db.session.rollback() flash("You already have a pet with the same name!") return render_template("petNew.html", form=form, username=current_user.username)
def pet_details(pet_id): """View function for Showing Details of Each Pet.""" form = PetForm() pet = Pet.query.get(pet_id) if pet is None: abort(404, description="No Pet was Found with the given ID") if form.validate_on_submit(): pet.name = form.name.data pet.age = form.age.data pet.bio = form.bio.data try: db.session.commit() except Exception as e: db.session.rollback() return render_template( "details.html", pet=pet, form=form, message="A Pet with this name already exists!") return render_template("details.html", pet=pet, form=form)
def add_pet(): """ Add pet form """ form = PetForm() 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 pet = Pet(name=name, species=species, photo_url=photo_url, age=age, notes=notes) db.session.add(pet) db.session.commit() return redirect('/') else: return render_template('add_pet.html', form=form)
def edit_pet(pet_id): """ should display page to edit PET's details """ pet = Pet.query.get_or_404(pet_id) form = PetForm(obj=pet) if form.validate_on_submit(): pet.name = form.name.data pet.species = form.species.data pet.url_photo = form.photo_url.data pet.age = form.age.data pet.notes = form.notes.data pet.available = form.available.data db.session.add(pet) db.session.commit() return redirect(f'/{pet_id}') else: return render_template('edit-pet.html', form=form, pet=pet)