Skip to content

Commit 44d7b7a

Browse files
committed
refactoring/cleaning
1 parent d9cb7eb commit 44d7b7a

4 files changed

Lines changed: 18 additions & 11 deletions

File tree

python/python-guestbook/pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ disable=print-statement,
135135
exception-escape,
136136
comprehension-escape,
137137
invalid-name,
138-
redefined-builtin
138+
superfluous-parens
139139

140140
# Enable the message, report, category or checker with the given id(s). You can
141141
# either give multiple identifier separated by comma (,) or put this option

python/python-guestbook/src/backend/back.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
A sample backend server. Saves and retrieves entries using mongodb
33
"""
44
import os
5+
import time
56
from flask import Flask, jsonify, request
67
from flask_pymongo import PyMongo
7-
from functools import reduce
88
import bleach
9-
import time
109

1110
app = Flask(__name__)
1211
app.config["MONGO_URI"] = 'mongodb://{}/guestbook'.format(os.environ.get('GUESTBOOK_DB_ADDR'))
@@ -15,7 +14,8 @@
1514
@app.route('/messages', methods=['GET'])
1615
def get_messages():
1716
""" retrieve and return the list of messages on GET request """
18-
msg_list = list(mongo.db.messages.find({}, {'author':1, 'message':1, 'date':1, '_id':0}).sort("_id", -1))
17+
field_mask = {'author':1, 'message':1, 'date':1, '_id':0}
18+
msg_list = list(mongo.db.messages.find({}, field_mask).sort("_id", -1))
1919
return jsonify(msg_list), 201
2020

2121
@app.route('/messages', methods=['POST'])
@@ -33,4 +33,7 @@ def add_message():
3333
if os.environ.get(v) is None:
3434
print("error: {} environment variable not set".format(v))
3535
exit(1)
36+
37+
# start Flask server
38+
# Flask's debug mode is unrelated to ptvsd debugger used by Cloud Code
3639
app.run(debug=False, port=os.environ.get('PORT'), host='0.0.0.0')

python/python-guestbook/src/frontend/front.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
"""
44
import json
55
import os
6+
import datetime
7+
import time
68
from flask import Flask, render_template, redirect, url_for, request, jsonify
79
import requests
8-
import bleach
9-
import datetime
1010
import dateutil.relativedelta
11-
import time
1211

1312
app = Flask(__name__)
1413
app.config["BACKEND_URI"] = 'http://{}/messages'.format(os.environ.get('GUESTBOOK_API_ADDR'))
@@ -31,12 +30,14 @@ def post():
3130
timeout=3)
3231
return redirect(url_for('main'))
3332

34-
def format_duration(prev_timestamp):
33+
def format_duration(timestamp):
34+
""" Format the time since the input timestamp in a human readable way """
3535
now = datetime.datetime.fromtimestamp(time.time())
36-
prev = datetime.datetime.fromtimestamp(prev_timestamp)
36+
prev = datetime.datetime.fromtimestamp(timestamp)
3737
rd = dateutil.relativedelta.relativedelta(now, prev)
3838

39-
for n, unit in [(rd.years, "year"), (rd.days, "day"), (rd.hours, "hour"), (rd.minutes, "minute")]:
39+
for n, unit in [(rd.years, "year"), (rd.days, "day"), (rd.hours, "hour"),
40+
(rd.minutes, "minute")]:
4041
if n == 1:
4142
return "{} {} ago".format(n, unit)
4243
elif n > 1:
@@ -50,5 +51,9 @@ def format_duration(prev_timestamp):
5051
print("error: {} environment variable not set".format(v))
5152
exit(1)
5253

54+
# register format_duration for use in html template
5355
app.jinja_env.globals.update(format_duration=format_duration)
56+
57+
# start Flask server
58+
# Flask's debug mode is unrelated to ptvsd debugger used by Cloud Code
5459
app.run(debug=False, port=os.environ.get('PORT'), host='0.0.0.0')
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
Flask==1.0.2
22
requests==2.21.0
3-
bleach==3.1.0
43
python-dateutil==2.8.0

0 commit comments

Comments
 (0)