-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
333 lines (259 loc) · 11.2 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Float, ForeignKey, PrimaryKeyConstraint, Boolean, LargeBinary, DateTime
from config import SQLALCHEMY_DATABASE_URI
import datetime
engine = create_engine(SQLALCHEMY_DATABASE_URI, echo=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
# Set your classes here.
class Users(Base):
__tablename__ = 'Users'
id = Column(Integer, primary_key=True)
name = Column(String(120), nullable=False)
email = Column(String(120), unique=True, nullable=False)
location = Column(Integer, ForeignKey("Locations.id", ondelete="CASCADE"))
userName = Column(String(120), unique=True, nullable=False)
password = Column(String(30), nullable=False)
permission = Column(Integer, ForeignKey("Permissions.id", ondelete="CASCADE"), nullable=False)
isOauth = Column(Boolean, nullable=False)
def __init__(self, name, password, email, location, userName, permission, isOauth):
self.name = name
self.password = password
self.email = email
self.location = location
self.userName = userName
self.permission = permission
self.isOauth = isOauth
def get_user(sent_id):
return db_session.query(Users).get(sent_id)
def post_user(sent_name, sent_password, sent_email, sent_location, sent_userName, sent_permission, sent_oauth):
new_user = Users(name=sent_name,password=sent_password,email=sent_email,location=sent_location, userName=sent_userName,permission=sent_permission,isOauth=sent_oauth)
db_session.add(new_user)
db_session.commit()
def delete_user(sent_id):
del_user = db_session.query(Users).get(sent_id)
db_session.delete(del_user)
db_session.commit()
def update_user(sent_id, sent_name, sent_password, sent_email, sent_location, sent_userName, sent_permission):
updated_user = db_session.query(Users).get(sent_id)
updated_user.id = sent_id
updated_user.name = sent_name
updated_user.password = sent_password
updated_user.email = sent_email
updated_user.location = sent_location
updated_user.userName = sent_userName
updated_user.permission = sent_permission
db_session.commit()
def update_user_password(sent_id, sent_password):
updated_user = db_session.query(Users).get(sent_id)
updated_user.password = sent_password
db_session.commit()
def authenticate(username, password):
result = db_session.query(Users).filter(Users.name == username, Users.password == password).first()
return result
class Items(Base):
__tablename__ = 'Items'
id = Column(Integer, primary_key=True)
location = Column(Integer, ForeignKey("Locations.id", ondelete="CASCADE"), nullable=False)
ownerId = Column(Integer, ForeignKey("Users.id", ondelete="CASCADE"), nullable=False)
name = Column(String(120), nullable=False)
description = Column(String(500), nullable=False)
imageURL = Column(String(120))
rating = Column(Float)
price = Column(Float)
date = Column(DateTime, default=datetime.datetime.utcnow)
def __init__(self, location, ownerId, name, description, imageURL, rating, price):
self.location = location
self.ownerId = ownerId
self.name = name
self.description = description
self.imageURL = imageURL
self.rating = rating
self.price = price
def get_item(sent_id):
return db_session.query(Items).get(sent_id)
def post_item(sent_location, sent_ownerId, sent_name, sent_description, sent_imageURL, sent_rating, sent_price):
new_item = Items(location=sent_location, ownerId=sent_ownerId, name=sent_name, description=sent_description, imageURL=sent_imageURL, rating=sent_rating, price=sent_price)
db_session.add(new_item)
db_session.flush()
db_session.commit()
return new_item.id
def delete_item(sent_id):
del_item = db_session.query(Items).get(sent_id)
db_session.delete(del_item)
db_session.commit()
def update_item(sent_id, sent_location, sent_ownerId, sent_name, sent_description, sent_imageURL, sent_rating, sent_price):
updated_item = db_session.query(Items).get(sent_id)
updated_item.location=sent_location
updated_item.ownerId=sent_ownerId
updated_item.name=sent_name
updated_item.description=sent_description
updated_item.imageURL=sent_imageURL
updated_item.rating=sent_rating
update_item.price=sent_price
db_session.commit()
class Rentals(Base):
__tablename__ = "Rentals"
renterId = Column(Integer, ForeignKey("Users.id", ondelete="CASCADE"), nullable=False)
itemId = Column(Integer, ForeignKey("Items.id", ondelete="CASCADE"), nullable=False)
date = Column(DateTime, default=datetime.datetime.utcnow)
__table_args__ = (
PrimaryKeyConstraint(
renterId, itemId,
),
)
def __init__(self, renterId, itemId):
self.renterId = renterId
self.itemId = itemId
def get_rental(sent_rentalId, sent_itemId):
return db_session.query(Rentals).get((sent_rentalId, sent_itemId))
def post_rental(sent_renterId, sent_itemId):
db_session.add(Rentals(renterId=sent_renterId, itemId=sent_itemId))
db_session.commit()
def delete_rental(sent_rentalId, sent_itemId):
ren = db_session.query(Rentals).get((sent_rentalId, sent_itemId))
db_session.delete(ren)
db_session.commit()
def update_rental(sent_renterId, sent_itemId):
ren = db_session.query(Rentals).get((sent_renterId, sent_itemId))
ren.renterId=sent_renterId
ren.itemId=sent_itemId
db_session.commit()
class Permissions(Base):
__tablename__ = "Permissions"
id = Column(Integer, primary_key=True)
permission = Column(String(120), nullable=False)
def __init__(self, permission):
self.permission = permission
def get_permission(sent_id):
return db_session.query(Permissions).get(sent_id)
def post_permission(sent_permission):
db_session.add(Permissions(permission=sent_permission))
db_session.commit()
def delete_permission(sent_id):
perm = db_session.query(Permissions).get(sent_id)
db_session.delete(perm)
db_session.commit()
def update_permission(sent_id, sent_permission):
perm = db_session.query(Permissions).get(sent_id)
perm.permission = sent_permission
db_session.commit()
class Comments(Base):
__tablename__ = "Comments"
id = Column(Integer, primary_key=True)
commentText = Column(String(120), nullable=False)
posterId = Column(Integer, ForeignKey("Users.id", ondelete="CASCADE"), nullable=False)
itemId = Column(Integer, ForeignKey("Items.id", ondelete="CASCADE"), nullable=False)
def __init__(self, commentText, posterId, itemId):
self.commentText = commentText
self.posterId = posterId
self.itemId = itemId
def get_comment(sent_id):
return db_session.query(Comments).get(sent_id)
def post_comment(sent_commentText, sent_posterId, sent_itemId):
db_session.add(Comments(commentText=sent_commentText, posterId=sent_posterId, itemId=sent_itemId))
db_session.commit()
def delete_comment(sent_id):
com = db_session.query(Comments).get(sent_id)
db_session.delete(com)
db_session.commit()
def update_comment(sent_id, sent_commentText, sent_posterId, sent_itemId):
com = db_session.query(Comments).get(sent_id)
com.commentText=sent_commentText
com.posterId=sent_posterId
com.itemId=sent_itemId
db_session.commit()
class Locations(Base):
__tablename__ = "Locations"
id = Column(Integer, primary_key=True)
lat = Column(Float, nullable=False)
lon = Column(Float, nullable=False)
def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
def get_location(sent_id):
return db_session.query(Locations).get(sent_id)
def post_location(sent_lat, sent_lon):
db_session.add(Locations(lat=sent_lat,lon=sent_lon))
db_session.commit()
def delete_location(sent_id):
loc = db_session.query(Locations).get(sent_id)
db_session.delete(loc)
db_session.commit()
def update_location(sent_id, sent_lat, sent_lon):
loc = db_session.query(Locations).get(sent_id)
loc.lat = sent_lat
loc.lon = sent_lon
db_session.commit()
class TempCodes(Base):
__tablename__ = "TempCodes"
id = Column(Integer, primary_key=True)
code = Column(String, nullable=False)
def __init__(self, code):
self.code = code
def get_code(sent_id):
return db_session.query(TempCodes).get(sent_id)
def post_code(sent_code):
db_session.add(TempCodes(code=sent_code))
db_session.commit()
def delete_code(sent_id):
loc = db_session.query(TempCodes).get(sent_id)
db_session.delete(loc)
db_session.commit()
class Images(Base):
__tablename__ = "Images"
id = Column(Integer, primary_key=True)
name = Column(String(300), nullable=False)
data = Column(LargeBinary, nullable=False)
def __init__(self, data, name):
self.data = data
self.name = name
def post_image(sent_image, sent_name):
db_session.add(Images(data=sent_image, name=sent_name))
db_session.commit()
class Tags(Base):
__tablename__ = "Tags"
id = Column(Integer, primary_key=True)
name = Column(String(300), nullable=False, unique=True)
def __init__(self, name):
self.name = name
def post_tag(sent_name):
db_session.add(Tags(name=sent_name))
db_session.commit()
class TagItems(Base):
__tablename__ = "TagItems"
tagId = Column(Integer, ForeignKey("Tags.id", ondelete="CASCADE"), nullable=False)
itemId = Column(Integer, ForeignKey("Items.id", ondelete="CASCADE"), nullable=False)
__table_args__ = (
PrimaryKeyConstraint(
tagId, itemId,
),
)
def __init__(self, tagId, itemId):
self.tagId = tagId
self.itemId = itemId
def post_tagItem(sent_tagId, sent_itemId):
db_session.add(TagItems(tagId=sent_tagId, itemId=sent_itemId))
db_session.commit()
class Refunds(Base):
__tablename__ = "Refunds"
id = Column(Integer, primary_key=True)
renterId = Column(Integer, ForeignKey("Users.id", ondelete="CASCADE"), nullable=False)
itemId = Column(Integer, ForeignKey("Items.id", ondelete="CASCADE"), nullable=False)
def __init__(self, renterId, itemId):
self.renterId = renterId
self.itemId = itemId
def post_refund(sent_renterId, sent_itemId):
db_session.add(Refunds(renterId=sent_renterId, itemId=sent_itemId))
db_session.commit()
def delete_refund(sent_id):
ref = db_session.query(Refunds).get(sent_id)
db_session.delete(ref)
db_session.commit()
# Create tables.
Base.metadata.create_all(bind=engine)