Example #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')
Example #2
0
    def create_volunteer():
        # creates a new volunteer and returns it to the api
        body = request.get_json()
        if not body:
            abort(400)

        name = body.get('name', None)
        address = body.get('address', None)
        city = body.get('city', None)
        state = body.get('state', None)
        zip_code = body.get('zip_code', None)
        phone_number = body.get('phone_number', None)

        # all fields are required
        if name and address and city and state and zip_code and phone_number:
            try:
                new_volunteer = Volunteer(name, address, city, state, zip_code,
                                          phone_number)
                new_volunteer.insert()
                return {'success': True, 'volunteer': new_volunteer.format()}
            except Exception:
                abort(422)
        else:
            # request did not contain one or more required fields
            abort(400)
Example #3
0
def register_volunteer():
    try:
        body = request.get_json()
        volunteer = Volunteer(name=body['name'],
                              age=util.compute_int(body['age']),
                              gender=body.get('gender', 'NA'),
                              email=body['email'],
                              image_link=body.get('image_link', ''),
                              profile_link=util.compute_profile_link(),
                              seeking_student=util.compute_boolean(
                                  body['seeking_student']),
                              seeking_description=body['seeking_description'])

        volunteer.insert()

        return jsonify({"success": True}), 200

    except GenericException as e:
        raise e

    except Exception:
        raise APIException("Bad Request", 400)
Example #4
0
    def create_volunteer(*args, **kwargs):
        """Creates a Volunteer.

        Returns:
            response: json, status_code
        """
        try:
            body = request.get_json()
            volunteer = Volunteer(
                name=body.get('name'),
                phone_number=body.get('phone_number'),
                email=body.get('email'),
                event=body.get('event'),
            )
            volunteer.insert()
            return jsonify({
                'success': True,
                'volunteers': volunteer.format()
            }), 201
        except Exception as e:
            app.logger.error(e)
            abort(422)