Skip to content

Commit 09eb00b

Browse files
author
Shafaf
committed
Update: model createdgit add .
1 parent 816191f commit 09eb00b

7 files changed

Lines changed: 101 additions & 24 deletions

File tree

api/v1/plan.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1-
from flask import jsonify
1+
from flask import jsonify, request
22
from flask.views import MethodView
33

44
from . import v1
5+
# from model.subscription import Plan
6+
from extension import db
57

68
# test v1 api endpoint.
79
class Plan(MethodView):
810
def get(self):
911
return jsonify({"test": "This is plan api"}), 200
12+
13+
def post(self):
14+
req :dict = request.get_json()
15+
username = req.get("name")
16+
if username is None:
17+
return jsonify({"message": "name is required"}), 400
18+
19+
email = req.get("email")
20+
if email is None:
21+
return jsonify({"message": "email is required"}), 400
22+
# user = Plan(
23+
# username=username,
24+
# email=email
25+
# )
26+
27+
print("start")
28+
print(username)
29+
print(email)
30+
print("end")
31+
32+
# db.session.add(user)
33+
# db.session.commit()
34+
35+
print(user.id)
36+
37+
return jsonify({"status": True})
1038

1139
v1.add_url_rule("/plan", view_func=Plan.as_view("plan"))

app.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
from flask import Flask
2-
from sqlalchemy import create_engine
2+
from flask_sqlalchemy import SQLAlchemy
33

44
from api.v1 import v1
5-
6-
# from connection import conn
7-
# from model.subscription import hello
5+
from connection import SQLALCHEMY_DATABASE_URI
6+
from extension import db , migrate
7+
from model import Plan
88

99
def create_app():
1010
app = Flask(__name__)
1111

12-
print("Result fetched successfully")
12+
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
13+
14+
15+
extensions(app)
1316

17+
18+
# register the api points.
1419
app.register_blueprint(v1)
1520

21+
# checking is the application is running, remove me later.
1622
@app.route("/")
1723
def index():
1824
return "hello how are you!"
1925

2026
return app
2127

2228

29+
def extensions(app):
30+
db.init_app(app)
31+
migrate.init_app(app, db)
32+
33+
with app.app_context():
34+
db.create_all()
35+
print("Result fetched successfully")
36+
2337
create_app()

connection.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import os
2-
import psycopg2;
32

43
# Get the environment variables
5-
DB_USER = os.getenv('POSTGRES_USER', "postgres")
6-
DB_PASSWORD = os.getenv('POSTGRES_PASSWORD', "postgres")
7-
# DB_HOST = os.getenv('POSTGRES_HOST', "postgres")
8-
DB_PORT = os.getenv('POSTGRES_PORT', "5432")
9-
DB_NAME = os.getenv('POSTsGRES_DB', "demodb")
4+
pg_user = os.getenv('POSTGRES_USER', "muhammedshafaf")
5+
pg_pass = os.getenv('POSTGRES_PASSWORD', "postgres")
6+
pg_host = os.getenv('POSTGsRES_HOST', "localhost")
7+
pg_port = os.getenv('POSTGRES_PORT', "5432")
8+
pg_db = os.getenv('POSTsGRES_DB', "demodb")
109

11-
# database connection login
12-
conn = psycopg2.connect(
13-
dbname=DB_NAME,
14-
user=DB_USER,
15-
password=DB_PASSWORD,
16-
port=DB_PORT
17-
)
1810

11+
SQLALCHEMY_DATABASE_URI = os.getenv(
12+
"DATABASE_URL",
13+
f"postgresql://{pg_user}:{pg_pass}@{pg_host}:{pg_port}/{pg_db}",
14+
)

extension.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from flask_sqlalchemy import SQLAlchemy
2+
from flask_migrate import Migrate
3+
4+
db = SQLAlchemy()
5+
migrate = Migrate()

model/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .subscription import Plan

model/subscription.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1+
from sqlalchemy import Column, String, Integer, ForeignKey, Enum
2+
from sqlalchemy.orm import Mapped, mapped_column
3+
from extension import db
14

5+
class Plan(db.Model):
6+
__tablename__ = "plan"
7+
8+
id = Column(Integer, primary_key=True, nullable=False)
9+
name = Column(String, nullable=False)
10+
description = Column(String(300), nullable=True)
11+
amount = Column(Integer, nullable=False)
12+
intervals = Column(Integer, nullable=False)
13+
plan_id = Column(String)
14+
15+
class Subscription(db.Model):
16+
__tablename__ = "subscription"
17+
18+
STATUS_CHOICES = ["active", "disabled"]
19+
20+
id = Column(Integer, primary_key=True, nullable=False)
21+
plan_id = Column(Integer, ForeignKey(Plan.id), nullable=False)
22+
razorpay_user_id = Column(String)
23+
subscription_id = Column(String)
24+
subscription_status = Column(
25+
Enum(*STATUS_CHOICES, name="status", native_enum=False),
26+
nullable=False,
27+
default="active",
28+
)
29+
230

3-
# engine = create_engine('postgresql:///tutorial.db', echo=True)
4-
def hello():
5-
print("Hello from database.py")
6-
print("========== Database connection established successfully ==========")
31+
class User(db.Model):
32+
__tablename__ = "user"
33+
id = Column(Integer, primary_key=True, nullable=False)
34+
name = Column(String, nullable=False)
35+
email = Column(String, nullable=False)
36+
phone_number = Column(String, nullable=False)
37+

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Flask==3.0.2
22
psycopg2==2.9.9
3-
SQLAlchemy==2.0.29
3+
SQLAlchemy==2.0.29
4+
Flask-SQLAlchemy
5+
Flask-Migrate

0 commit comments

Comments
 (0)