def work self last_time Send notifications about new questions to all

 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
def work(self, last_time):
"""
Send notifications about new questions to all users.
@param last_time: last time when script was worked.
"""
# Select all users, whose last notification time is too old.
if self.debug_last_time:
last_time = datetime.now()#self.debug_last_time
else:
last_time = datetime.now() - timedelta(seconds=self.period_length)
self.cursor.execute("""
SELECT u.id, u.email, u.login, u.homecity,
u.qnotified
FROM users u
LEFT JOIN cities_in_users ciu ON ciu.user = u.id
WHERE u.question_notify = %s
AND (qnotified IS NULL OR qnotified < %s)""",
(self.period_type, last_time))
# и потом ...
# 1) Select questions that needs answers
self.cursor.execute("""
SELECT q.id, q.title, q.city, q.user
FROM articles q
WHERE q.atype = 1 AND q.date > %s AND q.countAnswers < %s
ORDER BY q.date ASC""",
(last_time, settings.QUESTION_NEEDS_ANSWERS))