def JOBSUBMIT_Send_Approval_Request (parameters, curdir, form, user_info=None):
    """
    This function sends an email to the referee in order to start the
    simple approval process.  This function is very CERN-specific and
    should be changed in case of external use.  Must be called after
    the Get_Report_Number function.

    Parameters:

       * addressesDAM: email addresses of the people who will receive
                       this email (comma separated list). this
                       parameter may contain the <CATEG> string. In
                       which case the variable computed from the
                       [categformatDAM] parameter replaces this
                       string.
                       eg.:"<CATEG>[email protected]"

       * categformatDAM: contains a regular expression used to compute
                         the category of the document given the
                         reference of the document.

                         eg.: if [categformatAFP]="TEST-<CATEG>-.*"
                         and the reference of the document is
                         "TEST-CATEGORY1-2001-001", then the computed
                         category equals "CATEGORY1"

       * titleFile: name of the file in which the title is stored.

       * contactnamefile: name of the file in which the title is stored.

       * contactemailfile: name of the file in which the title is stored.

       * referencefile: name of the file in which the title is stored.

       * affiliationfile: name of the file in which the title is stored.

       * regionfile: name of the file in which the title is stored.

       * rankfile: name of the file in which the title is stored.

       * fieldfile: name of the file in which the title is stored.

       * experimentsfile: name of the file in which the title is stored.

       * urlfile: name of the file in which the title is stored.

       * datefile: name of the file in which the title is stored.

       * abstractfile: name of the file in which the title is stored.

       * directory: parameter used to create the URL to access the
                    files.
    """
    global rn,sysno
    # variables declaration
    doctype = re.search(".*/([^/]*)/([^/]*)/[^/]*$",curdir).group(2)
    otheraddresses = parameters['addressesDAM']
    categformat = parameters['categformatDAM']
    # retrieve category
    categformat = categformat.replace("<CATEG>","([^-]*)")
    m_categ_search = re.match(categformat, rn)
    if m_categ_search is not None:
        if len(m_categ_search.groups()) > 0:
            ## Found a match for the category of this document. Get it:
            category = m_categ_search.group(1)
        else:
            ## This document has no category.
            category = "unknown"
    else:
        category = "unknown"

    # get record data
    date = get_file_contents(curdir, "date")
    title = get_file_contents(curdir, parameters['titleFile']).replace("\n","")
    title += " - %s" % date
    contactname = get_file_contents(curdir, parameters['contactnamefile']).replace("\n",", ")
    contactemail = get_file_contents(curdir, parameters['contactemailfile']).replace("\n",", ")
    reference = get_file_contents(curdir, parameters['referencefile']).replace("\n",", ")
    affiliation = get_file_contents(curdir, parameters['affiliationfile']).replace("\n",", ")
    region = get_file_contents(curdir, parameters['regionfile']).replace("\n",", ")
    rank = get_file_contents(curdir, parameters['rankfile']).replace("\n",", ")
    field = get_file_contents(curdir, parameters['fieldfile']).replace("\n",", ")
    experiments = get_file_contents(curdir, parameters['experimentsfile']).replace("\n",", ")
    url = get_file_contents(curdir, parameters['urlfile']).replace("\n"," ")
    date = get_file_contents(curdir, parameters['datefile']).replace("\n","")
    abstract = get_file_contents(curdir, parameters['abstractfile'])

    # we get the referee password
    sth = run_sql("SELECT access FROM sbmAPPROVAL WHERE rn=%s", (rn,))
    if len(sth) >0:
        access = sth[0][0]
    # Build referee's email address
    refereeaddress = ""
    # Try to retrieve the referee's email from the referee's database
    for user in acc_get_role_users(acc_get_role_id("referee_%s_%s" % (doctype,category))):
        refereeaddress += user[1] + ","
    # And if there are general referees
    for user in acc_get_role_users(acc_get_role_id("referee_%s_*" % doctype)):
        refereeaddress += user[1] + ","
    refereeaddress = re.sub(",$","",refereeaddress)
    # Creation of the mail for the referee
    addresses = ""
    if refereeaddress != "":
        addresses = refereeaddress + ","
    if otheraddresses != "" and otheraddresses != CFG_WEBSUBMIT_JOBS_SUPPORT_EMAIL:
        addresses += otheraddresses
    else:
        addresses = re.sub(",$","",addresses)
    record_url = "%s/%s/%s" % (CFG_SITE_URL, CFG_SITE_RECORD, sysno)
    title_referee = "Request for approval of %s" % rn
    mail_referee = """
The document %(rn)s has been submitted to the Jobs database.\nYour approval is requested on it.

Title: %(title)s
Contact name(s): %(contactname)s
Contact email(s): %(contactemail)s
Reference(s): %(reference)s
Affliliation(s): %(affiliation)s

Region(s): %(region)s
Rank(s): %(rank)s
Field(s): %(field)s
Experiments(s): %(experiments)s

URL: %(url)s

Deadline date: %(date)s

Description:
%(abstract)s

The record will appear here:
%(recordlink)s

To approve/reject the document, you should go to this URL:\n%(access)s\n
    """ % {'rn' : rn,
           'title' : title,
           'contactname' : contactname,
           'contactemail' : contactemail,
           'reference' : reference,
           'affiliation' : affiliation,
           'region' : region,
           'rank' : rank,
           'region' : region,
           'field' : field,
           'experiments' : experiments,
           'url' : url,
           'date' : date,
           'abstract' : abstract,
           'access' : "%s/approve.py?access=%s" % (CFG_SITE_URL, access),
           'recordlink' : record_url
           }
    #Send mail to referee
    send_email(fromaddr=CFG_WEBSUBMIT_JOBS_FROMADDR, toaddr=CFG_WEBSUBMIT_JOBS_SUPPORT_EMAIL, subject=title_referee, \
               content=mail_referee, footer=email_footer(support_email=CFG_WEBSUBMIT_JOBS_SUPPORT_EMAIL),
               copy_to_admin=CFG_WEBSUBMIT_COPY_MAILS_TO_ADMIN, bccaddr=addresses, replytoaddr=contactemail)
    return ""
def JOBSUBMIT_Send_Approval_Request(parameters, curdir, form, user_info=None):
    """
    This function sends an email to the referee in order to start the
    simple approval process.  This function is very CERN-specific and
    should be changed in case of external use.  Must be called after
    the Get_Report_Number function.

    Parameters:

       * addressesDAM: email addresses of the people who will receive
                       this email (comma separated list). this
                       parameter may contain the <CATEG> string. In
                       which case the variable computed from the
                       [categformatDAM] parameter replaces this
                       string.
                       eg.:"<CATEG>[email protected]"

       * categformatDAM: contains a regular expression used to compute
                         the category of the document given the
                         reference of the document.

                         eg.: if [categformatAFP]="TEST-<CATEG>-.*"
                         and the reference of the document is
                         "TEST-CATEGORY1-2001-001", then the computed
                         category equals "CATEGORY1"

       * titleFile: name of the file in which the title is stored.

       * contactnamefile: name of the file in which the title is stored.

       * contactemailfile: name of the file in which the title is stored.

       * referencefile: name of the file in which the title is stored.

       * affiliationfile: name of the file in which the title is stored.

       * regionfile: name of the file in which the title is stored.

       * rankfile: name of the file in which the title is stored.

       * fieldfile: name of the file in which the title is stored.

       * experimentsfile: name of the file in which the title is stored.

       * urlfile: name of the file in which the title is stored.

       * datefile: name of the file in which the title is stored.

       * abstractfile: name of the file in which the title is stored.

       * directory: parameter used to create the URL to access the
                    files.
    """
    global rn, sysno
    # variables declaration
    doctype = re.search(".*/([^/]*)/([^/]*)/[^/]*$", curdir).group(2)
    otheraddresses = parameters['addressesDAM']
    categformat = parameters['categformatDAM']
    # retrieve category
    categformat = categformat.replace("<CATEG>", "([^-]*)")
    m_categ_search = re.match(categformat, rn)
    if m_categ_search is not None:
        if len(m_categ_search.groups()) > 0:
            ## Found a match for the category of this document. Get it:
            category = m_categ_search.group(1)
        else:
            ## This document has no category.
            category = "unknown"
    else:
        category = "unknown"

    # get record data
    date = get_file_contents(curdir, "date")
    title = get_file_contents(curdir,
                              parameters['titleFile']).replace("\n", "")
    title += " - %s" % date
    contactname = get_file_contents(curdir,
                                    parameters['contactnamefile']).replace(
                                        "\n", ", ")
    contactemail = get_file_contents(curdir,
                                     parameters['contactemailfile']).replace(
                                         "\n", ", ")
    reference = get_file_contents(curdir, parameters['referencefile']).replace(
        "\n", ", ")
    affiliation = get_file_contents(curdir,
                                    parameters['affiliationfile']).replace(
                                        "\n", ", ")
    region = get_file_contents(curdir,
                               parameters['regionfile']).replace("\n", ", ")
    rank = get_file_contents(curdir,
                             parameters['rankfile']).replace("\n", ", ")
    field = get_file_contents(curdir,
                              parameters['fieldfile']).replace("\n", ", ")
    experiments = get_file_contents(curdir,
                                    parameters['experimentsfile']).replace(
                                        "\n", ", ")
    url = get_file_contents(curdir, parameters['urlfile']).replace("\n", " ")
    date = get_file_contents(curdir, parameters['datefile']).replace("\n", "")
    abstract = get_file_contents(curdir, parameters['abstractfile'])

    # we get the referee password
    sth = run_sql("SELECT access FROM sbmAPPROVAL WHERE rn=%s", (rn, ))
    if len(sth) > 0:
        access = sth[0][0]
    # Build referee's email address
    refereeaddress = ""
    # Try to retrieve the referee's email from the referee's database
    for user in acc_get_role_users(
            acc_get_role_id("referee_%s_%s" % (doctype, category))):
        refereeaddress += user[1] + ","
    # And if there are general referees
    for user in acc_get_role_users(acc_get_role_id("referee_%s_*" % doctype)):
        refereeaddress += user[1] + ","
    refereeaddress = re.sub(",$", "", refereeaddress)
    # Creation of the mail for the referee
    addresses = ""
    if refereeaddress != "":
        addresses = refereeaddress + ","
    if otheraddresses != "" and otheraddresses != CFG_WEBSUBMIT_JOBS_SUPPORT_EMAIL:
        addresses += otheraddresses
    else:
        addresses = re.sub(",$", "", addresses)
    record_url = "%s/%s/%s" % (CFG_SITE_URL, CFG_SITE_RECORD, sysno)
    title_referee = "Request for approval of %s" % rn
    mail_referee = """
The document %(rn)s has been submitted to the Jobs database.\nYour approval is requested on it.

Title: %(title)s
Contact name(s): %(contactname)s
Contact email(s): %(contactemail)s
Reference(s): %(reference)s
Affliliation(s): %(affiliation)s

Region(s): %(region)s
Rank(s): %(rank)s
Field(s): %(field)s
Experiments(s): %(experiments)s

URL: %(url)s

Deadline date: %(date)s

Description:
%(abstract)s

The record will appear here:
%(recordlink)s

To approve/reject the document, you should go to this URL:\n%(access)s\n
    """ % {
        'rn': rn,
        'title': title,
        'contactname': contactname,
        'contactemail': contactemail,
        'reference': reference,
        'affiliation': affiliation,
        'region': region,
        'rank': rank,
        'region': region,
        'field': field,
        'experiments': experiments,
        'url': url,
        'date': date,
        'abstract': abstract,
        'access': "%s/approve.py?access=%s" % (CFG_SITE_URL, access),
        'recordlink': record_url
    }
    #Send mail to referee
    send_email(fromaddr=CFG_WEBSUBMIT_JOBS_FROMADDR, toaddr=CFG_WEBSUBMIT_JOBS_SUPPORT_EMAIL, subject=title_referee, \
               content=mail_referee, footer=email_footer(support_email=CFG_WEBSUBMIT_JOBS_SUPPORT_EMAIL),
               copy_to_admin=CFG_WEBSUBMIT_COPY_MAILS_TO_ADMIN, bccaddr=addresses, replytoaddr=contactemail)
    return ""
def JOBSUBMIT_Send_APP_Mail(parameters, curdir, form, user_info=None):
    """
    This function send an email informing the original submitter of a
    document that the referee has approved/ rejected the document.

    Parameters:

       * addressesAPP: email addresses of the people who will receive
         this email (comma separated list). this parameter may contain
         the <CATEG> string. In which case the variable computed from
         the [categformatAFP] parameter replaces this string.
         eg.: "<CATEG>[email protected]"

       * categformatAPP contains a regular expression used to compute
         the category of the document given the reference of the
         document.
         eg.: if [categformatAFP]="TEST-<CATEG>-.*" and the reference
         of the document is "TEST-CATEGORY1-2001-001", then the computed
         category equals "CATEGORY1"

       * emailFile: Name of the file containing the email of the
                    submitter of the document

       * newrnin: Name of the file containing the 2nd reference of the
                  document (if any).

       * decision_file: Name of the file containing the decision of the
                document.

       * comments_file: Name of the file containing the comments of the
                document.

       * edsrn: Name of the file containing the reference of the
                document.
    """
    global titlevalue,authorvalue,sysno,rn
    doctype = form['doctype']
    titlevalue = titlevalue.replace("\n"," ")
    authorvalue = authorvalue.replace("\n","; ")
    # variables declaration
    categformat = parameters['categformatAPP']
    otheraddresses = parameters['addressesAPP']
    newrnpath = parameters['newrnin']
    ## Get the name of the decision file:
    try:
        decision_filename = parameters['decision_file']
    except KeyError:
        decision_filename = ""
    ## Get the name of the comments file:
    try:
        comments_filename = parameters['comments_file']
    except KeyError:
        comments_filename = ""

    ## Now try to read the comments from the comments_filename:
    if comments_filename in (None, "", "NULL"):
        ## We don't have a name for the comments file.
        ## For backward compatibility reasons, try to read the comments from
        ## a file called 'COM' in curdir:
        if os.path.exists("%s/COM" % curdir):
            try:
                fh_comments = open("%s/COM" % curdir)
                comment = fh_comments.read()
                fh_comments.close()
            except IOError:
                ## Unable to open the comments file
                exception_prefix = "Error in WebSubmit function " \
                                   "Send_APP_Mail. Tried to open " \
                                   "comments file [%s/COM] but was " \
                                   "unable to." % curdir
                register_exception(prefix=exception_prefix)
                comment = ""
            else:
                comment = comment.strip()
        else:
            comment = ""
    else:
        ## Try to read the comments from the comments file:
        if os.path.exists("%s/%s" % (curdir, comments_filename)):
            try:
                fh_comments = open("%s/%s" % (curdir, comments_filename))
                comment = fh_comments.read()
                fh_comments.close()
            except IOError:
                ## Oops, unable to open the comments file.
                comment = ""
                exception_prefix = "Error in WebSubmit function " \
                                "Send_APP_Mail. Tried to open comments " \
                                "file [%s/%s] but was unable to." \
                                % (curdir, comments_filename)
                register_exception(prefix=exception_prefix)
            else:
                comment = comment.strip()
        else:
            comment = ""

    ## Now try to read the decision from the decision_filename:
    if decision_filename in (None, "", "NULL"):
        ## We don't have a name for the decision file.
        ## For backward compatibility reasons, try to read the decision from
        ## a file called 'decision' in curdir:
        if os.path.exists("%s/decision" % curdir):
            try:
                fh_decision = open("%s/decision" % curdir)
                decision = fh_decision.read()
                fh_decision.close()
            except IOError:
                ## Unable to open the decision file
                exception_prefix = "Error in WebSubmit function " \
                                   "Send_APP_Mail. Tried to open " \
                                   "decision file [%s/decision] but was " \
                                   "unable to." % curdir
                register_exception(prefix=exception_prefix)
                decision = ""
            else:
                decision = decision.strip()
        else:
            decision = ""
    else:
        ## Try to read the decision from the decision file:
        try:
            fh_decision = open("%s/%s" % (curdir, decision_filename))
            decision = fh_decision.read()
            fh_decision.close()
        except IOError:
            ## Oops, unable to open the decision file.
            decision = ""
            exception_prefix = "Error in WebSubmit function " \
                               "Send_APP_Mail. Tried to open decision " \
                               "file [%s/%s] but was unable to." \
                               % (curdir, decision_filename)
            register_exception(prefix=exception_prefix)
        else:
            decision = decision.strip()

    if os.path.exists("%s/%s" % (curdir,newrnpath)):
        fp = open("%s/%s" % (curdir,newrnpath))
        newrn = fp.read()
        fp.close()
    else:
        newrn = ""
    # Document name
    res = run_sql("SELECT ldocname FROM sbmDOCTYPE WHERE sdocname=%s", (doctype,))
    docname = res[0][0]
    # retrieve category
    categformat = categformat.replace("<CATEG>", "([^-]*)")
    m_categ_search = re.match(categformat, rn)
    if m_categ_search is not None:
        if len(m_categ_search.groups()) > 0:
            ## Found a match for the category of this document. Get it:
            category = m_categ_search.group(1)
        else:
            ## This document has no category.
            category = "unknown"
    else:
        category = "unknown"

    # Creation of the mail for the referee
    otheraddresses = otheraddresses.replace("<CATEG>",category)
    addresses = ""
    if otheraddresses != "":
        addresses += otheraddresses
    else:
        addresses = re.sub(",$","",addresses)

    ## Add the record's submitter(s) into the list of recipients:
    # The submitters email address is read from the file specified by 'emailFile'
    try:
        fp = open("%s/%s" % (curdir,parameters['emailFile']))
        addresses += fp.read().replace ("\n"," ")
        fp.close()
    except:
        pass

    if decision == "approve":
        mailtitle = "%s has been approved" % rn
        mailbody = "The submitted job listing with reference number %s has been fully approved." % (rn,)
        mailbody += "\n\nIt will soon become visible in the INSPIRE-HEP Jobs database - <%s/Jobs>" % (CFG_SITE_URL,)
    else:
        mailtitle = "%s has been rejected" % rn
        mailbody = "The %s %s has been rejected." % (docname,rn)
    if rn != newrn and decision == "approve" and newrn != "":
        mailbody += "\n\nIts new reference number is: %s" % newrn
    mailbody += "\n\nTitle: %s\n\nAuthor(s): %s\n\n" % (titlevalue,authorvalue)
    if comment != "":
        mailbody += "Comments from the referee:\n%s\n" % comment
    # Send mail to referee
    send_email(fromaddr=CFG_WEBSUBMIT_JOBS_FROMADDR, toaddr=addresses, subject=mailtitle, \
               content=mailbody, footer=email_footer(support_email=CFG_WEBSUBMIT_JOBS_SUPPORT_EMAIL), copy_to_admin=CFG_WEBSUBMIT_COPY_MAILS_TO_ADMIN)
    return ""