## This file is part of the CERN Document Server Software (CDSware).
## Copyright (C) 2002 CERN.
##
## The CDSware is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## The CDSware is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDSware; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
##read config variables
#include "config.wml"
#include "configbis.wml"
<protect>## $Id$ </protect>
<protect>## DO NOT EDIT THIS FILE! IT WAS AUTOMATICALLY GENERATED FROM CDSware WML SOURCES.</protect>
"""Alert engine implementation."""
## rest of the Python code goes below
__version__="$Id$"
fromcgiimportparse_qs
fromsreimportsearch,sub
fromtimeimportlocaltime,strftime,mktime,sleep
importsmtplib
fromconfigimport*
fromsearch_engineimportperform_request_search
fromdbqueryimportrun_sql
fromhtmlparserimport*
fromstringimportsplit
MAXIDS=50
FROMADDR='CDS Alert Engine <%s>'%alertengineemail
ALERTURL=weburl+'/youralerts.py/list'
DEVELOPERADDR=[supportemail]
# Debug levels:
# 0 = production, nothing on the console, email sent
# 1 = messages on the console, email sent
# 2 = messages on the console, but no email sent
# 3 = many messages on the console, no email sent
# 4 = many messages on the console, email sent to DEVELOPERADDR
DEBUGLEVEL=0
defupdate_date_lastrun(alert):
returnrun_sql('update user_query_basket set date_lastrun=%s where id_user=%s and id_query=%s and id_basket=%s;',(strftime("%Y-%m-%d"),alert[0],alert[1],alert[2],))
defget_alert_queries(frequency):
returnrun_sql('select distinct id, urlargs from query q, user_query_basket uqb where q.id=uqb.id_query and uqb.frequency=%s and uqb.date_lastrun <= now();',(frequency,))
defget_alert_queries_for_user(uid):
returnrun_sql('select distinct id, urlargs, uqb.frequency from query q, user_query_basket uqb where q.id=uqb.id_query and uqb.id_user=%s and uqb.date_lastrun <= now();',(uid,))
defget_alerts(query,frequency):
r=run_sql('select id_user, id_query, id_basket, frequency, date_lastrun, alert_name, notification from user_query_basket where id_query=%s and frequency=%s;',(query['id_query'],frequency,))
msg+='\n\n'+wrap('Only the first %s records were displayed. Please consult the search URL given at the top of this email to see all the results.'%MAXIDS)
returnmsg
defemail_notify(alert,records,argstr):
globalFROMADDR
globalALERTURL
globalDEBUGLEVEL
globalDEVELOPERADDR
iflen(records)==0:
return
msg=""
ifDEBUGLEVEL>0:
msg="*** THIS MESSAGE WAS SENT IN DEBUG MODE ***\n\n"
msg+="Hello\n\n"
msg+=wrap("Below are the results of the email alert that you set up with the CERN Document Server. This is an automatic message, please don't reply to its address. For any question, use <%s> instead."%supportemail)