diff --git a/modules/miscutil/sql/tabcreate.sql b/modules/miscutil/sql/tabcreate.sql index a2514748de..4dad05ed64 100644 --- a/modules/miscutil/sql/tabcreate.sql +++ b/modules/miscutil/sql/tabcreate.sql @@ -3754,6 +3754,13 @@ CREATE TABLE IF NOT EXISTS user_query_basket ( KEY alert_name (alert_name) ) ENGINE=MyISAM; +CREATE TABLE IF NOT EXISTS ATLASPublDraft_alert ( + report_number varchar(40) NOT NULL, + user_email varchar(255) NOT NULL, + date_added datetime NOT NULL default '0000-00-00 00:00:00', + PRIMARY KEY (report_number, user_email) +) ENGINE=MyISAM; + -- baskets CREATE TABLE IF NOT EXISTS bskBASKET ( id int(15) unsigned NOT NULL auto_increment, diff --git a/modules/miscutil/sql/tabdrop.sql b/modules/miscutil/sql/tabdrop.sql index e5897f5e67..39a36c4e87 100644 --- a/modules/miscutil/sql/tabdrop.sql +++ b/modules/miscutil/sql/tabdrop.sql @@ -449,6 +449,7 @@ DROP TABLE IF EXISTS basket; DROP TABLE IF EXISTS basket_record; DROP TABLE IF EXISTS record; DROP TABLE IF EXISTS user_query_basket; +DROP TABLE IF EXISTS ATLASPublDraft_alert; DROP TABLE IF EXISTS cmtRECORDCOMMENT; DROP TABLE IF EXISTS cmtRECORDCOMMENT_bibdoc; DROP TABLE IF EXISTS cmtCOLLAPSED; diff --git a/modules/webalert/lib/webalert.py b/modules/webalert/lib/webalert.py index 5b44943144..4f91570443 100644 --- a/modules/webalert/lib/webalert.py +++ b/modules/webalert/lib/webalert.py @@ -630,3 +630,67 @@ def count_user_alerts_for_given_query(id_user, result = run_sql(query, params) return result[0][0] + + +def APD_alert(report_number, user_email, subscribe=True): + """ + Register or deregister an alert in the database for ATLAS Publication Draft + """ + + success = True + + # The alert shoud be register for the root report number + # ATLAS-HIGG-2016-05 insted of ATLAS-HIGG-2016-05-002 + try: + report_number = '-'.join(report_number.split('-')[:-1]) + except Error: + success = False + + if subscribe: + try: + run_sql("INSERT INTO ATLASPublDraft_alert (report_number, user_email, date_added) VALUES (%s, %s, NOW())", (report_number, user_email)) + except Error: + success = False + else: + try: + res = run_sql("DELETE FROM ATLASPublDraft_alert WHERE report_number=%s AND user_email=%s", (report_number, user_email)) + except Error: + success = False + + return success + + +def APD_is_alert_set(report_number, user_email): + """ + Check if an alert is set for a particulart report number and user email. + """ + # The alert is registered for the root report number + # ATLAS-HIGG-2016-05 insted of ATLAS-HIGG-2016-05-002 + try: + report_number = '-'.join(report_number.split('-')[:-1]) + except Error: + return False + + try: + run_sql("SELECT report_number FROM ATLASPublDraft_alert WHERE report_number=%s and user_email=%s", (report_number, user_email))[0][0] + except IndexError: + return False + + return True + + +def APD_get_subscribed_emails(report_number): + """ + Return the list of emails subscribed for a particular report number + """ + + # The alert is registered for the root report number + # ATLAS-HIGG-2016-05 insted of ATLAS-HIGG-2016-05-002 + try: + report_number = '-'.join(report_number.split('-')[:-1]) + except Error: + return None + + res = run_sql("SELECT user_email FROM ATLASPublDraft_alert WHERE report_number=%s", (report_number,)) + + return [item[0] for item in res] diff --git a/modules/webalert/lib/webalert_webinterface.py b/modules/webalert/lib/webalert_webinterface.py index 20edb2279f..6ed77d7e64 100644 --- a/modules/webalert/lib/webalert_webinterface.py +++ b/modules/webalert/lib/webalert_webinterface.py @@ -21,6 +21,8 @@ __lastupdated__ = """$Date$""" +import re + from invenio.config import CFG_SITE_SECURE_URL, CFG_SITE_NAME, \ CFG_ACCESS_CONTROL_LEVEL_SITE, CFG_SITE_NAME_INTL, CFG_SITE_LANG from invenio.webpage import page @@ -32,13 +34,17 @@ perform_pause_alert, \ perform_resume_alert, \ perform_request_youralerts_popular, \ - AlertError + AlertError, \ + APD_alert + from invenio.webuser import getUid, page_not_authorized, isGuestUser from invenio.webinterface_handler import wash_urlargd, WebInterfaceDirectory from invenio.urlutils import redirect_to_url, make_canonical_urlargd from invenio.webstat import register_customevent from invenio.errorlib import register_exception from invenio.webuser import collect_user_info +from invenio.search_engine import perform_request_search, \ + check_user_can_view_record from invenio.messages import gettext_set_language import invenio.template @@ -57,7 +63,8 @@ class WebInterfaceYourAlertsPages(WebInterfaceDirectory): 'remove', 'pause', 'resume', - 'popular'] + 'popular', + 'APD'] def index(self, req, dummy): """Index page.""" @@ -778,3 +785,72 @@ def popular(self, req, form): lastupdated=__lastupdated__, navmenuid='youralerts', secure_page_p=1) + + def APD(self, req, form): + """ + Handler for subscribing/unsubscribing from ATLAS Publication Drafts + alerts. + """ + + argd = wash_urlargd(form, {'RN': (str, None), + 'subscribe': (int, 1), + 'ln': (str, CFG_SITE_LANG), + } + ) + + uid = getUid(req) + RN = argd['RN'] + subscribe = argd['subscribe'] + + if CFG_ACCESS_CONTROL_LEVEL_SITE >= 1: + return page_not_authorized(req, "%s/youralerts/APD" % + (CFG_SITE_SECURE_URL,), + navmenuid="youralerts") + + elif uid == -1 or isGuestUser(uid): + return redirect_to_url(req, "%s/youraccount/login%s" % + (CFG_SITE_SECURE_URL, + make_canonical_urlargd( + {'referer': "%s/youralerts/APD%s" % ( + CFG_SITE_SECURE_URL, + make_canonical_urlargd(argd, {})), + 'ln': argd['ln']}, + {}) + ) + ) + + user_info = collect_user_info(req) + + # check that the report number followes a specific pattern + # (that we are dealing with an ATLAS Publication Draft) + ATLASPublDraft_pattern = re.compile(r'^ATLAS-[A-Z]+-\d{4}-\d+-\d{3}$') + if not ATLASPublDraft_pattern.match(RN): + return page_not_authorized(req, "../", + text="You are not authorized to set up alerts for this report number.") + + # find the recid associated + try: + recid = perform_request_search(p='"{0}"'.format(RN), f='reportnumber', c=['ATLAS Publication Drafts'])[0] + except IndexError: + return page_not_authorized(req, "../", + text="You are not authorized to set up alerts for this report number.") + + # check that user can access the record + (auth_code, dummy) = check_user_can_view_record(user_info, recid) + if auth_code != 0: + return page_not_authorized(req, "../", + text="You are not authorized to set up alerts for this record.") + + success = False + + if user_info['email']: + if subscribe: + success = APD_alert(RN, user_info['email'], True) + else: + success = APD_alert(RN, user_info['email'], False) + + if not success: + return page_not_authorized(req, "../", + text="You are not authorized to subscribe or unsubscribe for alerts for this record.") + + return redirect_to_url(req, "{0}/record/{1}".format(CFG_SITE_SECURE_URL, recid))