-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
49 lines (41 loc) · 1.28 KB
/
models.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
41
42
43
44
45
46
47
48
49
from sqlalchemy import Column, String, create_engine,Integer,ForeignKey
from flask_sqlalchemy import SQLAlchemy
import json
import os
database_name="marbreproject"
database_path = os.environ.get('DATABASE_URL',"postgres://{}:{}@{}/{}".format('postgres', 'postgres','localhost:5432', database_name))
db = SQLAlchemy()
'''
setup_db(app)
binds a flask application and a SQLAlchemy service
'''
def setup_db(app, database_path=database_path):
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
class Marbre(db.Model):
id = Column(Integer(), primary_key=True)
title = Column(String(80))
image = Column(String(200))
price = Column(Integer())
origin = Column(String(80))
description = Column(String(500),nullable=True)
def insert(self):
db.session.add(self)
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def update(self):
db.session.commit()
def format(self):
return {
'id': self.id,
'title': self.title,
'image': self.image,
'price': self.price,
'origin': self.origin,
'description':self.description,
}