forked from seemaullal/melon-reservations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
40 lines (29 loc) · 1.09 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from datetime import datetime
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Reservation(db.Model):
__tablename__ = "reservations"
reservation_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
username = db.Column(db.String)
start_time = db.Column(db.DateTime(timezone=True))
def __repr__(self):
return f"<User username={self.username} start_time={self.start_time}>"
def to_dict(self):
return {
"reservation_id": self.reservation_id,
"username": self.username,
"start_time": self.start_time.isoformat(),
}
def connect_to_db(flask_app, echo = False):
db_uri = os.environ["DATABASE_URL"].replace("postgres", "postgresql")
flask_app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
flask_app.config["SQLALCHEMY_ECHO"] = echo
flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = flask_app
db.init_app(flask_app)
print("Connected to the db!")
if __name__ == "__main__":
app = Flask(__name__)
connect_to_db(app, True)