Пример #1
0
def add_pet():
    """ add pet form and handle adding """

    form = AddPet()

    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
        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"{new_pet.name} added!")
        return redirect("/")

    else:
        return render_template("add.html", form=form)
Пример #2
0
def add_pet():
    """Add pet page & functionality to add pet to database"""
    form = AddPet()

    if form.validate_on_submit():
        name = form.name.data
        species = form.species.data
        age = form.age.data
        notes = form.notes.data
        available = form.available.data
        photo_url = form.photo_url.data
        if not photo_url:
            photo_url = 'https://bitsofco.de/content/images/2018/12/broken-1.png'

        new_pet = Pet(name=name,
                      species=species,
                      photo_url=photo_url,
                      age=age,
                      notes=notes,
                      available=available)

        print(photo_url)

        db.session.add(new_pet)
        db.session.commit()
        return redirect('/')
    else:
        return render_template('add-pet.html', form=form)
Пример #3
0
def pet_add():
    """Handler for AddPet form.

    Includes validation of AddPet form. If form is successfully validated -> add
    pet to database. If invalid response -> return the add page again. 
    """

    form = AddPet()

    # handle form validation
    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"{name} has been added to the {species} species!")
        return redirect('/')
    else:
        return render_template('add-pet.html', form=form)
Пример #4
0
def add_pet():
    """Add a pet."""

    form = AddPet()

    if form.validate_on_submit():
        #after validating input, add to database then redirect back to same page
        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}!")

        return redirect("/")

    else:
        #render add pet page on get or submission failure
        return render_template("add_pet_form.html", form=form)
Пример #5
0
def add_pet_form():
    '''Render add pet form.'''

    form = AddPet()

    if form.validate_on_submit():
        name = form.name.data
        species = form.species.data
        photo_url = form.photo_url.data
        if len(photo_url) == 0 or photo_url is None:
            photo_url = Pet.default_image_url
        age = form.age.data
        notes = form.notes.data
        if len(notes) == 0 or notes is None:
            notes = Pet.default_note
        new_pet = Pet(name=name,
                      species=species,
                      photo_url=photo_url,
                      age=age,
                      notes=notes)
        db.session.add(new_pet)
        db.session.commit()

        flash("Pet added")

        return redirect('/')
    else:
        return render_template('add_pet_form.html', form=form)
Пример #6
0
def add_pet():
    form = AddPet()
    if form.validate_on_submit():
        session = db_session.create_session()
        pet = Pet(pet_name=form.pet_name.data,
                  age=form.age.data,
                  type=form.type.data,
                  poroda=form.poroda.data)
        session.merge(current_user)
        current_user.pets.append(pet)
        session.commit()
        return redirect('/my_pets')
    resp = render_template('add_my_pet.html',
                           title='Добавить моего питомца',
                           form=form)
    return resp
Пример #7
0
def add_pet_form():
    """Show add pet form and handle new pet data"""

    form = AddPet()

    if form.validate_on_submit():
        pet_info = {
            key: val
            for key, val in form.data.items() if key != 'csrf_token'
        }
        new_pet = Pet(**pet_info)
        db.session.add(new_pet)
        db.session.commit()
        return redirect('/')
    else:
        return render_template('add_pet.html', form=form)
Пример #8
0
def add_pet():
    form = AddPet()
    species = [(None, 'Choose ...'), ('cat', 'cat'), ('dog', 'dog'),
               ('porcupine', 'porcupine')]
    form.species.choices = species

    if form.validate_on_submit():
        data = {k: v for k, v in form.data.items() if k != "csrf_token"}
        pet = Pets(**data)

        db.session.add(pet)
        db.session.commit()

        return redirect("/")
    else:
        return render_template("add.html", form=form)
Пример #9
0
def add_pet():
    """Show submit form or submit form to add a pet"""
    form = AddPet()

    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_form.html', form=form)
Пример #10
0
def edit_pet(pet_id):
    '''Show Edit Pet form'''
    pet = Pet.query.get_or_404(pet_id)

    form = AddPet(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('edit_pet.html', form=form)
Пример #11
0
def edit_pet_details(pet_id):
    """Get pet details & show on page in editable form"""
    pet = Pet.query.get_or_404(pet_id)
    form = AddPet(obj=pet)

    if form.validate_on_submit():
        pet.name = form.name.data
        pet.species = form.species.data
        pet.age = form.age.data
        pet.notes = form.notes.data
        pet.available = form.available.data
        pet.photo_url = form.photo_url.data

        db.session.commit()
        return redirect(f'/pet-details/{pet_id}')

    else:
        return render_template('edit-pet.html', pet=pet, form=form)
Пример #12
0
def add_pet():
    '''Show Add Pet form'''
    form = AddPet()

    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
        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_new_pet.html', form=form)
Пример #13
0
def edit_pet_form(pet_id):
    '''Render edit pet form and pet details.'''
    pet = Pet.query.get_or_404(pet_id)
    form = AddPet(obj=pet)
    if form.validate_on_submit():

        pet.name = form.name.data
        pet.species = form.species.data
        pet.photo_url = form.photo_url.data
        if len(pet.photo_url) == 0 or pet.photo_url is None:
            pet.photo_url = Pet.default_image_url
        pet.age = form.age.data
        pet.notes = form.notes.data
        if len(pet.notes) == 0 or pet.notes is None:
            pet.notes = Pet.default_note
        db.session.commit()

        flash("Pet edited")

        return redirect('/')
    return render_template('edit_pet_form.html', form=form, pet=pet)
Пример #14
0
def add_pet():
    """Pet add form; handle adding"""
    form = AddPet()
    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

        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_form.html', form=form)
Пример #15
0
def add_pet():
    form = AddPet()

    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'created new pet: name of {name}, species is {species}')
        return redirect('/')
    else:
        return render_template('add_pet_form.html', form=form)
Пример #16
0
def add_pet():
    """the add a pet to the database form"""
    form = AddPet()

    if form.validate_on_submit():
        # if form.photo.data != None:
        #     pic = form.photo.data

        #     filename = images.save(pic)
        # else:
        #     pic = photo_url.field.data
        new_pet = 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_pet)
        db.session.commit()
        flash(f'{new_pet.name} has been added!')
        return redirect('/')

    else:
        return render_template('add_pet.html', form=form)
Пример #17
0
def add_a_pet():
    """Show the add a form page and process the form"""

    form = AddPet()

    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_form.html", form=form)