Skip to content

Commit

Permalink
First Push
Browse files Browse the repository at this point in the history
  • Loading branch information
hatdropper1977 committed Mar 28, 2019
1 parent 7e34104 commit f554ef7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Virtual Env Stuff
bin/
include/
lib64

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
52 changes: 52 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python
import boto3, json, string
from datetime import datetime
from flask import Flask, render_template, request
from flask_bootstrap import Bootstrap
from models import QuizForm
from random import choice

class Config(object):
SECRET_KEY = '78w0o5tuuGex5Ktk8VvVDF9Pw3jv1MVE'

S3_BUCKET_NAME = 'transcribe-input-test'

# Generate a random Obkect ID
def random_string_gen(size=20, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits + '-_'):
return ''.join(choice(chars) for _ in range(size))
s3 = boto3.resource('s3')

application = Flask(__name__)
application.config.from_object(Config)

Bootstrap(application)

@application.route('/', methods=['GET', 'POST'])
def take_test():
form = QuizForm(request.form)
if not form.validate_on_submit():
return render_template('take_quiz_template.html', form=form)
if request.method == 'POST':
S3_OBJECT_NAME = random_string_gen()
completed_quiz = {}
completed_quiz['agree'] = request.form.get('agree')
completed_quiz['anumber'] = request.form.get('anumber')
completed_quiz['client_ip_addr'] = request.remote_addr
completed_quiz['_id'] = S3_OBJECT_NAME
completed_quiz['ipaddr'] = request.form.get('ipaddr')
completed_quiz['@timestamp'] = datetime.now().isoformat()
completed_quiz['textblob'] = request.form.get('textblob')
S3_OBJECT_JSON = json.dumps(completed_quiz)
s3 = boto3.resource('s3')
s3.Object(S3_BUCKET_NAME, '{}.json'.format(S3_OBJECT_NAME)).put(Body=S3_OBJECT_JSON)
return 'Your key is {}.'.format(S3_OBJECT_NAME)

@application.route('/user/<userkey>')
def show_user_data(userkey):
S3_OBJECT_NAME = '{}.json'.format(userkey)
obj = s3.Object(S3_BUCKET_NAME, S3_OBJECT_NAME)
user_json = obj.get()['Body'].read().decode('utf-8')
return render_template( 'show_data_template.html', user_json = json.loads(user_json) )

if __name__ == '__main__':
application.run(host='0.0.0.0', debug=True)
11 changes: 11 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask_wtf import FlaskForm
from wtforms import BooleanField, IntegerField, StringField, SubmitField, TextAreaField
from wtforms.validators import InputRequired, Length, IPAddress

# Form ORM
class QuizForm(FlaskForm):
agree = BooleanField('Check this box if you would like')
anumber = IntegerField('Enter a number',validators = [InputRequired()])
ipaddr = StringField('Enter an IP address', validators=[IPAddress()])
textblob = TextAreaField('Who do you think won the console wars of 1991, Sega Genesis or Super Nintendo? (2048 characters)', validators=[InputRequired(),Length(max=2047)] )
submit = SubmitField('Submit')
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
boto3
Flask-Bootstrap
Flask-WTF
13 changes: 13 additions & 0 deletions templates/show_data_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block content %}
<div class="container">
<h3>Here's your data!!!</h3>
<hr>
{% for key in user_json %}
<li>{{ key }}: {{ user_json[key] }}</li>
{% endfor %}
<hr>
<p>Copyright 2019 <a href="https://john.soban.ski">John Sobanski</a></p>
</div>
{% endblock %}
12 changes: 12 additions & 0 deletions templates/take_quiz_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block content %}
<div class="container">
<h3>Please answer this very important essay</h3>
<p>If you don't it'll go on your permanent record!.</p>
<hr>
{{ wtf.quick_form(form) }}
<hr>
<p>Copyright 2018 <a href="https://www.freshlex.com">Freshlex, LLC</a></p>
</div>
{% endblock %}

0 comments on commit f554ef7

Please sign in to comment.