#7 Basic flask app. Setup routes, DB, and made a user model. Made initial migrations
This commit is contained in:
33
backend/app/__init__.py
Normal file
33
backend/app/__init__.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from flask import Flask
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
from flask_login import LoginManager
|
||||
from config import Config
|
||||
|
||||
|
||||
db = SQLAlchemy()
|
||||
migrate = Migrate()
|
||||
login = LoginManager()
|
||||
login.login_view = "login"
|
||||
login.login_message = "Please log in to access this page."
|
||||
|
||||
|
||||
def create_app(config_class=Config):
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(config_class)
|
||||
|
||||
db.init_app(app)
|
||||
migrate.init_app(app, db)
|
||||
login.init_app(app)
|
||||
|
||||
app.app_context().push()
|
||||
|
||||
from app.bp import bp as main_bp
|
||||
|
||||
app.register_blueprint(main_bp)
|
||||
|
||||
app.app_context()
|
||||
return app
|
||||
|
||||
|
||||
from app import models
|
||||
5
backend/app/bp.py
Normal file
5
backend/app/bp.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from flask import Blueprint
|
||||
|
||||
bp = Blueprint('index', __name__)
|
||||
|
||||
from app import routes
|
||||
15
backend/app/errors.py
Normal file
15
backend/app/errors.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from flask import jsonify
|
||||
from werkzeug.http import HTTP_STATUS_CODES
|
||||
|
||||
|
||||
def error_response(status_code, message=None):
|
||||
payload = {"error": HTTP_STATUS_CODES.get(status_code, "Unknown error")}
|
||||
if message:
|
||||
payload["message"] = message
|
||||
response = jsonify(payload)
|
||||
response.status_code = status_code
|
||||
return response
|
||||
|
||||
|
||||
def bad_request(message):
|
||||
return error_response(400, message)
|
||||
21
backend/app/models.py
Normal file
21
backend/app/models.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from app import db
|
||||
from flask_login import UserMixin
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(64), index=True, unique=True)
|
||||
email = db.Column(db.String(120), index=True, unique=True)
|
||||
password_hash = db.Column(db.String(128))
|
||||
about_me = db.Column(db.String(140))
|
||||
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
token = db.Column(db.String(32), index=True, unique=True)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"username": self.username,
|
||||
"email": self.email,
|
||||
"about_me": self.about_me,
|
||||
}
|
||||
22
backend/app/routes.py
Normal file
22
backend/app/routes.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from app.bp import bp
|
||||
from flask import Response, jsonify, request
|
||||
from app.errors import error_response
|
||||
|
||||
from app import db
|
||||
from app.models import User
|
||||
|
||||
|
||||
@bp.route("/login", methods=["POST"])
|
||||
def login():
|
||||
data = request.get_json()
|
||||
print(data)
|
||||
if not data.get("user_id"):
|
||||
return error_response(400, "Must supply user_id")
|
||||
|
||||
user = User.query.get(data.get("user_id"))
|
||||
if not user:
|
||||
return error_response(400, "User not found")
|
||||
|
||||
resp = jsonify(user.to_dict())
|
||||
resp.status_code = 200
|
||||
return resp
|
||||
Reference in New Issue
Block a user