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)
def post_volunteer(payload): try: body = request.get_json() name = body['name'] email = body['email'] phone = body['phone'] except Exception as e: print(e) abort(400) try: volunteer = Volunteer(name=name, email=email, phone=phone) db.session.add(volunteer) db.session.commit() return jsonify({'success': True, 'volunteer': volunteer.format()}) except Exception as e: db.session.rollback() abort(422) finally: db.session.close()
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)