Ejemplo n.º 1
0
    def add_volunteer_submission():
        # adds a new volunteer with data from volunteer_form and redirects to
        # the dashboard
        form = VolunteerForm()
        if not form.validate_on_submit():
            # if the form has errors, display error messages and resend the
            # current page
            flash('Task could not be created because one or more data fields'
                  ' were invalid:')
            for field, message in form.errors.items():
                flash(message[0])
            return render_template('volunteer_form.html',
                                   form=form,
                                   title="Add a New Volunteer")

        # the form is valid
        name = request.form['name']
        address = request.form['address']
        city = request.form['city']
        state = request.form['state']
        zip_code = request.form['zip_code']
        phone_number = request.form['phone_number']

        new_volunteer = Volunteer(name, address, city, state, zip_code,
                                  phone_number)
        try:
            new_volunteer.insert()
            flash('Volunteer ' + name + ' was successfully added')
        except Exception as e:
            flash('An error occurred.  The Volunteer could not be added')
            abort(422)

        return redirect('/dashboard')
Ejemplo n.º 2
0
    def update_volunteer_submission(vol_id):
        # updates the volunteer having id = vol_id and returns the updated
        # volunteer to the gui.  Use route /volunteers/vol_id method=PATCH to
        # return the volunteer to the api
        form = VolunteerForm()

        if not form.validate_on_submit():
            # if the form has errors, display error messages and resend the
            # current page
            flash('Volunteer ' + request.form['name'] + ' could not be updated'
                  ' because one or more '
                  'fields were invalid:')
            for field, message in form.errors.items():
                flash(message[0])
            volunteer = form.data
            volunteer['id'] = vol_id
            return render_template('volunteer_form.html',
                                   form=form,
                                   volunteer=volunteer,
                                   title="Update Volunteer")

        # form is valid
        try:
            volunteer = Volunteer.query.get(vol_id)
            form = VolunteerForm(obj=volunteer)
            form.populate_obj(volunteer)
            volunteer.update()
        except Exception as e:
            flash('Volunteer ' + request.form['name'] +
                  ' could not be updated')
            abort(422)

        flash('Volunteer ' + volunteer.name + ' was successfully updated')
        return redirect('/volunteers')