-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodels.py
297 lines (249 loc) · 10.4 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
import uuid
from datetime import datetime, timedelta
from sqlalchemy import Table, Column, Integer, String, Binary, Boolean, \
ForeignKey, DateTime, Float, or_
from sqlalchemy.orm import mapper, relationship
from database import metadata, db_session
class User():
query = db_session.query_property()
def __init__(self, username, firstname, lastname, email, phone,
password, salt):
self.username = username
self.firstname = firstname
self.lastname = lastname
self.email = email
self.phone = phone
self.password = password
self.salt = salt
self.created_date = datetime.now
self.confirmed = False
self.confirmation_token = uuid.uuid4().hex
def __repr__(self):
return '<User %r>' % (self.username)
def from_token(token):
if token:
return User.query.filter(
User.login_token == token).first()
def as_pub_dict(self):
pub_dict = {
'id': self.id,
'username': self.username
}
return pub_dict
def as_private_dict(self):
priv_dict = {
'username': self.username,
'firstname': self.firstname,
'lastname': self.lastname,
'email': self.email,
'phone': self.phone,
'created_at': self.created_at
}
return priv_dict
class Ticket():
query = db_session.query_property()
def __init__(self, type_id, owner_id, price, paid=False,
reserved_until=None, reserved_at=None, seat_num=None):
self.type_id = type_id
self.owner_id = owner_id
self.price = price
self.paid = paid
self.reserved_until = reserved_until
self.reserved_at = reserved_at
self.seat_num = seat_num
self.qr_token = uuid.uuid4().hex
def __repr__(self):
return '<Ticket %r>' % (self.id)
def as_pub_dict(self):
reserved_until = self.reserved_until.strftime('%Y-%m-%dT%H:%M:%S')
now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
pub_dict = {
'type_id': self.type_id,
'owner_id': self.owner_id,
'paid': self.paid,
'reserved_until': reserved_until,
'now': now,
'price': self.price,
'discount_amount': self.discount_amount,
'total': self.total
}
if self.seat_num:
pub_dict['seat_num'] = self.seat_num
if self.owner:
pub_dict['owner_username'] = self.owner.username
return pub_dict
def as_private_dict(self):
priv_dict = self.as_pub_dict()
priv_dict['qr_token'] = self.qr_token
return priv_dict
def book_temp(user_id, ticket_type, price, tickets_max, seat_num=None):
try:
# Get reservation and paid ticket total count for user
user_ticket_count = Ticket.query \
.filter(Ticket.owner_id == user_id) \
.filter(or_(
Ticket.paid, Ticket.reserved_until >= datetime.now())) \
.count()
# Check if user can order a ticket
if user_ticket_count > 0:
db_session.rollback()
return False, \
'Vous avez déjà un billet ou une réservation en cours !'
# Get reservation and paid ticket total count for ticket type
ticket_type_count = Ticket.query \
.filter(Ticket.type_id == ticket_type) \
.filter(or_(
Ticket.paid, Ticket.reserved_until >= datetime.now())) \
.count()
# Check if more tickets is allowed for this type
if ticket_type_count >= tickets_max[ticket_type]:
db_session.rollback()
return False, \
'Le maximum de billet a été réservé pour le moment !'
# Check if seat is taken
if seat_num:
wanted_seat_count = Ticket.query \
.filter(Ticket.seat_num == seat_num) \
.filter(or_(
Ticket.paid, Ticket.reserved_until >= datetime.now()))\
.count()
if wanted_seat_count > 0:
db_session.rollback()
return False, \
'Ce siège est déjà occupé ou réservé !'
# Book ticket for 10 minutes
reserved_until = datetime.now() + timedelta(minutes=10)
# Insert ticket
ticket = Ticket(ticket_type, user_id, price,
reserved_until=reserved_until, seat_num=seat_num)
db_session.add(ticket)
db_session.commit()
return True, ticket
except Exception as e:
try:
db_session.rollback()
except Exception as e:
pass
return False, '''\
Une erreur inconnue être survenue lors de la réservation de votre bilet.''',
str(e)
class Payment():
query = db_session.query_property()
def __init__(self, amount, ticket_id, paypal_payment_id):
self.amount = amount
self.ticket_id = ticket_id
self.paypal_payment_id = paypal_payment_id
def __repr__(self):
return '<Seat %r>' % (self.id)
class Team():
query = db_session.query_property()
def __init__(self, name, game, captain_id):
self.name = name
self.game = game
self.captain_id = captain_id
self.created_date = datetime.now
def __repr__(self):
return '<Team %r>' % (self.name)
def get_team_id(self):
return Team.query.filter(Team.name == self.name) \
.filter(Team.game == self.game).first().id
def get_captain_name(self):
user = User.query.filter(User.id == self.captain_id).first()
if user:
return user.username
def as_pub_dict(self):
pub_dict = {
'id': self.get_team_id(),
'name': self.name,
'game': self.game,
'captain_name': self.get_captain_name(),
}
return pub_dict
class TeamUser():
query = db_session.query_property()
def __init__(self, team_id, user_id, accepted=False):
self.team_id = team_id
self.user_id = user_id
self.accepted = accepted
def __repr__(self):
return '<Team-User %r - %r>' % (self.team_id, self.user_id)
def get_team_user_id(self):
return TeamUser.query.filter(TeamUser.team_id == self.team_id) \
.filter(TeamUser.user_id == self.user_id).first().id
def get_user_name(self):
user = User.query.filter(User.id == self.user_id).first()
if user:
return user.username
def as_pub_dict(self):
pub_dict = {
'id': self.get_team_user_id(),
'username': self.get_user_name(),
'team_id': self.team_id,
'accepted': self.accepted
}
return pub_dict
teams = Table('teams', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(64), nullable=False),
Column('game', String(255), nullable=False),
Column('captain_id', Integer, ForeignKey('users.id')),
Column('created_at', DateTime, default=datetime.now),
Column('modified_at', DateTime, onupdate=datetime.now)
)
users = Table('users', metadata,
Column('id', Integer, primary_key=True),
Column('username', String(64), nullable=False),
Column('firstname', String(64), nullable=False),
Column('lastname', String(64), nullable=False),
Column('email', String(64), nullable=False),
Column('phone', String(32), nullable=False),
Column('created_at', DateTime, default=datetime.now),
# private fields
Column('modified_at', DateTime, onupdate=datetime.now),
Column('password', Binary(64), nullable=False),
Column('salt', String(32), nullable=False),
Column('confirmed', Boolean, default=False),
Column('confirmation_token', String(32))
)
team_users = Table('team_users', metadata,
Column('id', Integer, primary_key=True),
Column('team_id', Integer, ForeignKey('teams.id')),
Column('user_id', Integer, ForeignKey('users.id')),
Column('accepted', Boolean, nullable=False)
)
tickets = Table('tickets', metadata,
Column('id', Integer, primary_key=True),
# pc or console
Column('type_id', Integer, nullable=False),
# avoid n-n for now...
Column('owner_id', Integer, ForeignKey('users.id'),
nullable=False),
Column('seat_num', Integer),
# Look for related payment and remove this field ?
Column('paid', Boolean, default=False, nullable=False),
Column('price', Float, nullable=False),
Column('discount_amount', Float, default=0, nullable=False),
Column('total', Float, default=0, nullable=False),
Column('reserved_until', DateTime, nullable=False),
# private fields
Column('qr_token', String(32), nullable=False),
Column('created_at', DateTime, default=datetime.now),
Column('modified_at', DateTime, onupdate=datetime.now),
Column('participate_gg', Boolean, default=False)
)
payments = Table('payments', metadata,
Column('id', Integer, primary_key=True),
Column('ticket_id', Integer, ForeignKey('tickets.id')), # SKU
Column('paypal_payer_id', String(255)), # nullable
Column('paypal_payment_id', String(255), nullable=False),
Column('amount', Float, nullable=False),
Column('created_at', DateTime, default=datetime.now),
Column('modified_at', DateTime, onupdate=datetime.now)
)
mapper(User, users)
mapper(Ticket, tickets, properties={
'owner': relationship(User)
})
mapper(Team, teams)
mapper(TeamUser, team_users)
mapper(Payment, payments)