You've already forked flask-mongo-api-boilerplate
mirror of
https://github.com/LukePeters/flask-mongo-api-boilerplate.git
synced 2026-05-17 23: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:
3
Pipfile
3
Pipfile
@@ -11,6 +11,7 @@ flask-cors = "*"
|
|||||||
pymongo = "*"
|
pymongo = "*"
|
||||||
python-jose = "*"
|
python-jose = "*"
|
||||||
passlib = "*"
|
passlib = "*"
|
||||||
|
pytz = "*"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.7"
|
python_version = "3.7"
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Pipenv
|
|
||||||
- MongoDB
|
- MongoDB
|
||||||
- Python 3 (defaults to Python 3.7, but you can change this in the Pipfile before setup)
|
- 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
|
1. Clone this repo to your local web server
|
||||||
2. `cd` into the directory within the terminal
|
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)
|
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
|
## Running the app
|
||||||
|
|
||||||
1. Run `pipenv shell` to activate the virtual environment
|
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
|
## Further configuration
|
||||||
|
|
||||||
|
|||||||
@@ -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 random
|
||||||
import time
|
import uuid
|
||||||
|
|
||||||
def nowEpoch():
|
def nowDatetimeUserTimezone(user_timezone):
|
||||||
return int(time.time()) * 1000
|
tzone = timezone(user_timezone)
|
||||||
|
return datetime.datetime.now(tzone)
|
||||||
|
|
||||||
|
def nowDatetimeUTC():
|
||||||
|
tzone = UTC
|
||||||
|
now = datetime.datetime.now(tzone)
|
||||||
|
return now
|
||||||
|
|
||||||
def JsonResp(data, status):
|
def JsonResp(data, status):
|
||||||
from flask import Response
|
from flask import Response
|
||||||
|
from bson import json_util
|
||||||
import json
|
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():
|
def randID():
|
||||||
randId = randString(3) + randString(3) + randString(3) + randString(3) + randString(3) + randString(3)
|
randId = uuid.uuid4().hex
|
||||||
return randId
|
return randId
|
||||||
|
|
||||||
def randString(length):
|
def randString(length):
|
||||||
|
|||||||
@@ -13,12 +13,11 @@ class User:
|
|||||||
"id": tools.randID(),
|
"id": tools.randID(),
|
||||||
"ip_addresses": [request.remote_addr],
|
"ip_addresses": [request.remote_addr],
|
||||||
"acct_active": True,
|
"acct_active": True,
|
||||||
"date_created": tools.nowEpoch(),
|
"date_created": tools.nowDatetimeUTC(),
|
||||||
"last_login": tools.nowEpoch(),
|
"last_login": tools.nowDatetimeUTC(),
|
||||||
"first_name": "",
|
"first_name": "",
|
||||||
"last_name": "",
|
"last_name": "",
|
||||||
"email": "",
|
"email": ""
|
||||||
"plan": "free"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
@@ -66,7 +65,7 @@ class User:
|
|||||||
|
|
||||||
app.db.users.update({ "id": user["id"] }, { "$set": {
|
app.db.users.update({ "id": user["id"] }, { "$set": {
|
||||||
"refresh_token": refresh_token,
|
"refresh_token": refresh_token,
|
||||||
"last_login": tools.nowEpoch()
|
"last_login": tools.nowDatetimeUTC()
|
||||||
} })
|
} })
|
||||||
|
|
||||||
resp = tools.JsonResp({
|
resp = tools.JsonResp({
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ EOF
|
|||||||
|
|
||||||
echo "${BLUE}VIRTUAL ENVIRONMENT SETUP${NC}"
|
echo "${BLUE}VIRTUAL ENVIRONMENT SETUP${NC}"
|
||||||
echo
|
echo
|
||||||
|
pip install pipenv
|
||||||
pipenv install
|
pipenv install
|
||||||
echo
|
echo
|
||||||
|
|
||||||
@@ -100,5 +101,5 @@ echo
|
|||||||
echo "To start the Flask app run these two commands:"
|
echo "To start the Flask app run these two commands:"
|
||||||
echo
|
echo
|
||||||
echo "${GREY}> ${NC}pipenv shell"
|
echo "${GREY}> ${NC}pipenv shell"
|
||||||
echo "${GREY}> ${NC}./run.sh"
|
echo "${GREY}> ${NC}./run"
|
||||||
echo
|
echo
|
||||||
Reference in New Issue
Block a user