-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_setup.py
139 lines (115 loc) · 3.71 KB
/
database_setup.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import sys
import os
from sqlalchemy import Column, ForeignKey, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
email = Column(String(250), nullable=False)
picture = Column(String(250))
@property
def serialize(self):
return {
'name': self.name,
'id': self.id,
'email': self.email,
'picture': self.picture
}
class Review(Base):
__tablename__ = 'review'
id = Column(Integer, primary_key=True)
review_text = Column(String(500))
location_id = Column(Integer, ForeignKey('location.id'))
location = relationship("Location")
user = relationship(User)
user_id = Column(Integer, ForeignKey('user.id'))
@property
def serialize(self):
""" Return object data in easily serializable format"""
return {
'id': self.id,
'review_text': self.review_text,
'reviewer_name': self.user.name,
'reviewer_id': self.user.id,
'reviewer_pic': self.user.picture,
'location_name': self.location.label,
'location_id': self.location.id
}
class Location(Base):
__tablename__ = 'location'
id = Column(Integer, primary_key=True)
label = Column(String(80))
lat = Column(
Float(40), nullable=False)
lng = Column(
Float(40), nullable=False)
menuLabel = Column(String(80))
icon = Column(String(80))
type = Column(String(80))
active = Column(String(80))
reviews = relationship(Review, cascade="all,save-update,delete-orphan")
user = relationship(User)
user_id = Column(Integer, ForeignKey('user.id'))
__mapper_args__ = {
'polymorphic_identity': 'location',
'polymorphic_on': type
}
@property
def serialize(self):
# Returns object data in easily serializeable format
return {
'id': self.id,
'position': {
'lat': self.lat,
'lng': self.lng
},
'menuLabel': self.menuLabel,
'label': self.label,
'icon': self.icon,
'locationType': self.type,
'locator_id': self.user_id,
'location.name': self.user.name,
'locator_picture': self.user.picture
}
class Restaurant(Location):
__tablename__ = 'restaurant'
id = Column(Integer, ForeignKey('location.id'), primary_key=True)
yelp_id = Column(String(80))
__mapper_args__ = {
'polymorphic_identity': 'food',
}
@property
def serialize(self):
props = super(Restaurant, self).serialize
props['yelp_id'] = self.yelp_id
return props
class Recreation(Location):
__tablename__ = 'recreation'
id = Column(Integer, ForeignKey('location.id'), primary_key=True)
wiki_id = Column(String(80))
__mapper_args__ = {
'polymorphic_identity': 'rec',
}
@property
def serialize(self):
props = super(Recreation, self).serialize
props['wiki_id'] = self.wiki_id
return props
class School(Location):
__tablename__ = 'school'
id = Column(Integer, ForeignKey('location.id'), primary_key=True)
wiki_id = Column(String(80))
__mapper_args__ = {
'polymorphic_identity': 'school',
}
@property
def serialize(self):
props = super(School, self).serialize
props['wiki_id'] = self.wiki_id
return props
engine = create_engine(os.environ['DB_KEY'])
Base.metadata.create_all(engine)