Skip to content

Commit

Permalink
Stores user data by date
Browse files Browse the repository at this point in the history
  • Loading branch information
hatdropper1977 committed Mar 30, 2019
1 parent f554ef7 commit 0345471
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# web-db-app-w-s3
#web-db-app-w-s3
A tutorial that shows how to deploy a Web Database Application that has S3 as the persistence layer.

#[Click for instructions](https://john.soban.ski/an-inexpensive-web-database-app-via-s3-part-one.html)
16 changes: 9 additions & 7 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def take_test():
if not form.validate_on_submit():
return render_template('take_quiz_template.html', form=form)
if request.method == 'POST':
S3_SUB_BUCKET_NAME = datetime.now().strftime('%Y%m%d')
S3_OBJECT_NAME = random_string_gen()
completed_quiz = {}
completed_quiz['agree'] = request.form.get('agree')
Expand All @@ -38,13 +39,14 @@ def take_test():
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)
s3.Object(S3_BUCKET_NAME, '{}/{}.json'.format(S3_SUB_BUCKET_NAME,S3_OBJECT_NAME)).put(Body=S3_OBJECT_JSON)
return 'Your key is {}/{}.'.format(S3_SUB_BUCKET_NAME,S3_OBJECT_NAME)

@application.route('/user/<user_date>/<user_key>')
def show_user_data(user_date,user_key):
S3_SUB_BUCKET_NAME = user_date
S3_OBJECT_NAME = user_key
obj = s3.Object(S3_BUCKET_NAME, '{}/{}.json'.format(S3_SUB_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) )

Expand Down
2 changes: 1 addition & 1 deletion templates/take_quiz_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ <h3>Please answer this very important essay</h3>
<hr>
{{ wtf.quick_form(form) }}
<hr>
<p>Copyright 2018 <a href="https://www.freshlex.com">Freshlex, LLC</a></p>
<p>Copyright 2018 <a href="https://john.soban.ski">John Sobanski</a></p>
</div>
{% endblock %}

0 comments on commit 0345471

Please sign in to comment.