From 0f360be43ce53de59948682f609b1ecad51fa252 Mon Sep 17 00:00:00 2001 From: Luke Peters Date: Fri, 15 Mar 2019 17:28:38 -0400 Subject: [PATCH] Switched to isodate vs epoch for all timestamps. Utilzing Python's uuid for random IDs now. Improved setup script to install Pipenv if it isn't already installed. --- Pipfile | 3 ++- README.md | 5 ++--- api/main/tools/__init__.py | 21 ++++++++++++++++----- api/main/user/models.py | 9 ++++----- run.sh => run | 0 setup.sh => setup | 3 ++- 6 files changed, 26 insertions(+), 15 deletions(-) rename run.sh => run (100%) rename setup.sh => setup (98%) diff --git a/Pipfile b/Pipfile index d441ccd..ae5202d 100644 --- a/Pipfile +++ b/Pipfile @@ -11,6 +11,7 @@ flask-cors = "*" pymongo = "*" python-jose = "*" passlib = "*" +pytz = "*" [requires] -python_version = "3.7" \ No newline at end of file +python_version = "3.7" diff --git a/README.md b/README.md index 60ce26c..c2822f9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ ## Requirements -- Pipenv - MongoDB - Python 3 (defaults to Python 3.7, but you can change this in the Pipfile before setup) @@ -8,14 +7,14 @@ 1. Clone this repo to your local web server 2. `cd` into the directory within the terminal -3. Run `./setup.sh` to setup pipenv and configure the Flask app +3. Run `./setup` to setup pipenv and configure the Flask app Here's a quick video of the setup process (no audio): [flask-mongo-api-boilerplate-setup.mp4](https://img.lukepeters.me/flask-mongo-api-boilerplate-setup.mp4) ## Running the app 1. Run `pipenv shell` to activate the virtual environment -2. Run `./run.sh` to start the Flask application +2. Run `./run` to start the Flask application ## Further configuration diff --git a/api/main/tools/__init__.py b/api/main/tools/__init__.py index 89e752d..bdccade 100644 --- a/api/main/tools/__init__.py +++ b/api/main/tools/__init__.py @@ -1,16 +1,27 @@ +from flask import current_app as app +from pytz import timezone, UTC +from datetime import timedelta +import time, datetime import random -import time +import uuid -def nowEpoch(): - return int(time.time()) * 1000 +def nowDatetimeUserTimezone(user_timezone): + tzone = timezone(user_timezone) + return datetime.datetime.now(tzone) + +def nowDatetimeUTC(): + tzone = UTC + now = datetime.datetime.now(tzone) + return now def JsonResp(data, status): from flask import Response + from bson import json_util import json - return Response(json.dumps(data), mimetype="application/json", status=status) + return Response(json.dumps(data, default=json_util.default), mimetype="application/json", status=status) def randID(): - randId = randString(3) + randString(3) + randString(3) + randString(3) + randString(3) + randString(3) + randId = uuid.uuid4().hex return randId def randString(length): diff --git a/api/main/user/models.py b/api/main/user/models.py index 8ef0cb1..262bd1d 100644 --- a/api/main/user/models.py +++ b/api/main/user/models.py @@ -13,12 +13,11 @@ class User: "id": tools.randID(), "ip_addresses": [request.remote_addr], "acct_active": True, - "date_created": tools.nowEpoch(), - "last_login": tools.nowEpoch(), + "date_created": tools.nowDatetimeUTC(), + "last_login": tools.nowDatetimeUTC(), "first_name": "", "last_name": "", - "email": "", - "plan": "free" + "email": "" } def get(self): @@ -66,7 +65,7 @@ class User: app.db.users.update({ "id": user["id"] }, { "$set": { "refresh_token": refresh_token, - "last_login": tools.nowEpoch() + "last_login": tools.nowDatetimeUTC() } }) resp = tools.JsonResp({ diff --git a/run.sh b/run similarity index 100% rename from run.sh rename to run diff --git a/setup.sh b/setup similarity index 98% rename from setup.sh rename to setup index f1a9fee..5997a4a 100755 --- a/setup.sh +++ b/setup @@ -28,6 +28,7 @@ EOF echo "${BLUE}VIRTUAL ENVIRONMENT SETUP${NC}" echo +pip install pipenv pipenv install echo @@ -100,5 +101,5 @@ echo echo "To start the Flask app run these two commands:" echo echo "${GREY}> ${NC}pipenv shell" -echo "${GREY}> ${NC}./run.sh" +echo "${GREY}> ${NC}./run" echo \ No newline at end of file