-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.py
388 lines (333 loc) · 10.6 KB
/
database.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from math import log10
import psycopg2
from urllib.parse import urlparse
from datetime import datetime
from config import *
from dbtoken import create_token
import logging
# postgres://user:password@server:port/database i think
url = urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
def from_iso_date(x):
return datetime.strptime(x, "%Y-%m-%d")
class Scoring(object):
NONE = 0
BMWEST_16 = 1
class IncorrectEvent(Exception):
def __init__(self):
Exception.__init__(self, "This doesn't correspond to this event.")
class Song(object):
def __init__(self,
name,
author,
fake_author,
bga_author,
description,
link,
email,
song_id,
event_id,
cnt,
tok):
self.name = name
self.author = author
self.fake_author = fake_author
self.bga_author = bga_author
self.description = description
self.link = link
self.email = email
self.id = song_id
self.event_id = event_id
self.impression_count = cnt
self.token = tok
evt = Event(event_id)
if evt.use_fake_name:
if evt.are_impressions_finished:
if len(fake_author) > 0:
self.display_name = "{} ({})".format(fake_author, author)
else:
self.display_name = author
else:
if len(fake_author) > 0:
self.display_name = fake_author
else:
self.display_name = author
else:
self.display_name = author
class Event:
def assign_from_row(self, row):
# database
self.id = row[0]
# meta/contact
self.name = row[1]
self.description = row[2]
self.organizers = row[3]
self.download_package = row[4]
self.sidebar_content = row[5]
self.email = row[6]
self.twitter = row[7]
# URLs
self.banner_url = row[8]
self.css_url = row[9]
# dates are converted to datetime.date by psycopg
self.impression_start = row[10]
self.impression_end = row[11]
self.submission_start = row[12]
self.submission_end = row[13]
# scoring method
self.scoring_method = int(row[14]) if row[14] else 0
# flags
self.use_fake_name = row[15]
# token
self.token = row[16]
return self
def __init__(self, event_id=None):
now = datetime.utcnow().date()
self.id = event_id
self.name = ""
self.description = ""
self.organizers = ""
self.download_package = ""
self.sidebar_content = ""
self.email = ""
self.twitter = ""
self.banner_url = ""
self.css_url = ""
self.impression_start = now
self.impression_end = now
self.submission_start = now
self.submission_end = now
self.scoring_method = Scoring.NONE
self.use_fake_name = False
self.token = ""
if event_id:
# Create object from id
c = conn.cursor()
c.execute("""SELECT * FROM event WHERE id=%s""", (event_id,))
rows = c.fetchall()
if len(rows) > 0:
for row in rows:
self.assign_from_row(row)
else:
raise IncorrectEvent()
@property
def are_submissions_open(self):
if not DEBUG:
return self.submission_start <= datetime.utcnow().date() <= self.submission_end
else:
return True
@property
def allow_blank_comments(self):
if self.scoring_method == Scoring.NONE:
return False
return True
@property
def are_impressions_open(self):
if not DEBUG:
return self.impression_start <= datetime.utcnow().date() <= self.impression_end
else:
return True
@property
def can_see_submissions(self):
if not DEBUG:
return self.submission_start <= datetime.utcnow().date()
else:
return True
@property
def are_impressions_finished(self):
if not DEBUG:
return datetime.utcnow().date() >= self.submission_end
else:
return True
@property
def entries(self):
entries = []
c = conn.cursor()
c.execute("""
SELECT
entry.name,
entry.author,
entry.fake_author,
entry.bga_author,
entry.description,
entry.url,
entry.email,
entry.id,
entry.event_id,
COUNT(impression.id),
entry.token
FROM entry
LEFT JOIN impression ON entry.id=impression.parent_entry
WHERE entry.event_id=%s
GROUP BY entry.id;
""", (self.id,))
for row in c.fetchall():
entries.append(Song(*row))
return entries
def insert_entry(self, name, author, fake_author, bga_author, description, link, email):
c = conn.cursor()
tok = create_token()
c.execute("""INSERT INTO entry
(event_id, name,author,fake_author,bga_author,description,url,email,token)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)""",
(self.id, name, author, fake_author, bga_author, description, link, email, tok))
conn.commit()
return tok
def update_entry(self, name, author, fake_author, bga_author, description, link, email, sid, tok):
c = conn.cursor()
c.execute("""UPDATE entry SET
name=%s,
author=%s,
fake_author=%s,
bga_author=%s,
description=%s,
url=%s,
email=%s
WHERE id=%s AND token=%s""", (name,
author,
fake_author,
bga_author,
description,
link,
email,
sid, tok))
conn.commit()
def get_impressions(self, song):
if song.event_id == self.id:
impressions = []
c = conn.cursor()
c.execute('SELECT * FROM impression WHERE parent_entry=%s', (song.id,))
for row in c.fetchall():
impressions.append(Impression(row[1], row[2], row[3]))
return impressions
else:
raise IncorrectEvent()
def get_song_rating(self, song_id):
impressions = self.get_impressions(song_id)
return self.get_rating_impressions(impressions)
def get_rating_impressions(self, impressions):
if self.scoring_method == Scoring.BMWEST_16:
return self.bmwest_scoring(impressions)
else:
return 0
def bmwest_scoring(self, impressions):
score = sum(int(x.rating) for x in impressions)
if len(impressions):
return round(log10(len(impressions) + 1) * score / float(len(impressions)), 2)
else:
return 0
def normalize_scoring(self, rating):
if self.scoring_method == Scoring.BMWEST_16:
rating = int(rating)
if rating < 0 or rating > 100:
raise ValueError("Rating is out of range.")
else:
rating = 0
return rating
def insert_impression(self, song_id, author, rating, comment, ip):
rating = self.normalize_scoring(rating)
c = conn.cursor()
if len(author) == 0:
author = "Anonymous"
if get_song_by_id(song_id).event_id == self.id:
t = (author, int(rating), comment, song_id, ip)
c.execute("""INSERT INTO impression
(author, rating, comment, parent_entry, ip)
VALUES(%s,%s,%s,%s,%s)""", t)
conn.commit()
else:
raise IncorrectEvent()
class Impression(object):
def __init__(self, author, rating, comment):
self.author = author
self.rating = rating
self.comment = comment
def generate():
c = conn.cursor()
f = open("database.sql")
c.execute(f.read())
conn.commit()
def get_events():
c = conn.cursor()
c.execute("""SELECT * FROM event""")
return [Event().assign_from_row(row) for row in c.fetchall()]
def get_song_by_id(song_id):
c = conn.cursor()
c.execute("""
SELECT
entry.name,
entry.author,
entry.fake_author,
entry.bga_author,
entry.description,
entry.url,
entry.email,
entry.id,
entry.event_id,
COUNT(impression.id),
entry.token
FROM entry
LEFT JOIN impression ON entry.id=impression.parent_entry
WHERE entry.id = %s
GROUP BY entry.id; """, (song_id,))
for row in c.fetchall():
return Song(*row)
return None
def get_song_by_token_and_id(token, song_id):
c = conn.cursor()
c.execute("""
SELECT
entry.name,
entry.author,
entry.fake_author,
entry.bga_author,
entry.description,
entry.url,
entry.email,
entry.id,
entry.event_id,
COUNT(impression.id),
entry.token
FROM entry
LEFT JOIN impression ON entry.id=impression.parent_entry
WHERE entry.token = %s AND entry.id = %s
GROUP BY entry.id; """, (token, song_id))
for row in c.fetchall():
return Song(*row)
return None
def from_scoring_name(x): # must match the values at form
if x == "bmwest2016":
return Scoring.BMWEST_16
else:
return Scoring.NONE
def update_event(*row):
c = conn.cursor()
row = list(row)
row[12] = from_scoring_name(row[12])
row = tuple(row)
c.execute("""UPDATE event SET
name=%s,
description=%s,
organizers=%s,
download_url=%s,
email=%s,
twitter_handle=%s,
banner_url=%s,
css_url=%s,
impression_start=%s,
impression_end=%s,
submission_start=%s,
submission_end=%s,
scoring_method=%s,
use_fake_name=%s
WHERE id=%s AND token=%s
""", row)
logging.info(row)
conn.commit()
return None