-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
105 lines (84 loc) · 3.21 KB
/
models.py
File metadata and controls
105 lines (84 loc) · 3.21 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from flask import json
from sqlalchemy import create_engine, ForeignKey, Enum
from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.types import Date
engine = create_engine('mysql://uTrade:m@localhost:8889/uTrade', echo=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def to_json(inst, cls):
"""
Jsonify the sql alchemy query result.
"""
convert = dict()
# add your coversions for things like datetime's
# and what-not that aren't serializable.
d = dict()
for c in cls.__table__.columns:
v = getattr(inst, c.name)
if c.type in convert.keys() and v is not None:
try:
d[c.name] = convert[c.type](v)
except:
d[c.name] = "Error: Failed to covert using ", str(convert[c.type])
elif v is None:
d[c.name] = str()
else:
d[c.name] = v
return json.dumps(d)
# Set your classes here.
class Item(Base):
__tablename__ = 'Items'
id = Column(Integer, primary_key=True)
title = Column(String(150))
img_url = Column(String(200))
isbn = Column(String(150))
authors = Column(String(100))
edition = Column(Integer)
def __init__(self, isbn=None, title=None, img_url=None,
authors=None, edition=None):
self.title = title
self.img_url = img_url
self.isbn = isbn
self.authors = authors
self.edition = edition
@property
def json(self):
return to_json(self, self.__class__)
class Post(Base):
__tablename__ = 'Posts'
id = Column(Integer, primary_key=True)
description = Column(String(240))
type = Column(Enum('SellerPost', 'RequestPost'))
price = Column(Integer) # seller
book_condition = Column(Enum('New', 'Like New', 'Good', 'Fair', 'Bad')) # seller
condition_description = Column(String(250)) # seller
user_id = Column(Integer, ForeignKey('Users.id'))
item_id = Column(Integer, ForeignKey('Items.id'))
item = relationship(Item, backref=backref("item", uselist=False))
def __init__(self, item_id, description=None, user_id=None,
type=None, price=None, book_condition=None, condition_description=None):
self.description = description
self.item_id = item_id
self.user_id = user_id
self.type = type
self.price = price
self.book_condition = book_condition
self.condition_description = condition_description
class User(Base):
__tablename__ = 'Users'
id = Column(Integer, primary_key=True)
name = Column(String(120), unique=True)
email = Column(String(120), unique=True)
posts = relationship(Post, backref='author', lazy='dynamic')
phone = Column(String(100))
def __init__(self, name=None, email=None, phone=None):
self.name = name
self.email = email
self.phone = phone
# Create tables.
Base.metadata.create_all(bind=engine)