/
palabro.py
119 lines (94 loc) · 3.35 KB
/
palabro.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
#coding=utf-8
import web
from datetime import date
from index import db
def get(palabro):
result = db.select(
'palabros',
what='*, UNIX_TIMESTAMP(publish) AS unix_publish',
where={'palabro': palabro})
if result:
word = result[0]
word['date'] = date.fromtimestamp(word['unix_publish'])
return word
else:
return False
def getLatest():
result = db.select(
'palabros',
what='*, UNIX_TIMESTAMP(publish) AS unix_publish',
where='publish <= DATE(NOW())',
order='publish DESC',
limit=1
)
if result:
word = result[0]
word['date'] = date.fromtimestamp(word['unix_publish'])
return word
else:
return False
def getNavWords(date):
first_word = False
prev_word = False
next_word = False
latest_word = False
# get previous word
prev_result = db.select(
'palabros',
what='*, UNIX_TIMESTAMP(publish) AS unix_publish',
where='publish < "'+date.isoformat()+'"',
order='publish DESC',
limit=1
)
if prev_result:
prev_word = prev_result[0]
first_result = db.select('palabros', what='*, UNIX_TIMESTAMP(publish) AS unix_publish', order='publish ASC', limit=1)[0]
if first_result['publish'] != prev_word['publish']:
first_word = first_result
next_result = db.select(
'palabros',
what='*, UNIX_TIMESTAMP(publish) AS unix_publish',
where='publish > "'+date.isoformat()+'" AND publish <= DATE(NOW())',
order='publish ASC',
limit=1
)
if next_result:
next_word = next_result[0]
latest_result = db.select('palabros', what='*, UNIX_TIMESTAMP(publish) AS unix_publish', where='publish <= DATE(NOW())', order='publish DESC', limit=1)[0]
if latest_result['publish'] != next_word['publish']:
latest_word = latest_result
return {
'first': first_word,
'prev': prev_word,
'next': next_word,
'latest': latest_word
}
def getRandom():
return db.select('palabros', where='publish <= NOW()', order='RAND()', limit=1)[0]
def getRange(start, end):
return db.select(
'palabros',
where='publish >= "%s" AND publish <= "%s"' % (start, end),
order='publish DESC'
)
def getArchive():
return db.select(
'palabros',
what='publish, YEAR(publish) as year, MONTH(publish) AS month',
where='DATE(publish) <= DATE(NOW())',
group='year, month',
order='publish DESC'
)
def getMonthArchive(year, month):
return db.select(
'palabros',
where=sqlwhere({'YEAR(publish)': year, 'MONTH(publish)': month}) + ' AND DATE(publish) <= DATE(NOW())',
order='publish DESC'
)
def getQueue():
return db.select('palabros', where='publish > DATE(NOW())', order='publish DESC')
def add(palabro, hint, description):
new_publish = db.select('palabros', what='DATE_ADD(MAX(publish), INTERVAL 1 DAY) AS new_publish')[0]['new_publish']
db.insert('palabros', palabro=palabro, hint=hint, description=description, publish=new_publish)
def edit(palabro, hint, description):
db.update('palabros', hint=hint, description=description, where={'palabro': palabro})