Ejemplo n.º 1
0
def submit(): 
    if request.method == 'POST':
        # validate the form before we do anything else
        form = scripts.validate_registration_field(request.data)
    if form and not form.errors:
        # now that it's good, add it to the database
        response = scripts.add_new_attendee(form)
        
        # catch our custom errors first
        if response.get('error_response'):
            if response.get('error_response') == 'user_exists':
                tmp = jsonify({'HTTPStatusCode': 200, 'message': 'user_exists'})
                resp = flask.make_response(tmp, 200)

                # get out of there, she's gonna blow!
                return resp

        # build response to database update
        if response and response.get('aws_response', {}).get('ResponseMetadata', {}).get('HTTPStatusCode', 0) == 200:
            tmp = jsonify({'HTTPStatusCode': 200, 'message': 'Added user'})
            resp = flask.make_response(tmp, 200)

            # now start a background process to send the email out, passing in hash
            jobs = []
            data = response.get('new_attendee')
            p = multiprocessing.Process(target=send_email, args=tuple(data.items()))
            jobs.append(p)
            p.start()

        else:
            tmp = jsonify({'HTTPStatusCode': 500, 'message': 'Failed to update database'})
            resp = flask.make_response(tmp, 500)
    elif form and form.errors:
        tmp = jsonify({'HTTPStatusCode': 400, 'message': form.errors})
        resp = flask.make_response(tmp, 400)
    else:
        tmp = jsonify({'HTTPStatusCode': 400, 'message': 'Failed to validate form'})
        resp = flask.make_response(tmp, 400)
    
    return resp
import scripts
import json

with open('form_data.json') as form_file:
	attendee = json.load(form_file)
# attendee = {"first_name":"Andrew","last_name":"Alexander","email":"*****@*****.**","cell":8142322883,"reimbursement":False,"first":False,"ethnicity":["other"],"dietary":["allergy_egg"],"age":24,"gender":"male","year":"6","shirt_size":"m"}

# att_dict = json.dumps(attendee)

# counter = 0 
# for i in att_dict.iteritems():
# 	counter = counter + 1
# 	print counter
# 	print i
form = scripts.validate_registration_field(json.dumps(attendee))
resp = scripts.add_new_attendee(form)

print resp