Skip to content

Commit f00562a

Browse files
committedDec 5, 2018
boiler_plate_flask_blueprint_mongo_api_application
1 parent bfa2cb1 commit f00562a

File tree

10 files changed

+89
-0
lines changed

10 files changed

+89
-0
lines changed
 

‎app/__init__.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flask import Flask
2+
3+
from flask_pymongo import PyMongo
4+
5+
6+
mongo = PyMongo()
7+
8+
def create_app(config_filename=None):
9+
application = Flask(__name__, instance_relative_config=True)
10+
application.config.from_pyfile(config_filename)
11+
initialize_extensions(application)
12+
register_blueprints(application)
13+
return application
14+
15+
def initialize_extensions(application):
16+
mongo.init_app(application)
17+
from app.models.user import User
18+
19+
20+
def register_blueprints(application):
21+
from app.controllers import users_blueprint
22+
23+
application.register_blueprint(users_blueprint)
24+
25+

‎app/controllers/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from flask import Blueprint
2+
3+
users_blueprint = Blueprint('users', __name__)
4+
from . import users_controller

‎app/controllers/users_controller.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import request, jsonify
2+
3+
from . import users_blueprint
4+
from app.models.user import User
5+
from app import mongo
6+
7+
8+
@users_blueprint.route('/users')
9+
def get_users():
10+
users = User.get()
11+
return jsonify({'data': users})

‎app/models/user.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from app import mongo
2+
3+
class User():
4+
5+
@classmethod
6+
def get(self):
7+
users = mongo.db.users.find()
8+
user_list = []
9+
for user in users:
10+
data = {}
11+
data['first_name'] = user['first_name']
12+
data['last_name'] = user['last_name']
13+
user_list.append(data)
14+
return user_list
15+

‎bin/start.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3 test.py

‎config/application.py

Whitespace-only changes.

‎config/environments/development.cfg

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
##########################################################
2+
#
3+
# This is a sample flask.cfg for developing a Flask application
4+
#
5+
##########################################################
6+
import os
7+
8+
9+
# Get the folder of the top-level directory of this project
10+
BASEDIR = os.path.abspath(os.path.dirname(__file__))
11+
12+
# Update later by using a random number generator and moving
13+
# the actual key outside of the source code under version control
14+
SECRET_KEY = 'bad_secret_key'
15+
WTF_CSRF_ENABLED = True
16+
DEBUG = True
17+
18+
# SQLAlchemy
19+
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(BASEDIR, 'app.db')
20+
SQLALCHEMY_TRACK_MODIFICATIONS = False
21+
22+
# Bcrypt algorithm hashing rounds
23+
BCRYPT_LOG_ROUNDS = 15
24+

‎config/hello.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
yourpath = os.path.abspath(os.path.dirname(__file__))
4+
print(os.path.abspath(os.path.dirname(__file__)))
5+
print(os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir)))

‎main.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from app import create_app
2+
3+
application = create_app('development.cfg')

‎test.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('Hello Flask')

0 commit comments

Comments
 (0)