Skip to content

Commit

Permalink
working with db
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamBrem committed Sep 22, 2023
1 parent 1231f7c commit 50d8ad4
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 23 deletions.
Empty file added database/__init__.py
Empty file.
Empty file added database/db_operations.py
Empty file.
Empty file added init_db.py
Empty file.
25 changes: 8 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
from views import parentStudentviews

app = Flask(__name__)

app.register_blueprint(parentStudentviews.user_bp)
from flask_restful import Resource, Api
from flask_sqlalchemy import SQLAlchemy

from views import parentStudentViews, teacherViews


@app.route("/")
def index():
return "index"

@app.route("/getUser/<id>", methods=["GET"])
def getUser(id):
return jsonify({"id": id})

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://liambrem:pass@localhost:5432/ft"
db = SQLAlchemy(app)

@app.route("/createUser", methods=["POST"])
def createUser():
data = request.get_json()
return jsonify(data)
api = Api(app)

app.register_blueprint(parentStudentViews.userBP)


if __name__ == "__main__":
Expand Down
Empty file added models/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions models/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from main import db

class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(80), unique=False, nullable=False)

def __init__(self, username, email):
self.username = username
self.email = email
14 changes: 14 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
aniso8601==9.0.1
blinker==1.6.2
certifi==2023.7.22
charset-normalizer==3.2.0
click==8.1.7
flask==2.3.3
Flask-RESTful==0.3.10
flask-sqlalchemy==3.1.1
greenlet==2.0.2
idna==3.4
importlib-metadata==6.8.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
psycopg2==2.9.7
pytz==2023.3.post1
requests==2.31.0
six==1.16.0
SQLAlchemy==2.0.21
typing-extensions==4.8.0
urllib3==2.0.5
werkzeug==2.3.7
zipp==3.17.0
6 changes: 6 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import requests

BASE = "http://127.0.0.1:5000/"

response = requests.get(BASE + "user/3")
print(response.json())
27 changes: 21 additions & 6 deletions views/parentStudentViews.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
from flask import Blueprint, jsonify, request
from flask_restful import Resource, Api

user_bp = Blueprint("user", __name__)
userBP = Blueprint("user", __name__)


listOfStudents = {
"1": {"name": "Liam", "email": "email:"},
"2": {"name": "Daws", "email": "email:"},
"3": {"name": "Brem", "email": "email:"}
}

@user_bp.route("/createUser", methods=["POST"])
def createUser():
#route specific code
class User(Resource):
def get(self, id):
return listOfStudents[id]


data = request.get_json()
return jsonify(data)
def post(self, name, age):
new_user = User(name=name, age=age)
return {"data": "Posted"}



#initialize the flask restful api using the blueprint
api = Api(userBP)
api.add_resource(User, "/user/<id>")

0 comments on commit 50d8ad4

Please sign in to comment.