Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F76566344
webalert.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Aug 8, 21:12
Size
15 KB
Mime Type
text/x-c
Expires
Sat, Aug 10, 21:12 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
19724880
Attached To
R3600 invenio-infoscience
webalert.py
View Options
## $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
sys
import
time
import
zlib
import
urllib
import
time
sys
.
path
.
append
(
'
%s
'
%
pylibdir
)
from
config
import
*
from
webpage
import
page
from
dbquery
import
run_sql
from
user
import
getUid
,
create_user_infobox
from
mod_python
import
apache
except
ImportError
,
e
:
print
"Error:
%s
"
%
e
import
sys
sys
.
exit
(
1
)
### IMPLEMENTATION
# 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
# query the database
if
permanent
==
"n"
:
SQL_query
=
"SELECT q.id,q.pattern,q.base,q.url, DATE_FORMAT(uq.date,'
%%
d
%%
b
%%
Y<BR>
%%
H:
%%
i') "
\
"FROM query q, user_query uq "
\
"WHERE uq.id_user='
%s
' "
\
"AND uq.id_query=q.id "
\
"ORDER BY uq.date DESC"
%
id_user
else
:
# permanent="y"
SQL_query
=
"SELECT q.id,q.pattern,q.base,q.url "
\
"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>%s</B> searches during the last 30 days.</P>"""
%
len
(
query_result
)
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>Pattern</B></TD><TD><B>Catalogue</B></TD>"""
\
"""<TD><B>Action</B></TD>"""
if
permanent
==
"n"
:
out
+=
"""<TD><B>Date</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>%s</TD>"""
\
"""<TD><A href="%s">Execute search</A><BR><A href="./input_alert?idq=%d">Set new alert</A></TD>"""
\
%
(
i
,
row
[
1
],
row
[
2
],
row
[
3
],
row
[
0
])
if
permanent
==
"n"
:
out
+=
"""<TD>%s</TD>"""
%
row
[
4
]
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
SQL_query
=
"SELECT base,pattern FROM query WHERE id='
%s
'"
%
id_query
query_result
=
run_sql
(
SQL_query
)
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
query_result
:
out
+=
"""<TR><TD> </TD><TD align="left" width="10"><B>Search pattern:</B></TD>"""
\
"""<TD align="left" width="500">%s</TD></TR>\n"""
%
row
[
1
]
out
+=
"""<TR><TD> </TD><TD align="left" width="10"><B>in catalogue:</B></TD>"""
\
"""<TD align="left" width="500">%s</TD></TR>\n"""
%
row
[
0
]
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 identification 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> </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 insert a new basket 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 type="submit" name="action" value=" SET ALERT "></CODE> """
\
"""<CODE class="blocknote"><INPUT 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
=
yourbaskets
.
perform_create_basket
(
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
# 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.pattern,q.base,q.url, 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 checking<BR>frequency</B></TD>"""
\
"""<TD><B>Notification<BR><NOBR>by e-mail<NOBR></B></TD><TD><B>Result in<BR>basket</B></TD><TD><B>Date<BR>last run</B></TD>"""
\
"""<TD><B>Creation<BR>date</B></TD><TD><B>Search<BR>pattern</B></TD><TD><B>Search<BR>catalogue</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
[
8
]
==
"day"
:
alert_frequency
=
"daily"
else
:
if
row
[
8
]
==
"week"
:
alert_frequency
=
"weekly"
else
:
# row[8]="month"
alert_frequency
=
"monthly"
# set notification by email field: yes or no
if
row
[
9
]
==
"y"
:
email_notification
=
"yes"
else
:
# row[9] == "n"
email_notification
=
"no"
# set basket name
if
row
[
13
]
==
""
:
basket_name
=
"""<CENTER>--</CENTER>"""
else
:
basket_name
=
row
[
13
]
# 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>%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¬if=%s&idb=%d">Modify</A><BR>"""
\
"""<A href="%s">Execute search</A></TD></TR>"""
\
%
(
i
,
row
[
7
],
alert_frequency
,
email_notification
,
basket_name
,
row
[
11
],
row
[
10
],
row
[
1
],
row
[
2
],
row
[
7
],
row
[
4
],
row
[
5
],
row
[
6
],
row
[
5
],
row
[
7
],
row
[
8
],
row
[
9
],
row
[
6
],
row
[
3
])
out
+=
"""</TABLE>\n"""
out
+=
"""<P>You have defined <B>%s</B> alerts.</P>"""
%
len
(
query_result
)
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
):
# 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
=
yourbaskets
.
perform_create_basket
(
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
Log In to Comment