def new_puppy():
    shelters = Shelter.query.all()
    form = PuppyForm(request.form)
    form.shelter.choices = form.shelter.choices + [
        (sh.id, sh.name)
        for sh in shelters if sh.maximum_capacity > sh.current_occupancy
    ]
    if form.validate_on_submit():
        new_profile = PuppyProfile(
            picture=form.profile['picture'].data,
            description=form.profile['description'].data,
            specialNeeds=form.profile['special_needs'].data)
        if form.shelter.data != 0:
            shelter = Shelter.query.filter_by(id=form.shelter.data).one()
            shelter.current_occupancy += 1
            db.session.add(shelter)
        else:
            shelter = None
        new_puppy = Puppy(name=form.name.data,
                          weight=form.weight.data,
                          dateOfBirth=form.date_of_birth.data,
                          gender=form.gender.data,
                          profile=new_profile,
                          shelter=shelter)
        db.session.add(new_puppy)
        db.session.commit()
        if app.debug:
            app.logger.debug("Puppy {} created!".format(
                (new_puppy.id, new_puppy.name)))
        flash("Puppy {} created!".format((new_puppy.id, new_puppy.name)))
        return redirect(url_for('puppies'))
    else:
        return render_template('newpuppy.html', form=form)
def new_puppy():
    shelters = Shelter.query.all()
    form = PuppyForm(request.form)
    form.shelter.choices = form.shelter.choices + [
        (sh.id, sh.name)
        for sh in shelters
        if sh.maximum_capacity > sh.current_occupancy]
    if form.validate_on_submit():
        new_profile = PuppyProfile(
            picture=form.profile['picture'].data,
            description=form.profile['description'].data,
            specialNeeds=form.profile['special_needs'].data)
        if form.shelter.data != 0:
            shelter = Shelter.query.filter_by(id=form.shelter.data).one()
            shelter.current_occupancy += 1
            db.session.add(shelter)
        else:
            shelter = None
        new_puppy = Puppy(name=form.name.data, weight=form.weight.data,
                          dateOfBirth=form.date_of_birth.data,
                          gender=form.gender.data, profile=new_profile,
                          shelter=shelter)
        db.session.add(new_puppy)
        db.session.commit()
        if app.debug:
            app.logger.debug("Puppy {} created!".format(
                (new_puppy.id, new_puppy.name)))
        flash("Puppy {} created!".format((new_puppy.id, new_puppy.name)))
        return redirect(url_for('puppies'))
    else:
        return render_template('newpuppy.html', form=form)
def edit_puppy(puppy_id):
    puppy = Puppy.query.filter_by(id=puppy_id).one()
    shelters = Shelter.query.all()
    form = PuppyForm(request.form)

    if form.validate_on_submit():
        puppy.name = form.name.data
        puppy.weight = form.weight.data
        puppy.dateOfBirth = form.date_of_birth.data
        puppy.gender = form.gender.data
        # Profile info
        if puppy.profile:
            puppy.profile.picture = form.profile['picture'].data
            puppy.profile.description = form.profile['description'].data
            puppy.profile.specialNeeds = form.profile['special_needs'].data
        else:
            new_profile = PuppyProfile(
                picture=form.profile['picture'].data,
                description=form.profile['description'].data,
                specialNeeds=form.profile['special_needs'].data)
            puppy.profile = new_profile
        if form.shelter.data != 0:
            new_shelter = Shelter.query.filter_by(id=form.shelter.data).one()
            if puppy.shelter:
                old_shelter = puppy.shelter
                old_shelter.current_occupancy -= 1
                db.session.add(old_shelter)
            puppy.shelter = new_shelter
            new_shelter.current_occupancy += 1
            db.session.add(new_shelter)
        else:
            if puppy.shelter:
                old_shelter = puppy.shelter
                old_shelter.current_occupancy -= 1
                db.session.add(old_shelter)
            puppy.shelter = None

        db.session.add(puppy)
        db.session.commit()
        if app.debug:
            app.logger.debug("Puppy {} edited!".format((puppy_id, puppy.name)))
        flash("Puppy {} edited!".format((puppy_id, puppy.name)))
        return redirect(url_for('puppies'))
    else:
        form = PuppyForm(obj=puppy)
        if len(puppy.adopters) > 0:
            del form.shelter
        else:
            form.shelter.choices = form.shelter.choices + [
                (sh.id, sh.name) for sh in shelters
                if sh.maximum_capacity > sh.current_occupancy
            ]
            if puppy.shelter:
                form.shelter.data = puppy.shelter_id
            else:
                form.shelter.data = 0
        form.date_of_birth.data = puppy.dateOfBirth
        form.profile['picture'].data = puppy.profile.picture
        form.profile['description'].data = puppy.profile.description
        form.profile['special_needs'].data = puppy.profile.specialNeeds
        return render_template('editpuppy.html', form=form, puppy=puppy)
def edit_puppy(puppy_id):
    puppy = Puppy.query.filter_by(id=puppy_id).one()
    shelters = Shelter.query.all()
    form = PuppyForm(request.form)

    if form.validate_on_submit():
        puppy.name = form.name.data
        puppy.weight = form.weight.data
        puppy.dateOfBirth = form.date_of_birth.data
        puppy.gender = form.gender.data
        # Profile info
        if puppy.profile:
            puppy.profile.picture = form.profile['picture'].data
            puppy.profile.description = form.profile['description'].data
            puppy.profile.specialNeeds = form.profile['special_needs'].data
        else:
            new_profile = PuppyProfile(
                picture=form.profile['picture'].data,
                description=form.profile['description'].data,
                specialNeeds=form.profile['special_needs'].data)
            puppy.profile = new_profile
        if form.shelter.data != 0:
            new_shelter = Shelter.query.filter_by(id=form.shelter.data).one()
            if puppy.shelter:
                old_shelter = puppy.shelter
                old_shelter.current_occupancy -= 1
                db.session.add(old_shelter)
            puppy.shelter = new_shelter
            new_shelter.current_occupancy += 1
            db.session.add(new_shelter)
        else:
            if puppy.shelter:
                old_shelter = puppy.shelter
                old_shelter.current_occupancy -= 1
                db.session.add(old_shelter)
            puppy.shelter = None

        db.session.add(puppy)
        db.session.commit()
        if app.debug:
            app.logger.debug("Puppy {} edited!".format(
                (puppy_id, puppy.name)))
        flash("Puppy {} edited!".format((puppy_id, puppy.name)))
        return redirect(url_for('puppies'))
    else:
        form = PuppyForm(obj=puppy)
        if len(puppy.adopters) > 0:
            del form.shelter
        else:
            form.shelter.choices = form.shelter.choices + [
                    (sh.id, sh.name)
                    for sh in shelters
                    if sh.maximum_capacity > sh.current_occupancy]
            if puppy.shelter:
                form.shelter.data = puppy.shelter_id
            else:
                form.shelter.data = 0
        form.date_of_birth.data = puppy.dateOfBirth
        form.profile['picture'].data = puppy.profile.picture
        form.profile['description'].data = puppy.profile.description
        form.profile['special_needs'].data = puppy.profile.specialNeeds
        return render_template('editpuppy.html', form=form, puppy=puppy)