1
0
mirror of https://github.com/LukePeters/flask-mongo-api-boilerplate.git synced 2026-05-15 22:56:29 +09:00

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.

This commit is contained in:
Luke Peters
2019-03-15 17:28:38 -04:00
parent 6477bdcac4
commit 0f360be43c
6 changed files with 26 additions and 15 deletions

View File

@@ -11,6 +11,7 @@ flask-cors = "*"
pymongo = "*"
python-jose = "*"
passlib = "*"
pytz = "*"
[requires]
python_version = "3.7"
python_version = "3.7"

View File

@@ -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

View File

@@ -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):

View File

@@ -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({

View File

View File

@@ -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