diff --git a/modules/webbasket/lib/.cvsignore b/modules/webbasket/lib/.cvsignore index 9928cf5cf..df3ddb84d 100644 --- a/modules/webbasket/lib/.cvsignore +++ b/modules/webbasket/lib/.cvsignore @@ -1,7 +1,6 @@ Makefile Makefile.in z_* *.O *.shtml -*.py *~ \ No newline at end of file diff --git a/modules/webbasket/lib/Makefile.am b/modules/webbasket/lib/Makefile.am index 2a65a38ac..3ba4ed52b 100644 --- a/modules/webbasket/lib/Makefile.am +++ b/modules/webbasket/lib/Makefile.am @@ -1,29 +1,26 @@ ## $Id$ ## This file is part of the CERN Document Server Software (CDSware). ## Copyright (C) 2002, 2003, 2004, 2005 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. pylibdir=$(libdir)/python/cdsware -pylib_DATA=webbasket.py -FILESWML = $(wildcard $(srcdir)/*.wml) -EXTRA_DIST = $(FILESWML:$(srcdir)/%=%) +pylib_DATA=webbasket.py -CLEANFILES = $(pylib_DATA) *~ *.tmp *.pyc +EXTRA_DIST = $(pylib_DATA) -%.py: %.py.wml $(top_srcdir)/config/config.wml $(top_builddir)/config/configbis.wml - $(WML) -o $@ $< \ No newline at end of file +CLEANFILES = *~ *.tmp *.pyc diff --git a/modules/webbasket/lib/webbasket.py b/modules/webbasket/lib/webbasket.py index 85d2fe4b7..a1bd83885 100644 --- a/modules/webbasket/lib/webbasket.py +++ b/modules/webbasket/lib/webbasket.py @@ -1,813 +1,811 @@ ## $Id$ ## Personal features - your baskets. ## This file is part of the CERN Document Server Software (CDSware). ## Copyright (C) 2002, 2003, 2004, 2005 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. -## $Id$ -## DO NOT EDIT THIS FILE! IT WAS AUTOMATICALLY GENERATED FROM CDSware WML SOURCES. """Web Baskets features.""" import sys import time import zlib import urllib from config import * from webpage import page from dbquery import run_sql from webuser import getUid, getDataUid,isGuestUser from search_engine import print_record from webaccount import warning_guest_user imagesurl = "%s/img" % weburl ### IMPLEMENTATION # perform_display(): display the baskets defined by the current user # input: default action="" display the list of baskets and the content of the selected basket; # action="DELETE" delete the selected basket; # action="RENAME" modify the basket name; # action="CREATE NEW" create a new basket; # action="SET PUBLIC" set access permission to public; # action="SET PRIVATE" set access permission to private; # action="REMOVE" remove selected items from basket; # action="EXECUTE" copy/move selected items to another basket; # action="ORDER" change the order of the items in the basket; # id_basket is the identifier of the selected basket # delete_alerts='n' if releted alerts shouldn't be deleted; 'y' if yes # confirm_action="CANCEL"cancel the delete action/="CONFIRM" confirm the delete action; # bname is the old basket name for renaming # newname is the new name for renaming the basket # mark[] contains the list of identifiers of the items to be removed # to_basket is the destination basket identifier for copy or move items # copy_move="1" if copy items is requested,"2" if move items is requested # idup, ordup are the identifier and the order of the item to be moved up # iddown, orddown are the identifier and the order of the item to be moved down # of is the output format code # output: list of baskets in formatted html+content of the selected basket def perform_display(uid, action="", delete_alerts="", confirm_action="", id_basket=0, bname="", newname="", newbname="", mark=[], to_basket="", copy_move="", idup="", ordup="", iddown="", orddown="", of="hb"): # set variables out = "" basket_name = "" public_basket="no" permission = [] bname = get_basket_name( id_basket ) # execute the requested action if (action == "DELETE") and (id_basket != '0') and (id_basket != 0): if (confirm_action == "CANCEL") or (confirm_action == "CONFIRM"): try: msg = perform_delete(uid, delete_alerts, confirm_action, id_basket) out += "%s
" % msg except BasketException, e: out += "The basket has not been deleted: %s" % e show_actions = 1 else: # goes to the form which deletes the selected basket out += delete_basket(uid, id_basket, bname) basket_name = bname show_actions = 0 id_basket = '0' else: show_actions = 1 if action == "CREATE NEW": # create a new basket if newname != "": # create a new basket newname try: id_basket = perform_create_basket(uid, newname) out += """The private basket %s has been created.
\n""" % newname except BasketException, e: out += """The basket %s has not been created: %s""" % (newname, e) else: out += """The basket has not been created: specify a basket name.""" else: if (id_basket != '0') and (id_basket != 0): if action == "RENAME": # rename the selected basket if newbname != "": # rename basket to newname try: id_basket = perform_rename_basket(uid, id_basket,newbname) out += """The basket %s has been renamed to %s.
\n""" % (bname, newbname) except BasketException, e: out += """The basket has not been renamed: %s"""%e else: out += """The basket has not been renamed: specify a basket name.""" else: if action == "SET PUBLIC": try: # set public permission set_permission(uid, id_basket, "y") url_public_basket = """%s/yourbaskets.py/display_public?id_basket=%s""" \ % (weburl, id_basket) out += """The selected basket is now publicly accessible at the following URL:
"""\ """%s

""" % (url_public_basket, url_public_basket) except BasketException, e: out += "The basket has not been made public: %s"%e else: if action == "SET PRIVATE": # set private permission try: set_permission(uid, id_basket, "n") out += """The selected basket is no more publically accessible.
""" except BasketException, e: out += "The basket has not been made private: %s"%e else: if action == "REMOVE": # remove the selected items from the basket try: remove_items(uid, id_basket, mark) out += """The selected items have been removed.
""" except BasketException, e: out += """The items have not been removed: %s"""%e else: if action == "EXECUTE": # copy/move the selected items to another basket if to_basket == '0': out += """Select a destination basket to copy/move items.
""" else: move_items(uid, id_basket, mark, to_basket, copy_move) out += """The selected items have been copied/moved.
""" else: if action == "ORDER": # change the order of the items in the basket try: order_items(uid, id_basket,idup,ordup,iddown,orddown) except BasketException, e: out += """The items have not been re-ordered: %s"""%e # display the basket's action form if (show_actions): # query the database for the list of baskets query_result = run_sql("SELECT b.id, b.name, b.public, ub.date_modification "\ "FROM basket b, user_basket ub "\ "WHERE ub.id_user=%s AND b.id=ub.id_basket "\ "ORDER BY b.name ASC ", (uid,)) out += """
""" if len(query_result) == 0: # create new basket form out += """No baskets have been defined.
""" out += """New basket name: """\ """ """\ """""" else: # display the list of baskets out += """You own %s baskets.
""" % len(query_result) out += """Select an existing basket: """\ """\n""" # buttons for basket's selection or creation out += """ """\ """\n""" out += """  or """\ """ """\ """

""" if id_basket: out += """\n"""\ """""" # basket permission private/public i = 0 while (i < len(permission)) : if (str(permission[i][0]) == id_basket): break i += 1 if (i < len(permission)): if (permission[i][1] == "n"): public_basket="no" out += """\n""" else : public_basket="yes" out += """\n""" if (public_basket=="yes"): url_public_basket = """%s/yourbaskets.py/display_public?id_basket=%s""" \ % (weburl, id_basket) out += """""" \ % (url_public_basket, url_public_basket) # is basket related to some alerts? alert_query_result = run_sql("SELECT alert_name FROM user_query_basket WHERE id_user=%s AND id_basket=%s", (uid, id_basket)) out += """
""" # buttons for actions: display basket content, delete, rename, create a new basket # basket delete out += """
The selected basket is %s. """ % basket_name # basket rename out += """
Basket access is set to private, convert to public?"""\ """
Basket access is set to public, convert to private?
"""\ """
Public URL: %s
""" if len(alert_query_result) == 0: out += """There isn't any alert related to this basket.""" else: out += """The following alerts are related to this basket: """ i = 1 for row in alert_query_result: if i == 1: out += """%s""" % row[0] i+=1 else: out += """, %s""" % row[0] i+=1 out += """
""" out += """
""" # hidden parameters out += """""" % basket_name out += """
""" # display the content of the selected basket if ((id_basket != '0') and (id_basket != 0)): if (basket_name == ""): if (newname != ""): basket_name = newname else: if (newbname != ""): basket_name = newbname out += display_basket_content(uid, id_basket, basket_name, of) # if is guest user print message of relogin if isGuestUser(uid): out += warning_guest_user(type="baskets") return out # display_basket_content: display the content of the selected basket # input: the identifier of the basket # the name of the basket # output: the basket's content def display_basket_content(uid, id_basket, basket_name, of): out = "" out_tmp="" # search for basket's items if (id_basket != '0') and (id_basket != 0): query_result = run_sql("SELECT br.id_record,br.nb_order "\ "FROM basket_record br "\ "WHERE br.id_basket=%s "\ "ORDER BY br.nb_order DESC ", (id_basket,)) if len(query_result) > 0: out += out_tmp # display the list of items out += """
""" out += """\n
""" out += """""" # display operations on the selected items: delete, copy or move out += """\n""" # display the list of items i = 1 preid = 0 preord = 0 for row in query_result : if i==1: out += """"""\ """"""\ """""" % (id_basket,row[0],row[1],preid,preord,imagesurl,preabstract) # display current item out += """"""\ """"""\ """""" % (imagesurl,preabstract) # hidden parameters out += """""" % id_basket out += """
Selected items: """\ """\n""" out += """  or  to""" # query the database for the list of baskets query_result1 = run_sql("SELECT b.id, b.name "\ "FROM basket b, user_basket ub "\ "WHERE ub.id_user=%s AND b.id=ub.id_basket AND b.id<>%s "\ "ORDER BY b.name ASC ", (uid,id_basket)) # display the list of baskets if len(query_result1) > 0: out +="""\n""" out +="""

%s"""\ """""" % (i,row[0],imagesurl) else: # complete display previous item out += """"""\ """"""\ """%s
%s"""\ """"""\ """""" % (i,row[0],id_basket,row[0],row[1],preid,preord,imagesurl) preid = row[0] preord = row[1] preabstract = print_record(row[0], of) i += 1 # complete display last item out += """"""\ """%s
""" else: out += """

The basket %s is empty.""" % basket_name out += out_tmp return out # delete_basket: present a form for the confirmation of the delete action # input: the identifier of the selected basket # the name of the selected basket # output: the information about the selected basket and the form for the confirmation of the delete action def delete_basket(uid, id_basket, basket_name): # set variables out = "" # search for related alerts out += """

""" out += """\n
""" out += """""" query_result = run_sql("SELECT alert_name FROM user_query_basket WHERE id_user=%s AND id_basket=%s", (uid, id_basket)) if len(query_result) == 0: Msg = """There isn't any alert related to this basket.""" out += """""" % Msg else: Msg = """The following alerts are related to this basket: """ i = 1 for row in query_result: if i == 1: Msg += """%s""" % row[0] i+=1 else: Msg += """, %s""" % row[0] i+=1 out += """""" % Msg out += """"""\ """""" # confirm delete action? yes or no out += """""" % basket_name out += """""" % id_basket out += """""" out += """
%s
%s
Do you want to remove the related alerts too? 
Delete the basket %s ? """\ """ """ # hidden parameters out += """
""" return out # perform_delete: present a form for the confirmation of the delete action # input: delete_alerts='n' if releted alerts shouldn't be deleted; 'y' if yes # action='YES' if delete action has been confirmed; 'NO' otherwise # id_basket contains the identifier of the selected basket # output: go back to the display baskets form with confirmation message def perform_delete(uid, delete_alerts, confirm_action, id_basket,): # set variables out = "" if (confirm_action=='CONFIRM'): #check that the user which is changing the basket name is the owner of it if not is_basket_owner( uid, id_basket ): raise NotBasketOwner("You are not the owner of this basket") # perform the cancellation msg = "The selected basket has been deleted." if (delete_alerts=='y'): # delete the related alerts, remove from the alerts table: user_query_basket query_result = run_sql("DELETE FROM user_query_basket WHERE id_user=%s AND id_basket=%s", (uid, id_basket)) msg += " The related alerts have been removed." else: # replace the basket identifier with 0 # select the records to update query_result = run_sql("SELECT id_query,alert_name,frequency,notification,date_creation,date_lastrun "\ "FROM user_query_basket WHERE id_user=%s AND id_basket=%s", (uid, id_basket)) # update the records for row in query_result: query_result_temp = run_sql("UPDATE user_query_basket "\ "SET alert_name=%s,frequency=%s,notification=%s,"\ "date_creation=%s,date_lastrun=%s,id_basket='0' "\ "WHERE id_user=%s AND id_query=%s AND id_basket=%s", (row[1],row[2],row[3],row[4],row[5],uid,row[0],id_basket)) # delete the relation with the user table query_result = run_sql("DELETE FROM user_basket WHERE id_user=%s AND id_basket=%s", (uid, id_basket)) # delete the basket information query_result = run_sql("DELETE FROM basket WHERE id=%s", (id_basket,)) # delete the basket content query_result = run_sql("DELETE FROM basket_record WHERE id_basket=%s", (id_basket,)) else: msg="" return msg # perform_rename_basket: rename an existing basket # input: basket identifier, basket new name # output: basket identifier def perform_rename_basket(uid, id_basket, newname): # check that there's no basket owned by this user with the same name if has_user_basket( uid, newname): raise BasketNameAlreadyExists("You already have a basket which name is '%s'"%newname) #check that the user which is changing the basket name is the owner of it if not is_basket_owner( uid, id_basket ): raise NotBasketOwner("You are not the owner of this basket") # update a row to the basket table tmp = run_sql("UPDATE basket SET name=%s WHERE id=%s", (newname, id_basket)) return id_basket class BasketException(Exception): """base exception class for basket related errors """ pass class BasketNameAlreadyExists(BasketException): """exception which is raised when a basket already exists with a certain name for a user """ pass class NotBasketOwner(BasketException): """exception which is raised when a user which is not the owner of a basket tries to perform an operation over it for which he has no privileges """ pass def has_user_basket(uid, basket_name): """checks if a user (uid) already has a basket which name is 'basket_name' (case-sensitive) """ return run_sql("select b.id from basket b, user_basket ub where ub.id_user=%s and b.id=ub.id_basket and b.name=%s", (uid, basket_name.strip())) def is_basket_owner(uid, bid): """checks whether or not the user (uid) is owner for the indicated basket (bid) """ return run_sql("select id_basket from user_basket where id_user=%s and id_basket=%s", (uid, bid)) def get_basket_name(bid): """returns the name of the basket corresponding to the given id """ res = run_sql("select name from basket where id=%s", (bid,)) if not res: return "" return res[0][0] # perform_create_basket: create a new basket and the relation with the user table # input: basket name # output: basket identifier def perform_create_basket(uid, basket_name): # check that there's no basket owned by this user with the same name if has_user_basket(uid, basket_name): raise BasketNameAlreadyExists("You already have a basket which name is '%s'"%basket_name) # add a row to the basket table id_basket = run_sql("INSERT INTO basket(id,name,public) VALUES ('0',%s,'n')", (basket_name,)) # create the relation between the user and the basket: user_basket query_result = run_sql("INSERT INTO user_basket(id_user,id_basket,date_modification) VALUES (%s,%s,%s)", (uid, id_basket, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) return id_basket # basket_exists checks if a basket is in the database # input: the name of the basket # output: the id of the basket if it exists, 0 otherwise def basket_exists (basket_name, uid): id_basket = run_sql("SELECT b.id FROM basket b, user_basket ub "\ "WHERE b.name=%s "\ "AND b.id=ub.id_basket "\ "AND ub.id_user=%s", (basket_name, uid)) return id_basket # set_permission: set access permission on a basket # input: basket identifier, basket public permission # output: basket identifier def set_permission(uid, id_basket, permission): #check that the user which is changing the basket name is the owner of it if not is_basket_owner( uid, id_basket ): raise NotBasketOwner("You are not the owner of this basket") # update a row to the basket table id_basket = run_sql("UPDATE basket SET public=%s WHERE id=%s", (permission, id_basket)) return id_basket # remove_items: remove the selected items from the basket # input: basket identifier, list of selected items # output: basket identifier def remove_items(uid, id_basket, mark): #check that the user which is changing the basket name is the owner of it if not is_basket_owner( uid, id_basket ): raise NotBasketOwner("You are not the owner of this basket") if type(mark)==list: selected_items=mark else: selected_items=[mark] for i in selected_items: # delete the basket content query_result = run_sql("DELETE FROM basket_record WHERE id_basket=%s AND id_record=%s", (id_basket, i)) return id_basket # check_copy: check if the record exists already in the basket # input: basket identifier, list of selected items # output: boolean def check_copy(idbask,i): query_result = run_sql("select * from basket_record where id_basket=%s and id_record=%s", (idbask,i)) if len(query_result)>0 : return 0 return 1 # copy/move the selected items to another basket # input: original basket identifier, list of selected items, # destination basket identifier, copy or move option: "1"=copy, "2"=move #output: basket identifier def move_items(uid, id_basket, mark, to_basket, copy_move="1"): if type(mark)==list: selected_items=mark else: selected_items=[mark] for i in selected_items: if check_copy(to_basket,i): query_result = run_sql("INSERT INTO basket_record(id_basket,id_record,nb_order) VALUES (%s,%s,'0')", (to_basket, i)) if copy_move=="2": #delete from previous basket remove_items(uid, id_basket, mark) return id_basket # change the order of the items in the basket # input: basket identifier # identifiers and positions of the items to be moved #output: basket identifier def order_items(uid, id_basket,idup,ordup,iddown,orddown): #check that the user which is changing the basket name is the owner of it if not is_basket_owner( uid, id_basket ): raise NotBasketOwner("You are not the owner of this basket") # move up the item idup (by switching its order number with the other item): query_result = run_sql("UPDATE basket_record SET nb_order=%s WHERE id_basket=%s AND id_record=%s", (orddown,id_basket,idup)) # move down the item iddown (by switching its order number with the other item): query_result = run_sql("UPDATE basket_record SET nb_order=%s WHERE id_basket=%s AND id_record=%s", (ordup,id_basket,iddown)) return id_basket # perform_display_public: display the content of the selected basket, if public # input: the identifier of the basket # the name of the basket # of is the output format code # output: the basket's content def perform_display_public(uid, id_basket, basket_name, action, to_basket, mark, newname, of): out = "" if action=="EXECUTE": # perform actions if newname != "": # create a new basket to_basket = perform_create_basket(uid, newname) out += """The private basket %s has been created.
\n""" % newname # copy the selected items if to_basket == '0': out += """Select a destination basket to copy the selected items.
""" else: move_items(uid, id_basket, mark, to_basket, '1') out += """The selected items have been copied.
""" # search for basket's items if (id_basket != '0') and (id_basket != 0): res = run_sql("select public from basket where id=%s", (id_basket,)) if len(res) == 0: out += """Non existing basket""" return out if str(res[0][0]).strip() != 'y': out += """The basket is private""" return out query_result = run_sql("SELECT br.id_record,br.nb_order "\ "FROM basket_record br "\ "WHERE br.id_basket=%s "\ "ORDER BY br.nb_order DESC ", (id_basket,)) if len(query_result) > 0: out += """Content of the public basket %s :
""" % basket_name # display the list of items out += """
""" out += """\n
""" out += """""" # copy selected items to basket query_result1 = run_sql("SELECT b.id, b.name "\ "FROM basket b, user_basket ub "\ "WHERE ub.id_user=%s AND b.id=ub.id_basket "\ "ORDER BY b.name ASC ", (uid,)) if len(query_result1) > 0: out += """Copy the selected items to """ out += """\n""" out += """ or new """ else: out += """Copy the selected items to new basket """ out += """  """ out += """

\n""" # display the list of items i = 1 preid = 0 preord = 0 for row in query_result : if i==1: out += """"""\ """"""\ """""" % (id_basket,row[0],row[1],preid,preord,imagesurl,preabstract) # display current item out += """"""\ """"""\ """""" % (imagesurl,preabstract) # hidden parameters out += """""" % id_basket out += """""" % basket_name out += """
%s"""\ """""" % (i,row[0],imagesurl) else: # complete display previous item out += """"""\ """"""\ """%s
%s"""\ """"""\ """""" % (i,row[0],id_basket,row[0],row[1],preid,preord,imagesurl) preid = row[0] preord = row[1] preabstract = print_record(row[0], of) i += 1 # complete display last item out += """"""\ """%s
""" else: out += """The basket %s is empty.""" % basket_name return out ## --- new stuff starts here --- def perform_request_add(uid=-1, recid=[], bid=[], bname=[]): """Add records recid to baskets bid for user uid. If bid isn't set, it'll ask user into which baskets to add them. If bname is set, it'll create new basket with this name, and add records there rather than to bid.""" out = "" # wash arguments: recIDs = recid bskIDs = bid if not type(recid) is list: recIDs = [recid] if not type(bid) is list: bskIDs = [bid] # sanity checking: if recIDs == []: return "

No records to add." # do we have to create some baskets? if bname: try: new_basket_ID = perform_create_basket(uid, bname) bskIDs = [new_basket_ID] except BasketException, e: out += """The basket %s has not been created: %s""" % (bname, e) if bskIDs == []: # A - some information missing, so propose list of baskets to choose from basket_id_name_list = get_list_of_user_baskets(uid) if basket_id_name_list != []: # there are some baskets; good out += "

Please choose the basket you want to add %d records to:" % len(recIDs) out += """

""" % weburl for recID in recIDs: out += """""" % recID out += """""" out += """""" out += """
""" else: # user have to create a basket first out += """

You don't own baskets defined yet.""" out += """

""" % weburl for recID in recIDs: out += """""" % recID out += """New basket name: """ out += """""" out += """""" out += """
""" else: # B - we have baskets IDs, so we can add records out += """

Adding %s records to basket(s)...""" % len(recIDs) for bskID in bskIDs: if is_basket_owner(uid, bskID): for recID in recIDs: try: res = run_sql("INSERT INTO basket_record(id_basket,id_record,nb_order) VALUES (%s,%s,%s)", (bskID,recID,'0')) except: pass # maybe records were already there? page reload happened? out += """done.""" else: out += """sorry, you are not the owner of this basket.""" out += perform_display(uid=uid, id_basket=bskIDs[0]) return out def get_list_of_user_baskets(uid): """Return list of lists [[basket_id, basket_name],[basket_id, basket_name],...] for the given user.""" out = [] res = run_sql("SELECT b.id, b.name "\ "FROM basket b, user_basket ub "\ "WHERE ub.id_user=%s AND b.id=ub.id_basket "\ "ORDER BY b.name ASC ", (uid,)) for row in res: out.append([row[0], row[1]]) return out def account_list_baskets(uid, action="", id_basket=0, newname=""): out = "" # query the database for the list of baskets query_result = run_sql("SELECT b.id, b.name, b.public, ub.date_modification "\ "FROM basket b, user_basket ub "\ "WHERE ub.id_user=%s AND b.id=ub.id_basket "\ "ORDER BY b.name ASC ", (uid,)) out += """

""" out += """You own the following baskets: """ out += """\n""" # buttons for basket's selection or creation out += """ """\ """\n""" out += """  or """\ """ """\ """

""" out += """
""" return out diff --git a/modules/webbasket/lib/webbasket.py.wml b/modules/webbasket/lib/webbasket.py.wml deleted file mode 100644 index 85d2fe4b7..000000000 --- a/modules/webbasket/lib/webbasket.py.wml +++ /dev/null @@ -1,813 +0,0 @@ -## $Id$ -## Personal features - your baskets. - -## This file is part of the CERN Document Server Software (CDSware). -## Copyright (C) 2002, 2003, 2004, 2005 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. - -## $Id$ -## DO NOT EDIT THIS FILE! IT WAS AUTOMATICALLY GENERATED FROM CDSware WML SOURCES. -"""Web Baskets features.""" - -import sys -import time -import zlib -import urllib -from config import * -from webpage import page -from dbquery import run_sql -from webuser import getUid, getDataUid,isGuestUser -from search_engine import print_record -from webaccount import warning_guest_user - -imagesurl = "%s/img" % weburl - -### IMPLEMENTATION - -# perform_display(): display the baskets defined by the current user -# input: default action="" display the list of baskets and the content of the selected basket; -# action="DELETE" delete the selected basket; -# action="RENAME" modify the basket name; -# action="CREATE NEW" create a new basket; -# action="SET PUBLIC" set access permission to public; -# action="SET PRIVATE" set access permission to private; -# action="REMOVE" remove selected items from basket; -# action="EXECUTE" copy/move selected items to another basket; -# action="ORDER" change the order of the items in the basket; -# id_basket is the identifier of the selected basket -# delete_alerts='n' if releted alerts shouldn't be deleted; 'y' if yes -# confirm_action="CANCEL"cancel the delete action/="CONFIRM" confirm the delete action; -# bname is the old basket name for renaming -# newname is the new name for renaming the basket -# mark[] contains the list of identifiers of the items to be removed -# to_basket is the destination basket identifier for copy or move items -# copy_move="1" if copy items is requested,"2" if move items is requested -# idup, ordup are the identifier and the order of the item to be moved up -# iddown, orddown are the identifier and the order of the item to be moved down -# of is the output format code -# output: list of baskets in formatted html+content of the selected basket -def perform_display(uid, action="", delete_alerts="", confirm_action="", id_basket=0, bname="", newname="", newbname="", mark=[], to_basket="", copy_move="", idup="", ordup="", iddown="", orddown="", of="hb"): - - # set variables - out = "" - basket_name = "" - public_basket="no" - permission = [] - bname = get_basket_name( id_basket ) - - # execute the requested action - if (action == "DELETE") and (id_basket != '0') and (id_basket != 0): - - if (confirm_action == "CANCEL") or (confirm_action == "CONFIRM"): - try: - msg = perform_delete(uid, delete_alerts, confirm_action, id_basket) - out += "%s
" % msg - except BasketException, e: - out += "The basket has not been deleted: %s" % e - show_actions = 1 - else: - # goes to the form which deletes the selected basket - out += delete_basket(uid, id_basket, bname) - basket_name = bname - show_actions = 0 - - id_basket = '0' - else: - show_actions = 1 - if action == "CREATE NEW": - # create a new basket - if newname != "": - # create a new basket newname - try: - id_basket = perform_create_basket(uid, newname) - out += """The private basket %s has been created.
\n""" % newname - except BasketException, e: - out += """The basket %s has not been created: %s""" % (newname, e) - else: - out += """The basket has not been created: specify a basket name.""" - else: - if (id_basket != '0') and (id_basket != 0): - if action == "RENAME": - # rename the selected basket - if newbname != "": - # rename basket to newname - try: - id_basket = perform_rename_basket(uid, id_basket,newbname) - out += """The basket %s has been renamed to %s.
\n""" % (bname, newbname) - except BasketException, e: - out += """The basket has not been renamed: %s"""%e - else: - out += """The basket has not been renamed: specify a basket name.""" - else: - if action == "SET PUBLIC": - try: - # set public permission - set_permission(uid, id_basket, "y") - url_public_basket = """%s/yourbaskets.py/display_public?id_basket=%s""" \ - % (weburl, id_basket) - out += """The selected basket is now publicly accessible at the following URL:
"""\ - """%s

""" % (url_public_basket, url_public_basket) - except BasketException, e: - out += "The basket has not been made public: %s"%e - else: - if action == "SET PRIVATE": - # set private permission - try: - set_permission(uid, id_basket, "n") - out += """The selected basket is no more publically accessible.
""" - except BasketException, e: - out += "The basket has not been made private: %s"%e - else: - if action == "REMOVE": - # remove the selected items from the basket - try: - remove_items(uid, id_basket, mark) - out += """The selected items have been removed.
""" - except BasketException, e: - out += """The items have not been removed: %s"""%e - else: - if action == "EXECUTE": - # copy/move the selected items to another basket - if to_basket == '0': - out += """Select a destination basket to copy/move items.
""" - else: - move_items(uid, id_basket, mark, to_basket, copy_move) - out += """The selected items have been copied/moved.
""" - else: - if action == "ORDER": - # change the order of the items in the basket - try: - order_items(uid, id_basket,idup,ordup,iddown,orddown) - except BasketException, e: - out += """The items have not been re-ordered: %s"""%e - - - # display the basket's action form - if (show_actions): - - # query the database for the list of baskets - query_result = run_sql("SELECT b.id, b.name, b.public, ub.date_modification "\ - "FROM basket b, user_basket ub "\ - "WHERE ub.id_user=%s AND b.id=ub.id_basket "\ - "ORDER BY b.name ASC ", - (uid,)) - out += """
""" - - if len(query_result) == 0: - # create new basket form - out += """No baskets have been defined.
""" - out += """New basket name: """\ - """ """\ - """""" - else: - # display the list of baskets - out += """You own %s baskets.
""" % len(query_result) - out += """Select an existing basket: """\ - """\n""" - - # buttons for basket's selection or creation - out += """ """\ - """\n""" - out += """  or """\ - """ """\ - """

""" - - if id_basket: - out += """\n"""\ - """""" - - - # basket permission private/public - i = 0 - while (i < len(permission)) : - if (str(permission[i][0]) == id_basket): - break - i += 1 - if (i < len(permission)): - if (permission[i][1] == "n"): - public_basket="no" - out += """\n""" - else : - public_basket="yes" - out += """\n""" - if (public_basket=="yes"): - url_public_basket = """%s/yourbaskets.py/display_public?id_basket=%s""" \ - % (weburl, id_basket) - out += """""" \ - % (url_public_basket, url_public_basket) - - # is basket related to some alerts? - alert_query_result = run_sql("SELECT alert_name FROM user_query_basket WHERE id_user=%s AND id_basket=%s", - (uid, id_basket)) - out += """
""" - - # buttons for actions: display basket content, delete, rename, create a new basket - # basket delete - out += """
The selected basket is %s. """ % basket_name - # basket rename - out += """
Basket access is set to private, convert to public?"""\ - """
Basket access is set to public, convert to private?
"""\ - """
Public URL: %s
""" - if len(alert_query_result) == 0: - out += """There isn't any alert related to this basket.""" - else: - out += """The following alerts are related to this basket: """ - i = 1 - for row in alert_query_result: - if i == 1: - out += """%s""" % row[0] - i+=1 - else: - out += """, %s""" % row[0] - i+=1 - out += """
""" - out += """
""" - - # hidden parameters - out += """""" % basket_name - - out += """
""" - - # display the content of the selected basket - if ((id_basket != '0') and (id_basket != 0)): - if (basket_name == ""): - if (newname != ""): - basket_name = newname - else: - if (newbname != ""): - basket_name = newbname - - out += display_basket_content(uid, id_basket, basket_name, of) - # if is guest user print message of relogin - if isGuestUser(uid): - out += warning_guest_user(type="baskets") - return out - - -# display_basket_content: display the content of the selected basket -# input: the identifier of the basket -# the name of the basket -# output: the basket's content -def display_basket_content(uid, id_basket, basket_name, of): - - out = "" - out_tmp="" - - # search for basket's items - if (id_basket != '0') and (id_basket != 0): - query_result = run_sql("SELECT br.id_record,br.nb_order "\ - "FROM basket_record br "\ - "WHERE br.id_basket=%s "\ - "ORDER BY br.nb_order DESC ", - (id_basket,)) - if len(query_result) > 0: - out += out_tmp - - # display the list of items - out += """
""" - out += """\n
""" - out += """""" - - # display operations on the selected items: delete, copy or move - out += """\n""" - # display the list of items - i = 1 - preid = 0 - preord = 0 - for row in query_result : - if i==1: - out += """"""\ - """"""\ - """""" % (id_basket,row[0],row[1],preid,preord,imagesurl,preabstract) - # display current item - out += """"""\ - """"""\ - """""" % (imagesurl,preabstract) - # hidden parameters - out += """""" % id_basket - out += """
Selected items: """\ - """\n""" - out += """  or  to""" - # query the database for the list of baskets - query_result1 = run_sql("SELECT b.id, b.name "\ - "FROM basket b, user_basket ub "\ - "WHERE ub.id_user=%s AND b.id=ub.id_basket AND b.id<>%s "\ - "ORDER BY b.name ASC ", - (uid,id_basket)) - # display the list of baskets - if len(query_result1) > 0: - out +="""\n""" - out +="""

%s"""\ - """""" % (i,row[0],imagesurl) - else: - # complete display previous item - out += """"""\ - """"""\ - """%s
%s"""\ - """"""\ - """""" % (i,row[0],id_basket,row[0],row[1],preid,preord,imagesurl) - preid = row[0] - preord = row[1] - preabstract = print_record(row[0], of) - i += 1 - # complete display last item - out += """"""\ - """%s
""" - else: - out += """

The basket %s is empty.""" % basket_name - out += out_tmp - - return out - - -# delete_basket: present a form for the confirmation of the delete action -# input: the identifier of the selected basket -# the name of the selected basket -# output: the information about the selected basket and the form for the confirmation of the delete action -def delete_basket(uid, id_basket, basket_name): - - # set variables - out = "" - - # search for related alerts - out += """

""" - out += """\n
""" - out += """""" - query_result = run_sql("SELECT alert_name FROM user_query_basket WHERE id_user=%s AND id_basket=%s", - (uid, id_basket)) - if len(query_result) == 0: - Msg = """There isn't any alert related to this basket.""" - out += """""" % Msg - else: - Msg = """The following alerts are related to this basket: """ - i = 1 - for row in query_result: - if i == 1: - Msg += """%s""" % row[0] - i+=1 - else: - Msg += """, %s""" % row[0] - i+=1 - out += """""" % Msg - out += """"""\ - """""" - - # confirm delete action? yes or no - out += """""" % basket_name - out += """""" % id_basket - out += """""" - out += """
%s
%s
Do you want to remove the related alerts too? 
Delete the basket %s ? """\ - """ """ - # hidden parameters - out += """
""" - - return out - - -# perform_delete: present a form for the confirmation of the delete action -# input: delete_alerts='n' if releted alerts shouldn't be deleted; 'y' if yes -# action='YES' if delete action has been confirmed; 'NO' otherwise -# id_basket contains the identifier of the selected basket -# output: go back to the display baskets form with confirmation message -def perform_delete(uid, delete_alerts, confirm_action, id_basket,): - - # set variables - out = "" - - if (confirm_action=='CONFIRM'): - #check that the user which is changing the basket name is the owner of it - if not is_basket_owner( uid, id_basket ): - raise NotBasketOwner("You are not the owner of this basket") - # perform the cancellation - msg = "The selected basket has been deleted." - - if (delete_alerts=='y'): - # delete the related alerts, remove from the alerts table: user_query_basket - query_result = run_sql("DELETE FROM user_query_basket WHERE id_user=%s AND id_basket=%s", - (uid, id_basket)) - msg += " The related alerts have been removed." - else: - # replace the basket identifier with 0 - # select the records to update - query_result = run_sql("SELECT id_query,alert_name,frequency,notification,date_creation,date_lastrun "\ - "FROM user_query_basket WHERE id_user=%s AND id_basket=%s", - (uid, id_basket)) - # update the records - for row in query_result: - query_result_temp = run_sql("UPDATE user_query_basket "\ - "SET alert_name=%s,frequency=%s,notification=%s,"\ - "date_creation=%s,date_lastrun=%s,id_basket='0' "\ - "WHERE id_user=%s AND id_query=%s AND id_basket=%s", - (row[1],row[2],row[3],row[4],row[5],uid,row[0],id_basket)) - - # delete the relation with the user table - query_result = run_sql("DELETE FROM user_basket WHERE id_user=%s AND id_basket=%s", (uid, id_basket)) - # delete the basket information - query_result = run_sql("DELETE FROM basket WHERE id=%s", (id_basket,)) - # delete the basket content - query_result = run_sql("DELETE FROM basket_record WHERE id_basket=%s", (id_basket,)) - - else: - msg="" - - return msg - -# perform_rename_basket: rename an existing basket -# input: basket identifier, basket new name -# output: basket identifier -def perform_rename_basket(uid, id_basket, newname): - # check that there's no basket owned by this user with the same name - if has_user_basket( uid, newname): - raise BasketNameAlreadyExists("You already have a basket which name is '%s'"%newname) - #check that the user which is changing the basket name is the owner of it - if not is_basket_owner( uid, id_basket ): - raise NotBasketOwner("You are not the owner of this basket") - # update a row to the basket table - tmp = run_sql("UPDATE basket SET name=%s WHERE id=%s", (newname, id_basket)) - - return id_basket - -class BasketException(Exception): - """base exception class for basket related errors - """ - pass - -class BasketNameAlreadyExists(BasketException): - """exception which is raised when a basket already exists with a certain name for a user - """ - pass - -class NotBasketOwner(BasketException): - """exception which is raised when a user which is not the owner of a basket tries - to perform an operation over it for which he has no privileges - """ - pass - -def has_user_basket(uid, basket_name): - """checks if a user (uid) already has a basket which name is 'basket_name' (case-sensitive) - """ - return run_sql("select b.id from basket b, user_basket ub where ub.id_user=%s and b.id=ub.id_basket and b.name=%s", - (uid, basket_name.strip())) - -def is_basket_owner(uid, bid): - """checks whether or not the user (uid) is owner for the indicated basket (bid) - """ - return run_sql("select id_basket from user_basket where id_user=%s and id_basket=%s", - (uid, bid)) - - -def get_basket_name(bid): - """returns the name of the basket corresponding to the given id - """ - res = run_sql("select name from basket where id=%s", (bid,)) - if not res: - return "" - return res[0][0] - - -# perform_create_basket: create a new basket and the relation with the user table -# input: basket name -# output: basket identifier -def perform_create_basket(uid, basket_name): - # check that there's no basket owned by this user with the same name - if has_user_basket(uid, basket_name): - raise BasketNameAlreadyExists("You already have a basket which name is '%s'"%basket_name) - # add a row to the basket table - id_basket = run_sql("INSERT INTO basket(id,name,public) VALUES ('0',%s,'n')", (basket_name,)) - - # create the relation between the user and the basket: user_basket - query_result = run_sql("INSERT INTO user_basket(id_user,id_basket,date_modification) VALUES (%s,%s,%s)", - (uid, id_basket, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) - return id_basket - - -# basket_exists checks if a basket is in the database -# input: the name of the basket -# output: the id of the basket if it exists, 0 otherwise -def basket_exists (basket_name, uid): - id_basket = run_sql("SELECT b.id FROM basket b, user_basket ub "\ - "WHERE b.name=%s "\ - "AND b.id=ub.id_basket "\ - "AND ub.id_user=%s", - (basket_name, uid)) - return id_basket - -# set_permission: set access permission on a basket -# input: basket identifier, basket public permission -# output: basket identifier -def set_permission(uid, id_basket, permission): - #check that the user which is changing the basket name is the owner of it - if not is_basket_owner( uid, id_basket ): - raise NotBasketOwner("You are not the owner of this basket") - # update a row to the basket table - id_basket = run_sql("UPDATE basket SET public=%s WHERE id=%s", (permission, id_basket)) - - return id_basket - -# remove_items: remove the selected items from the basket -# input: basket identifier, list of selected items -# output: basket identifier -def remove_items(uid, id_basket, mark): - #check that the user which is changing the basket name is the owner of it - if not is_basket_owner( uid, id_basket ): - raise NotBasketOwner("You are not the owner of this basket") - if type(mark)==list: - selected_items=mark - else: - selected_items=[mark] - for i in selected_items: - # delete the basket content - query_result = run_sql("DELETE FROM basket_record WHERE id_basket=%s AND id_record=%s", - (id_basket, i)) - - return id_basket - -# check_copy: check if the record exists already in the basket -# input: basket identifier, list of selected items -# output: boolean -def check_copy(idbask,i): - - query_result = run_sql("select * from basket_record where id_basket=%s and id_record=%s", - (idbask,i)) - if len(query_result)>0 : - return 0 - return 1 - -# copy/move the selected items to another basket -# input: original basket identifier, list of selected items, -# destination basket identifier, copy or move option: "1"=copy, "2"=move -#output: basket identifier -def move_items(uid, id_basket, mark, to_basket, copy_move="1"): - if type(mark)==list: - selected_items=mark - else: - selected_items=[mark] - for i in selected_items: - if check_copy(to_basket,i): - query_result = run_sql("INSERT INTO basket_record(id_basket,id_record,nb_order) VALUES (%s,%s,'0')", - (to_basket, i)) - - if copy_move=="2": - #delete from previous basket - remove_items(uid, id_basket, mark) - - return id_basket - -# change the order of the items in the basket -# input: basket identifier -# identifiers and positions of the items to be moved -#output: basket identifier -def order_items(uid, id_basket,idup,ordup,iddown,orddown): - #check that the user which is changing the basket name is the owner of it - if not is_basket_owner( uid, id_basket ): - raise NotBasketOwner("You are not the owner of this basket") - # move up the item idup (by switching its order number with the other item): - query_result = run_sql("UPDATE basket_record SET nb_order=%s WHERE id_basket=%s AND id_record=%s", - (orddown,id_basket,idup)) - - # move down the item iddown (by switching its order number with the other item): - query_result = run_sql("UPDATE basket_record SET nb_order=%s WHERE id_basket=%s AND id_record=%s", - (ordup,id_basket,iddown)) - - return id_basket - - -# perform_display_public: display the content of the selected basket, if public -# input: the identifier of the basket -# the name of the basket -# of is the output format code -# output: the basket's content -def perform_display_public(uid, id_basket, basket_name, action, to_basket, mark, newname, of): - out = "" - if action=="EXECUTE": - # perform actions - if newname != "": - # create a new basket - to_basket = perform_create_basket(uid, newname) - out += """The private basket %s has been created.
\n""" % newname - # copy the selected items - if to_basket == '0': - out += """Select a destination basket to copy the selected items.
""" - else: - move_items(uid, id_basket, mark, to_basket, '1') - out += """The selected items have been copied.
""" - - # search for basket's items - if (id_basket != '0') and (id_basket != 0): - res = run_sql("select public from basket where id=%s", (id_basket,)) - if len(res) == 0: - out += """Non existing basket""" - return out - if str(res[0][0]).strip() != 'y': - out += """The basket is private""" - return out - query_result = run_sql("SELECT br.id_record,br.nb_order "\ - "FROM basket_record br "\ - "WHERE br.id_basket=%s "\ - "ORDER BY br.nb_order DESC ", - (id_basket,)) - if len(query_result) > 0: - out += """Content of the public basket %s :
""" % basket_name - - # display the list of items - out += """
""" - out += """\n
""" - out += """""" - - - # copy selected items to basket - query_result1 = run_sql("SELECT b.id, b.name "\ - "FROM basket b, user_basket ub "\ - "WHERE ub.id_user=%s AND b.id=ub.id_basket "\ - "ORDER BY b.name ASC ", - (uid,)) - if len(query_result1) > 0: - out += """Copy the selected items to """ - out += """\n""" - out += """ or new """ - - else: - out += """Copy the selected items to new basket """ - - out += """  """ - out += """

\n""" - - # display the list of items - i = 1 - preid = 0 - preord = 0 - for row in query_result : - if i==1: - out += """"""\ - """"""\ - """""" % (id_basket,row[0],row[1],preid,preord,imagesurl,preabstract) - # display current item - out += """"""\ - """"""\ - """""" % (imagesurl,preabstract) - - # hidden parameters - out += """""" % id_basket - out += """""" % basket_name - out += """
%s"""\ - """""" % (i,row[0],imagesurl) - else: - # complete display previous item - out += """"""\ - """"""\ - """%s
%s"""\ - """"""\ - """""" % (i,row[0],id_basket,row[0],row[1],preid,preord,imagesurl) - preid = row[0] - preord = row[1] - preabstract = print_record(row[0], of) - i += 1 - # complete display last item - out += """"""\ - """%s
""" - else: - out += """The basket %s is empty.""" % basket_name - - return out - -## --- new stuff starts here --- - -def perform_request_add(uid=-1, recid=[], bid=[], bname=[]): - """Add records recid to baskets bid for user uid. If bid isn't set, it'll ask user into which baskets to add them. - If bname is set, it'll create new basket with this name, and add records there rather than to bid.""" - out = "" - # wash arguments: - recIDs = recid - bskIDs = bid - if not type(recid) is list: - recIDs = [recid] - if not type(bid) is list: - bskIDs = [bid] - # sanity checking: - if recIDs == []: - return "

No records to add." - # do we have to create some baskets? - if bname: - try: - new_basket_ID = perform_create_basket(uid, bname) - bskIDs = [new_basket_ID] - except BasketException, e: - out += """The basket %s has not been created: %s""" % (bname, e) - if bskIDs == []: - # A - some information missing, so propose list of baskets to choose from - basket_id_name_list = get_list_of_user_baskets(uid) - if basket_id_name_list != []: - # there are some baskets; good - out += "

Please choose the basket you want to add %d records to:" % len(recIDs) - out += """

""" % weburl - for recID in recIDs: - out += """""" % recID - out += """""" - out += """""" - out += """
""" - else: - # user have to create a basket first - out += """

You don't own baskets defined yet.""" - out += """

""" % weburl - for recID in recIDs: - out += """""" % recID - out += """New basket name: """ - out += """""" - out += """""" - out += """
""" - else: - # B - we have baskets IDs, so we can add records - out += """

Adding %s records to basket(s)...""" % len(recIDs) - for bskID in bskIDs: - if is_basket_owner(uid, bskID): - for recID in recIDs: - try: - res = run_sql("INSERT INTO basket_record(id_basket,id_record,nb_order) VALUES (%s,%s,%s)", - (bskID,recID,'0')) - except: - pass # maybe records were already there? page reload happened? - out += """done.""" - else: - out += """sorry, you are not the owner of this basket.""" - out += perform_display(uid=uid, id_basket=bskIDs[0]) - return out - -def get_list_of_user_baskets(uid): - """Return list of lists [[basket_id, basket_name],[basket_id, basket_name],...] for the given user.""" - out = [] - res = run_sql("SELECT b.id, b.name "\ - "FROM basket b, user_basket ub "\ - "WHERE ub.id_user=%s AND b.id=ub.id_basket "\ - "ORDER BY b.name ASC ", - (uid,)) - for row in res: - out.append([row[0], row[1]]) - return out - -def account_list_baskets(uid, action="", id_basket=0, newname=""): - - out = "" - # query the database for the list of baskets - query_result = run_sql("SELECT b.id, b.name, b.public, ub.date_modification "\ - "FROM basket b, user_basket ub "\ - "WHERE ub.id_user=%s AND b.id=ub.id_basket "\ - "ORDER BY b.name ASC ", - (uid,)) - - out += """

""" - out += """You own the following baskets: """ - out += """\n""" - - # buttons for basket's selection or creation - out += """ """\ - """\n""" - out += """  or """\ - """ """\ - """

""" - - out += """
""" - return out