Page MenuHomec4science

webalert.py.wml
No OneTemporary

File Metadata

Created
Fri, Jun 7, 14:26

webalert.py.wml

## $Id$
## PERSONAL FEATURE - YOUR ALERTS
## 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>
"""PERSONAL FEATURES - YOUR ALERTS"""
## rest of the Python code goes below
## fill config variables:
pylibdir = "<LIBDIR>/python"
try:
import cgi
import string
import sys
import time
import urllib
import zlib
sys.path.append('%s' % pylibdir)
from config import *
from webpage import page
from dbquery import run_sql
from webuser import getUid, create_user_infobox,isGuestUser
from webbasket import perform_create_basket
from mod_python import apache
except ImportError, e:
print "Error: %s" % e
import sys
sys.exit(1)
### IMPLEMENTATION
def get_textual_query_info_from_urlargs(urlargs):
"""Return nicely formatted search pattern and catalogue from urlargs of the search query.
Suitable for 'your searches' display."""
out = ""
args = cgi.parse_qs(urlargs)
if args.has_key('p'):
out += "<strong>Pattern:</strong> " + string.join(args['p'], "; ") + "<br>"
if args.has_key('f'):
out += "<strong>Field:</strong> " + string.join(args['f'], "; ") + "<br>"
if args.has_key('p1'):
out += "<strong>Pattern 1:</strong> " + string.join(args['p1'], "; ") + "<br>"
if args.has_key('f1'):
out += "<strong>Field 1:</strong> " + string.join(args['f1'], "; ") + "<br>"
if args.has_key('p2'):
out += "<strong>Pattern 2:</strong> " + string.join(args['p2'], "; ") + "<br>"
if args.has_key('f2'):
out += "<strong>Field 2:</strong> " + string.join(args['f2'], "; ") + "<br>"
if args.has_key('p3'):
out += "<strong>Pattern 3:</strong> " + string.join(args['p3'], "; ") + "<br>"
if args.has_key('f3'):
out += "<strong>Field 3:</strong> " + string.join(args['f3'], "; ") + "<br>"
if args.has_key('c'):
out += "<strong>Collections:</strong> " + string.join(args['c'], "; ") + "<br>"
elif args.has_key('cc'):
out += "<strong>Collection:</strong> " + string.join(args['cc'], "; ") + "<br>"
return out
# perform_display(): display the searches performed by the current user
# input: default permanent="n"; permanent="y" display permanent queries(most popular)
# output: list of searches in formatted html
def perform_display(permanent,uid):
# set variables
out = ""
id_user = uid # XXX
# first detect number of queries:
nb_queries_total = 0
nb_queries_distinct = 0
id_queries_distinct = []
res = run_sql("SELECT COUNT(*),COUNT(DISTINCT(id_query)) FROM user_query WHERE id_user=%s", (uid,), 1)
try:
nb_queries_total = res[0][0]
nb_queries_distinct = res[0][1]
except:
pass
# query for queries:
if permanent=="n":
SQL_query = "SELECT DISTINCT(q.id),q.urlargs "\
"FROM query q, user_query uq "\
"WHERE uq.id_user='%s' "\
"AND uq.id_query=q.id "\
"ORDER BY q.id DESC" % id_user
else:
# permanent="y"
SQL_query = "SELECT q.id,q.urlargs "\
"FROM query q "\
"WHERE q.type='p'"
query_result = run_sql(SQL_query)
# display message: number of items in the list
if permanent=="n":
out += """<P>You have performed <B>%d</B> searches (<strong>%d</strong> different questions) during the last 30 days or so.</P>"""\
% (nb_queries_total, nb_queries_distinct)
else:
# permanent="y"
out += """<P>Here are listed the <B>%s</B> most popular searches.</P>""" % len(query_result)
if len(query_result) > 0:
# display the list of searches
out += """<TABLE border="1" cellspacing="0" cellpadding="3" width="100%">\n"""
# no, pattern, catalogue, action, date
out += """<TR class="pageboxlefttop"><TD><B>No</B></TD><TD><B>Question</B></TD>"""\
"""<TD><B>Action</B></TD>"""
if permanent=="n":
out += """<TD><B>Last Run</B></TD>"""
out += """</TR>\n"""
i = 0
for row in query_result :
i += 1
# id, pattern, base, search url and search set alert, date
out += """<TR><TD><I>#%d</I></TD>"""\
"""<TD>%s</TD>"""\
"""<TD><A href="%s/search.py?%s">Execute&nbsp;search</A><BR><A href="./input_alert?idq=%d">Set&nbsp;new&nbsp;alert</A></TD>"""\
% (i, get_textual_query_info_from_urlargs(row[1]), weburl, row[1], row[0])
if permanent=="n":
# find out the date of last run for this query:
res = run_sql("SELECT DATE_FORMAT(MAX(date),'%%Y-%%m-%%d %%T') FROM user_query WHERE id_user=%s and id_query=%s",
(id_user, row[0]))
try:
out += """<TD>%s</TD>""" % res[0][0]
except:
out += """<TD>-unknown-</TD>"""
out += """</TR>\n"""
out += """</TABLE><BR>\n"""
return out
# perform_input_alert: get the alert settings
# input: action="add" for a new alert (blank form), action="modify" for an update (get old values)
# id_query id the identifier of the search to be alerted
# for the "modify" action specify old alert_name, frequency of checking, e-mail notification and basket id.
# output: alert settings input form
def perform_input_alert(action, id_query, alert_name, frequency, notification, id_basket,uid):
# set variables
out = ""
frequency_month = frequency
frequency_week = ""
frequency_day = ""
notification_yes = ""
notification_no = ""
id_user = uid # XXX
# display query information
res = run_sql("SELECT urlargs FROM query WHERE id=%s", (id_query,))
try:
urlargs = res[0][0]
except:
urlargs = "UNKNOWN"
out += """<TABLE border="0" cellspacing="0" cellpadding="2" width="650">\n"""
out += """<TR><TD colspan="3">This alert will notify you each time/only if a new item satisfy the following query </TD></TR>"""
for row in res:
out += """<TR><TD>&nbsp;&nbsp;</TD><TD align="left" valign="top" width="10"><B>QUERY:</B></TD>"""\
"""<TD align="left" valign="top" width="500">%s</TD></TR>\n""" % get_textual_query_info_from_urlargs(urlargs)
out += """</TABLE>"""
# define alert settings
if action == "update":
out += """<FORM name="setalert" action="../youralerts.py/update_alert" method="get">"""
if frequency == "month":
frequency_month = " selected"
else:
if frequency == "week":
frequency_week = " selected"
else:
# frequency = "day"
frequency_day = " selected"
if notification == "y":
notification_yes = " selected"
else:
# notification = "n"
notification_no = " selected"
else:
# action = "add"
out += """<FORM name="setalert" action="../youralerts.py/add_alert" method="get">"""
frequency_week = " selected"
notification_yes = " selected"
out += """<TABLE style="background-color:F1F1F1; border:thin groove grey" cellspacing="0" cellpadding="0"><TR><TD>"""
out += """<TABLE border="0" cellpadding="0" cellspacing ="10">"""
# alert name
out += """<TR><TD align="right" valign="top"><B>Alert&nbsp;identification&nbsp;name:</B></TD>"""\
"""<TD><INPUT type="text" name="name" size="20" maxlength="50" value="%s"></TD></TR>\n""" % alert_name
# alert frequency
out += """<TR><TD align="right"><B>Search-checking frequency:</B></TD>"""\
"""<TD><SELECT name="freq">"""\
"""<OPTION value="month"%s>montly</OPTION>"""\
"""<OPTION value="week"%s>weekly</OPTION>"""\
"""<OPTION value="day"%s>daily</OPTION></SELECT>\n"""\
"""</TD></TR>""" % (frequency_month, frequency_week, frequency_day)
# alert notification
out += """<TR><TD align="right"><B>Send notification e-mail?</B></TD>"""\
"""<TD><SELECT name="notif">"""\
"""<OPTION value="y"%s>yes</OPTION>"""\
"""<OPTION value="n"%s>no</OPTION></SELECT>"""\
"""<SMALL class="quicknote"> (if <B>no</B> you must specify a basket)</SMALL>&nbsp;</TD></TR>\n""" % (notification_yes, notification_no)
# alert in basket
SQL_query = "SELECT b.id, b.name FROM basket b,user_basket ub "\
"WHERE ub.id_user='%s' AND ub.id_basket=b.id ORDER BY b.name ASC" % id_user
query_result = run_sql(SQL_query)
out += """<TR><TD align="right" valign="top"><B>Store results in basket?</B></TD>"""\
"""<TD><SELECT name="idb"><OPTION value="0">- no basket -</OPTION>"""
for row in query_result :
if (action == "update") and (str(row[0]) == id_basket):
basket_selected = " selected"
else:
basket_selected = ""
out += """<OPTION value="%s"%s>%s</OPTION>""" % (row[0], basket_selected, row[1])
out += """</SELECT><BR><SMALL>or&nbsp;insert&nbsp;a&nbsp;new&nbsp;basket&nbsp;name</SMALL><BR>"""\
"""<INPUT type="text" name="bname" size="20" maxlength="50"></TD></TR>\n"""
# hidden parameters
out += """<TR><TD colspan="2" align="center"><BR>"""\
"""<INPUT type="hidden" name="idq" value="%s">""" % id_query
if action == "update":
out += """<TR><TD colspan="2" align="center"><BR>"""\
"""<INPUT type="hidden" name="old_idb" value="%s">""" % id_basket
# buttons confirmation/clear
out += """<CODE class="blocknote"><INPUT class="formbutton" type="submit" name="action" value="&nbsp;SET ALERT&nbsp;"></CODE>&nbsp;"""\
"""<CODE class="blocknote"><INPUT class="formbutton" type="reset" value="CLEAR DATA"></CODE>"""\
"""</TD></TR>\n"""
out += """</TABLE></TD></TR></TABLE></FORM>"""
return out
# perform_add_alert: add an alert to the database
# input: the name of the new alert;
# alert frequency: 'month', 'week' or 'day';
# setting for e-mail notification: 'y' for yes, 'n' for no;
# basket identifier: 'no' for no basket;
# new basket name for this alert;
# identifier of the query to be alerted
# output: confirmation message + the list of alerts Web page
def perform_add_alert(alert_name, frequency, notification, id_basket, new_basket_name, id_query,uid):
# set variables
out = ""
id_user=uid # XXX
# set the basket identifier
if new_basket_name != "":
# create a new basket
id_basket = perform_create_basket(uid, new_basket_name)
out += """The <I>private</I> basket <B>%s</B> has been created.<BR>\n""" % new_basket_name
# add a row to the alerts table: user_query_basket
SQL_query = "INSERT INTO user_query_basket (id_user, id_query, id_basket, frequency, date_creation, date_lastrun, alert_name, notification) "\
"VALUES ('%s','%s','%s','%s','%s','','%s','%s') " \
% (id_user, id_query, id_basket, frequency,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), alert_name, notification)
query_result = run_sql(SQL_query)
out += """The alert <B>%s</B> has been added to your profile.<BR><BR>""" % alert_name
out += perform_list_alerts(uid)
return out
# perform_list_alerts display the list of alerts for the connected user
def perform_list_alerts (uid):
# set variables
out = ""
id_user = uid # XXX
id_guest = isGuestUser(uid)
# link to the "add new alert" form
out += """<P>Set a new alert from <A href="display">your searches</A>, """\
"""the <A href="display?p='y'">most popular searches</A> or the input form.</P>"""
# query the database
SQL_query = "SELECT q.id, q.urlargs, a.id_user, a.id_query, a.id_basket, "\
"a.alert_name, a.frequency, a.notification, "\
"DATE_FORMAT(a.date_creation,'%%d %%b %%Y'), DATE_FORMAT(a.date_lastrun,'%%d %%b %%Y'), "\
"b.id, b.name "\
"FROM query q, user_query_basket a, basket b "\
"WHERE a.id_user='%s' "\
"AND a.id_query=q.id "\
"AND a.id_basket=b.id "\
"ORDER BY a.alert_name ASC " % id_user
query_result = run_sql(SQL_query)
if len(query_result) > 0:
# display the list of alerts
out += """<TABLE border="1" cellspacing="0" cellpadding="3" width="100%">\n"""
out += """<TR class="pageboxlefttop" align="center"><TD><B>No</B></TD><TD><B>Name</B></TD><TD><B>Search&nbsp;checking<BR>frequency</B></TD>"""\
"""<TD><B>Notification<BR><NOBR>by e-mail<NOBR></B></TD><TD><B>Result&nbsp;in<BR>basket</B></TD><TD><B>Date<BR>last&nbsp;run</B></TD>"""\
"""<TD><B>Creation<BR>date</B></TD><TD><B>Query</B></TD><TD><B>Action</B></TD></TR>\n"""
i = 0
for row in query_result :
i += 1
# set frequency of checking: daily, weekly, monthly
if row[6]=="day":
alert_frequency = "daily"
else:
if row[6]=="week":
alert_frequency = "weekly"
else:
# row[6]="month"
alert_frequency = "monthly"
# set notification by email field: yes or no
if row[7] == "y":
email_notification = "yes"
else:
# row[7] == "n"
email_notification = "no"
# set basket name
if row[11] =="":
basket_name="""<CENTER>--</CENTER>"""
else:
basket_name=row[11]
# id, alert name, frequency, e-mail alert, last run, creation, pattern, catalogue, actions
out += """<TR><TD><I>#%d</I></TD>"""\
"""<TD><B><NOBR>%s<NOBR></B></TD>"""\
"""<TD>%s</TD>"""\
"""<TD align="center">%s</TD>"""\
"""<TD><NOBR>%s<NOBR></TD>"""\
"""<TD><NOBR>%s<NOBR></TD>"""\
"""<TD><NOBR>%s<NOBR></TD>"""\
"""<TD>%s</TD>"""\
"""<TD><A href="./remove_alert?name=%s&idu=%d&idq=%d&idb=%d">Remove</A><BR>"""\
"""<A href="./modify_alert?idq=%d&name=%s&freq=%s&notif=%s&idb=%d">Modify</A><BR>"""\
"""<A href="%s/search.py?%s">Execute&nbsp;search</A></TD></TR>"""\
% (i,row[5],alert_frequency,email_notification,basket_name,row[9],row[8],
get_textual_query_info_from_urlargs(row[1]),row[5],row[2],row[3],row[4],row[3],row[5],row[6],row[7],row[4],weburl,row[1])
out += """</TABLE>\n"""
out += """<P>You have defined <B>%s</B> alerts.</P>""" % len(query_result)
if (id_guest == 1):
out += """<br><FONT color="red"> You are logged in as a <B>guest</B> user, so your baskets
will disappear at the end of the current session. If you wish you can login or register
<A href="../youraccount.py/login">here</A>.</FONT>"""
return out
# perform_remove_alert: remove an alert from the database
# input: identifier of the user;
# identifier of the query;
# identifier of the basket
# output: confirmation message + the list of alerts Web page
def perform_remove_alert( alert_name, id_user, id_query, id_basket,uid):
# set variables
out = ""
# remove a row from the alerts table: user_query_basket
SQL_query = "DELETE FROM user_query_basket "\
"WHERE id_user='%s' AND id_query='%s' AND id_basket='%s'" \
% (id_user, id_query, id_basket)
query_result = run_sql(SQL_query)
out += """The alert <B>%s</B> has been removed from your profile.<BR><BR>\n""" % alert_name
out += perform_list_alerts(uid)
return out
# perform_update_alert: update alert settings into the database
# input: the name of the new alert;
# alert frequency: 'month', 'week' or 'day';
# setting for e-mail notification: 'y' for yes, 'n' for no;
# new basket identifier: 'no' for no basket;
# new basket name for this alert;
# identifier of the query to be alerted
# old identifier of the basket associated to the alert
# output: confirmation message + the list of alerts Web page
def perform_update_alert(alert_name, frequency, notification, id_basket, new_basket_name, id_query, old_id_basket,uid):
#set variables
out = ""
id_user = uid # XXX
# set the basket identifier
if new_basket_name != "":
# create a new basket
id_basket = perform_create_basket(uid, new_basket_name)
out += """The <I>private</I> basket <B>%s</B> has been created.<BR>\n""" % new_basket_name
# update a row into the alerts table: user_query_basket
SQL_query = "UPDATE user_query_basket "\
"SET alert_name='%s',frequency='%s',notification='%s',date_creation='%s',date_lastrun='',id_basket='%s' "\
"WHERE id_user='%s' AND id_query='%s' AND id_basket='%s'" \
% (alert_name,frequency,notification,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),id_basket,id_user,id_query,old_id_basket)
#date_creation
#date_lastrun
query_result = run_sql(SQL_query)
out += """The alert <B>%s</B> has been successfully updated.<BR><BR>\n""" % alert_name
out += perform_list_alerts(uid)
return out

Event Timeline