Created
May 29, 2017 18:25
-
-
Save Deviad/f01db904ff4ab25f78b4c8fdb68f3e17 to your computer and use it in GitHub Desktop.
Controllers of my flask app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymysql | |
from flask import Blueprint, request, render_template, json, Flask | |
from flask_sqlalchemy import SQLAlchemy | |
from sqlalchemy import create_engine | |
from app.users_bundle.models import User | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy.exc import SQLAlchemyError | |
app = Flask(__name__) | |
app.config.from_object('config.DevelopmentConfig') | |
db = SQLAlchemy(app) | |
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'], encoding='utf8', echo=True) | |
Session = sessionmaker(bind=engine) | |
Session.configure(bind=engine) | |
session = Session() | |
users_bundle = Blueprint("user", __name__, url_prefix="/api") | |
def do_the_signup(username, password): | |
# return json.jsonify({'username': username, 'password': password}) | |
try: | |
user = User(username, password) | |
session.begin() | |
session.add(user) | |
session.commit() | |
session.close() | |
# using jsend standard https://labs.omniti.com/labs/jsend | |
response = json.jsonify({"status": "success"}) | |
response.status_code = 201 | |
return response | |
except SQLAlchemyError: | |
response = json.jsonify({"status": "fail"}) | |
response.status_code = 400 | |
return response | |
@users_bundle.route("/user", methods=['POST']) | |
def signup(): | |
if request.method == 'POST': | |
if request.content_type == 'application/json': | |
username = request.json['username'] | |
password = request.json['password'] | |
# password = request.args.get('password') | |
return do_the_signup(username, password) | |
# else: | |
# show_the_login_form() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment