forked from daeken/QuestCompanions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
356 lines (312 loc) · 7.92 KB
/
model.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import math, json, os, random
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy.orm import relationship
from sqlalchemy.types import *
from sms import sms
from metamodel import *
import hashlib
import markdown2
@Model
class Feedback(object):
profile_id = ForeignKey(Integer, 'User.id')
helpful = Boolean
date = DateTime
body = Unicode()
@Model
class FAQ(object):
creator_id = ForeignKey(Integer, 'User.id')
question = Unicode()
answer = Unicode()
answer_markdown = Unicode()
@Model
class News(object):
creator_id = ForeignKey(Integer, 'User.id')
headline = Unicode()
story = Unicode()
story_markdown = Unicode()
@staticmethod
def getLast(number=5):
return transact.query(News).order_by(News.id.desc()).limit(number).all()
# Games
WOW = 1
SWTOR = 2
def gamename(id):
if id == WOW:
return 'WoW'
elif id == SWTOR:
return 'SW:TOR'
@Model
class GoldHistory(object):
user_id = ForeignKey(Integer, 'User.id')
date = DateTime
amount = Integer
balance = Integer
dollars = Integer
job_id = ForeignKey(Integer, 'Job.id', nullable=True)
desc = Unicode
@Model
class Bid(object):
job_id = ForeignKey(Integer, 'Job.id')
char_id = ForeignKey(Integer, 'Character.id')
amount = Integer
date = DateTime
accepted = Boolean
def accept(self):
with transact:
self.update(accepted=True)
self.job.update(accepted_date=datetime.now())
@Model
class Job(object):
user_id = ForeignKey(Integer, 'User.id')
char_id = ForeignKey(Integer, 'Character.id')
game = Integer
created_date = DateTime
max_pay = Integer
time_reqd = Integer
desc = Unicode(140)
details = Unicode
reqs = String
bids = Bid.relation(backref='job')
canceled = Boolean
accepted_date = Nullable(DateTime())
timer_flags = Integer
timer_started = Nullable(DateTime())
completed = Boolean
fee_paid = Integer
gold_history = GoldHistory.relation(backref='job')
@staticmethod
def add(char, max_pay, time_reqd, desc, details, **kwargs):
with transact:
return Job.create(
user=char.user,
char=char,
game=char.game,
created_data=datetime.now(),
max_pay=max_pay,
time_reqd=time_reqd,
desc=desc,
details=details,
reqs=json.dumps(kwargs),
timer_flags=0,
completed=False,
canceled=False,
fee_paid=0
)
def gamename(self):
return gamename(self.game)
def bid(self, char, amount):
with transact:
return Bid.create(
job=self,
char=char,
amount=amount,
date=datetime.now()
)
def complete(self):
accepted = None
for bid in self.bids:
if bid.accepted:
accepted = bid
break
assert accepted
accepted_user = accepted.char.user
with transact:
fee = int(math.ceil(float(accepted.amount) * 0.3))
earned = accepted.amount-fee
self.update(completed=True, fee_paid=fee)
self.user.update(gold=self.user.gold-accepted.amount)
GoldHistory.create(
user=self.user,
date=datetime.now(),
amount=-accepted.amount,
balance=self.user.gold,
job=self,
desc=u'Paid %i gold for a job' % accepted.amount
)
accepted_user.update(gold=accepted_user.gold+earned)
GoldHistory.create(
user=accepted_user,
date=datetime.now(),
amount=earned,
balance=accepted_user.gold,
job=self,
desc=u'Earned %i gold for a job' % (earned)
)
@Model
class Character(object):
user_id = ForeignKey(Integer, 'User.id')
game = Integer
name = Unicode()
server = Nullable(Unicode())
avatar = String()
attrs = String()
last_update = Date()
jobs = Job.relation(backref='char')
bids = Bid.relation(backref='char')
def gamename(self):
return gamename(self.game)
def link(self, cls='charLink'):
from handler import handler
return '<a class="%s" data-char=%s href="%s">%s</a>' %\
(cls, self.json(), handler.char.get_index.url(self.id), self.name.replace('<', '<'))
def imagelink(self, cls='charLink'):
from handler import handler
return '<img class="%s" data-char=%s src="%s" alt="%s">' %\
(cls, self.json(), self.avatar.replace('"', '"'), self.name.replace('"', '"'))
def json(self):
val = dict(
game=gamename(self.game),
name=self.name,
server=self.server,
avatar=self.avatar,
)
if self.attrs:
attrs = json.loads(self.attrs)
if attrs:
val.update(attrs)
val = '"%s"' % json.dumps(val).replace('"', '"')
return val
def eligible(self, job):
char = job.char
if char.id == self.id or self.game != char.game or self.server != char.server:
return False
return True
@Model
class User(object):
enabled = Boolean
admin = Boolean
username = Unicode(255)
password = String(88)
gold = Integer
email = String
email_verified = Boolean
email_verification = String
email_notifications = Boolean
phone_number = String
phone_verified = Boolean
phone_verification_code = Integer
phone_verification_tries = Integer
phone_notifications = Boolean
feedback_score = Integer
feedback_positive = Integer
feedback_negative = Integer
characters = Character.relation(backref='user')
news = News.relation(backref='creator')
jobs = Job.relation(backref='user')
gold_history = GoldHistory.relation(backref='user')
feedbacks = Feedback.relation(backref='profile')
@staticmethod
def hash(password):
salt = ''.join('%02x' % ord(c) for c in os.urandom(24))
for i in range(1000):
password = hashlib.sha1(salt + password + salt).hexdigest()
return salt+password
@staticmethod
def checkHash(hash, password):
salt, hash = hash[:48], hash[48:]
for i in range(1000):
password = hashlib.sha1(salt + password + salt).hexdigest()
if password == hash:
return True
return False
@staticmethod
def add(username, password, admin):
if User.one(enabled=True, username=username):
return None
with transact:
return User.create(
enabled=True,
username=username,
password=User.hash(password),
admin=admin,
gold=0,
phone_number='',
phone_verified=False,
phone_notifications=True,
email='',
email_verified=False,
email_notifications=True,
feedback_score = 0,
feedback_positive = 0,
feedback_negative = 0
)
@staticmethod
def find(username, password):
if username == None or password == None:
return None
user = User.one(enabled=True, username=username)
if user and User.checkHash(user.password, password):
return user
if not user and len(User.all()) == 0:
return User.add(username, password, True)
return None
def change(self, email=None):
if email != None and email != self.email:
with transact:
self.update(
email=email,
email_verified=False
)
self.generateEmailVerification()
def generateEmailVerification(self):
from handler import email
code = ''.join('%02X' % random.randrange(256) for i in range(20))
with transact:
self.update(email_verification=code)
email(self.email, 'verify', code=code)
def addGold(self, amount, price):
with transact:
self.update(gold=self.gold+amount)
GoldHistory.create(
user=self,
date=datetime.now(),
amount=amount,
balance=self.gold,
dollars=price,
job=None,
desc=u'Bought %i gold for $%.2f' % (amount, price / 100.0)
)
def sms(self, message):
if not self.phone_verified:
return False
sms(self.phone_number, message)
return True
@Model
class Config(object):
name = String(20)
value = Unicode(255)
@staticmethod
def get(name):
if name == 'secret_key':
return '1a2dd09e9b7aec16069690d56bf0ce19567a92b442a0e279'
try:
return Config.one(name=name).value
except:
return None
@staticmethod
def getString(name):
data = Config.get(name)
if data == None:
return None
else:
return str(data)
@staticmethod
def set(name, value):
with transact:
try:
row = Config.one(name=name)
row.update(value=unicode(value))
except:
Config.create(
name=name,
value=unicode(value)
)
try:
file('prod', 'r')
db = 'postgresql://postgres:postgresqc@localhost/qc'
except:
db = 'sqlite:///model.db'
@setup(db)
def init():
pass