def Withdraw_Approval_Request(parameters, curdir, form, user_info=None):
    """
    This function is used in a "withdraw approval request" submission
    in order to register the withdral of the request in the WebSubmit
    "Approvals" DB (sbmAPPROVAL).
    At the time of the approval request withdrawal, the document could be
    in one of several different approval "states" and depending upon that
    state, the action taken by this function differs. The states are as
    follows:
          * Approval of the document has previously been requested and it is
            still in the "waiting" state.
             -> In this case, the status of the document in the sbmAPPROVAL
                table is set to "withdrawn".
          * Approval for the document has never been requested.
             -> In this case, there is nothing to do.
          * Approval of the document has previously been requested, but the
            document was rejected.
             -> In this case, it's too late to withdraw the approval request
                and there is nothing left to do.
          * Approval of the document has previously been requested and it has
            been approved.
             -> In this case, it's too late to withdraw the approval request
                and there is nothing left to do.
          * Approval of the document has previously been requested, but the
            request withdrawn.
             -> In this case, there is nothing to do.

    @param categ_file_withd: (string) - some document types are
           separated into different categories, each of which has its own
           referee(s).
           In such document types, it's necessary to know the document-
           type's category in order to choose the referee.
           This parameter provides a means by which the category information
           can be extracted from a file in the current submission's working
           directory. It should therefore be a filename.

    @param categ_rnseek_withd: (string) - some document types are
           separated into different categories, each of which has its own
           referee(s).
           In such document types, it's necessary to know the document-
           type's category in order to choose the referee.
           This parameter provides a means by which the category information
           can be extracted from the document's reference number.
           It is infact a string that will be compiled into a regexp and
           an attempt will be made to match it agains the document's reference
           number starting from the left-most position.
           The only pre-requisite is that the segment in which the category is
           sought should be indicated with <CATEGORY>.
           Thus, an example might be as follows:
              ATL(-COM)?-<CATEGORY>-.+
           This would allow "PHYS" in the following reference number to be
           recognised as the category:
              ATL-COM-PHYS-2008-001

    @return: (string) - a message for the user.

    @Exceptions raised: + InvenioWebSubmitFunctionStop when the submission
                          should be halted.
                        + InvenioWebSubmitFunctionError when an unexpected
                          error has been encountered and execution cannot
                          continue.
    """
    ## Get the reference number (as global rn - sorry!) and the document type:
    global rn
    doctype = form['doctype']

    ## A string variable to contain any information that should be displayed
    ## in the user's browser:
    info_out = ""

    ########
    ## Get the parameters from the list:

    ########
    ## Get the name of the category file:
    #######
    try:
        ## If it has been provided, get the name of the file in which the
        ## category is stored:
        category_file = parameters["categ_file_withd"]
    except KeyError:
        ## No value given for the category file:
        category_file = None
    else:
        if category_file is not None:
            category_file = str(category_file)
            category_file = os.path.basename(category_file).strip()
            if category_file == "":
                category_file = None
    ########
    ## Get the regexp that is used to find the category in the report number:
    ########
    try:
        ## If it has been provided, get the regexp used for identifying
        ## a document-type's category from its reference number:
        category_rn_regexp = parameters["categ_rnseek_withd"]
    except KeyError:
        ## No value given for the category regexp:
        category_rn_regexp = None
    else:
        if category_rn_regexp is not None:
            category_rn_regexp = str(category_rn_regexp).strip()
        if category_rn_regexp == "":
            category_rn_regexp = None

    #######
    ## Resolve the document type's category:
    ##
    ## This is a long process. The end result is that the category is extracted
    ## either from a file in curdir, or from the report number.
    ## If it's taken from the report number, the admin must configure the
    ## function to accept a regular expression that is used to find the
    ## category in the report number.
    ##
    if category_file is not None and category_rn_regexp is not None:
        ## It is not valid to have both a category file and a pattern
        ## describing how to extract the category from a report number.
        ## raise an InvenioWebSubmitFunctionError
        msg = "Error in Withdraw_Approval_Request function: received " \
              "instructions to search for the document's category in " \
              "both its report number AND in a category file. Could " \
              "not determine which to use - please notify " \
              "%(suppt-email)s." \
              % { 'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
        raise InvenioWebSubmitFunctionError(msg)
    elif category_file is not None:
        ## Attempt to recover the category information from a file in the
        ## current submission's working directory:
        category = ParamFromFile("%s/%s" % (curdir, category_file))
        if category is not None:
            category = category.strip()
        if category in (None, ""):
            ## The category cannot be resolved.
            msg = "Error in Withdraw_Approval_Request function: received " \
                  "instructions to search for the document's category in " \
                  "a category file, but could not recover the category " \
                  "from that file. The approval request therefore cannot " \
                  "be withdrawn for the document. Please report this " \
                  "problem to %(suppt-email)s." \
                  % { 'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
            raise InvenioWebSubmitFunctionError(msg)
    elif category_rn_regexp is not None:
        ## Attempt to recover the category information from the document's
        ## reference number using the regexp in category_rn_regexp:
        ##
        ## Does the category regexp contain the key-phrase "<CATEG>"?
        if category_rn_regexp.find("<CATEG>") != -1:
            ## Yes. Replace "<CATEG>" with "(?P<category>.+?)".
            ## For example, this:
            ##    ATL(-COM)?-<CATEG>-
            ## Will be transformed into this:
            ##    ATL(-COM)?-(?P<category>.+?)-
            category_rn_final_regexp = \
                category_rn_regexp.replace("<CATEG>", r"(?P<category>.+?)", 1)
        else:
            ## The regexp for category didn't contain "<CATEG>", but this is
            ## mandatory.
            msg = "Error in Withdraw_Approval_Request function: The " \
                  "[%(doctype)s] submission has been configured to search " \
                  "for the document type's category in its reference number, " \
                  "using a poorly formed search expression (no marker for " \
                  "the category was present.) Since the document's category " \
                  "therefore cannot be retrieved, its approval request " \
                  "cannot be withdrawn. Please report this problem to " \
                  "%(suppt-email)s." \
                  % { 'doctype'     : cgi.escape(doctype),
                      'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
            raise InvenioWebSubmitFunctionError(msg)
        ##
        try:
            ## Attempt to compile the regexp for finding the category:
            re_categ_from_rn = re.compile(category_rn_final_regexp)
        except sre_constants.error:
            ## The expression passed to this function could not be compiled
            ## into a regexp. Register this exception and raise an
            ## InvenioWebSubmitFunctionError:
            exception_prefix = "Error in Withdraw_Approval_Request function: " \
                               "The [%(doctype)s] submission has been " \
                               "configured to search for the document type's " \
                               "category in its reference number, using the " \
                               "following regexp: /%(regexp)s/. This regexp, " \
                               "however, could not be compiled correctly " \
                               "(created it from %(categ-search-term)s.)" \
                               % { 'doctype'       : doctype, \
                                   'regexp'        : category_rn_final_regexp, \
                                   'categ-search-term' : category_rn_regexp, }
            register_exception(prefix=exception_prefix)
            msg = "Error in Withdraw_Approval_Request function: The " \
                  "[%(doctype)s] submission has been configured to search " \
                  "for the document type's category in its reference number, " \
                  "using a poorly formed search expression. Since the " \
                  "document's category therefore cannot be retrieved, its " \
                  "approval request cannot be withdrawn. Please " \
                  "report this problem to %(suppt-email)s." \
                  % { 'doctype'     : cgi.escape(doctype),
                      'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
            raise InvenioWebSubmitFunctionError(msg)
        else:
            ## Now attempt to recover the category from the RN string:
            m_categ_from_rn = re_categ_from_rn.match(rn)
            if m_categ_from_rn is not None:
                ## The pattern matched in the string.
                ## Extract the category from the match:
                try:
                    category = m_categ_from_rn.group("category")
                except IndexError:
                    ## There was no "category" group. That group is mandatory.
                    exception_prefix = \
                       "Error in Withdraw_Approval_Request function: The " \
                       "[%(doctype)s] submission has been configured to " \
                       "search for the document type's category in its " \
                       "reference number using the following regexp: " \
                       "/%(regexp)s/. The search produced a match, but " \
                       "there was no \"category\" group in the match " \
                       "object although this group is mandatory. The " \
                       "regexp was compiled from the following string: " \
                       "[%(categ-search-term)s]." \
                       % { 'doctype'           : doctype, \
                           'regexp'            : category_rn_final_regexp, \
                           'categ-search-term' : category_rn_regexp, }
                    register_exception(prefix=exception_prefix)
                    msg = "Error in Withdraw_Approval_Request function: The " \
                          "[%(doctype)s] submission has been configured to " \
                          "search for the document type's category in its " \
                          "reference number, using a poorly formed search " \
                          "expression (there was no category marker). Since " \
                          "the document's category therefore cannot be " \
                          "retrieved, its approval request cannot be " \
                          "withdrawn. Please report this problem to " \
                          "%(suppt-email)s." \
                          % { 'doctype'     : cgi.escape(doctype),
                              'suppt-email' : \
                                  cgi.escape(CFG_SITE_SUPPORT_EMAIL),}
                    raise InvenioWebSubmitFunctionError(msg)
                else:
                    category = category.strip()
                    if category == "":
                        msg = "Error in Withdraw_Approval_Request function: " \
                              "The [%(doctype)s] submission has been " \
                              "configured to search for the document type's " \
                              "category in its reference number, but no " \
                              "category was found. The request for approval " \
                              "cannot be withdrawn. Please report this " \
                              "problem to %(suppt-email)s." \
                              % { 'doctype'     : cgi.escape(doctype),
                                  'suppt-email' : \
                                     cgi.escape(CFG_SITE_SUPPORT_EMAIL),}
                        raise InvenioWebSubmitFunctionError(msg)
            else:
                ## No match. Cannot find the category and therefore cannot
                ## continue:
                msg = "Error in Withdraw_Approval_Request function: The " \
                      "[%(doctype)s] submission has been configured to " \
                      "search for the document type's category in its " \
                      "reference number, but no match was made. The request " \
                      "for approval cannot be withdrawn. Please report " \
                      "this problem to %(suppt-email)s." \
                      % { 'doctype'     : cgi.escape(doctype),
                          'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL),}
                raise InvenioWebSubmitFunctionError(msg)
    else:
        ## The document type has no category.
        category = ""
    ##
    ## End of category recovery
    #######

    #######
    ##
    ## Query the "approvals" DB table to determine whether approval of this
    ## document has already been requested:
    approval_status = get_simple_approval_status(doctype, rn)
    if approval_status is None:
        ## Approval has never been requested for this document.
        ## One cannot withdraw an approval request that was never made.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> A request for the approval of the
 document [%s] has never been made.<br />
There is nothing to do.
</div>
""" % cgi.escape(rn)
        raise InvenioWebSubmitFunctionStop(msg)
    elif approval_status.lower() == "approved":
        ## This document has already been approved. It's too late to withdraw
        ## the approval request.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The document [%s] has already been
 approved.<br />
It is too late to withdraw the approval request.<br />
If you believe this to be an error, please contact %s, quoting the<br />
document's report-number [%s] and describing the problem.
</div>
""" % (cgi.escape(rn), cgi.escape(CFG_SITE_SUPPORT_EMAIL), cgi.escape(rn))
        raise InvenioWebSubmitFunctionStop(msg)
    elif approval_status.lower() == "rejected":
        ## This document has already been rejected. It's too late to withdraw
        ## the approval request.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The document [%s] has already been
 rejected.<br />
It is too late to withdraw the approval request.<br />
If you believe this to be an error, please contact %s, quoting the<br />
document's report-number [%s] and describing the problem.
</div>
""" % (cgi.escape(rn), cgi.escape(CFG_SITE_SUPPORT_EMAIL), cgi.escape(rn))
        raise InvenioWebSubmitFunctionStop(msg)
    elif approval_status.lower() == "withdrawn":
        ## The approval request for this document has already been withdrawn.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The approval request for the document
 [%s] has already been withdrawn.<br />
There is nothing to do.
</div>
""" % cgi.escape(rn)
        raise InvenioWebSubmitFunctionStop(msg)
    elif approval_status.lower() == "waiting":
        ## Mark the approval request as withdrawn:
        note = "Withdrawn by [%s]: %s\n#####\n" \
               % (cgi.escape(user_info['email']), \
                  cgi.escape(time.strftime("%d/%m/%Y %H:%M:%S", \
                                           time.localtime())))
        update_approval_request_status(doctype, \
                                       rn, \
                                       note=note, \
                                       status="withdrawn")
        info_out += """
<br />
<div>
The approval request for the document [%s] has been withdrawn.
</div>
""" % cgi.escape(rn)
    else:
        ## The document had an unrecognised "status". Raise an error.
        msg = "Error in Withdraw_Approval_Request function: The " \
              "[%(reportnum)s] document has an unknown approval status " \
              "(%(status)s). Unable to withdraw the request for its " \
              "approval. Please report this problem to the %(suppt-email)s." \
              % { 'reportnum'   : cgi.escape(rn),
                  'status'      : cgi.escape(approval_status),
                  'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
        raise InvenioWebSubmitFunctionError(msg)
    ##
    ## Finished - return any message to be displayed on the user's screen.
    return info_out
def Withdraw_Approval_Request(parameters, curdir, form, user_info=None):
    """
    This function is used in a "withdraw approval request" submission
    in order to register the withdral of the request in the WebSubmit
    "Approvals" DB (sbmAPPROVAL).
    At the time of the approval request withdrawal, the document could be
    in one of several different approval "states" and depending upon that
    state, the action taken by this function differs. The states are as
    follows:
          * Approval of the document has previously been requested and it is
            still in the "waiting" state.
             -> In this case, the status of the document in the sbmAPPROVAL
                table is set to "withdrawn".
          * Approval for the document has never been requested.
             -> In this case, there is nothing to do.
          * Approval of the document has previously been requested, but the
            document was rejected.
             -> In this case, it's too late to withdraw the approval request
                and there is nothing left to do.
          * Approval of the document has previously been requested and it has
            been approved.
             -> In this case, it's too late to withdraw the approval request
                and there is nothing left to do.
          * Approval of the document has previously been requested, but the
            request withdrawn.
             -> In this case, there is nothing to do.

    @param categ_file_withd: (string) - some document types are
           separated into different categories, each of which has its own
           referee(s).
           In such document types, it's necessary to know the document-
           type's category in order to choose the referee.
           This parameter provides a means by which the category information
           can be extracted from a file in the current submission's working
           directory. It should therefore be a filename.

    @param categ_rnseek_withd: (string) - some document types are
           separated into different categories, each of which has its own
           referee(s).
           In such document types, it's necessary to know the document-
           type's category in order to choose the referee.
           This parameter provides a means by which the category information
           can be extracted from the document's reference number.
           It is infact a string that will be compiled into a regexp and
           an attempt will be made to match it agains the document's reference
           number starting from the left-most position.
           The only pre-requisite is that the segment in which the category is
           sought should be indicated with <CATEGORY>.
           Thus, an example might be as follows:
              ATL(-COM)?-<CATEGORY>-.+
           This would allow "PHYS" in the following reference number to be
           recognised as the category:
              ATL-COM-PHYS-2008-001

    @return: (string) - a message for the user.

    @Exceptions raised: + InvenioWebSubmitFunctionStop when the submission
                          should be halted.
                        + InvenioWebSubmitFunctionError when an unexpected
                          error has been encountered and execution cannot
                          continue.
    """
    ## Get the reference number (as global rn - sorry!) and the document type:
    global rn
    doctype = form['doctype']

    ## A string variable to contain any information that should be displayed
    ## in the user's browser:
    info_out = ""

    ########
    ## Get the parameters from the list:

    ########
    ## Get the name of the category file:
    #######
    try:
        ## If it has been provided, get the name of the file in which the
        ## category is stored:
        category_file = parameters["categ_file_withd"]
    except KeyError:
        ## No value given for the category file:
        category_file = None
    else:
        if category_file is not None:
            category_file = str(category_file)
            category_file = os.path.basename(category_file).strip()
            if category_file == "":
                category_file = None
    ########
    ## Get the regexp that is used to find the category in the report number:
    ########
    try:
        ## If it has been provided, get the regexp used for identifying
        ## a document-type's category from its reference number:
        category_rn_regexp = parameters["categ_rnseek_withd"]
    except KeyError:
        ## No value given for the category regexp:
        category_rn_regexp = None
    else:
        if category_rn_regexp is not None:
            category_rn_regexp = str(category_rn_regexp).strip()
        if category_rn_regexp == "":
            category_rn_regexp = None

    #######
    ## Resolve the document type's category:
    ##
    ## This is a long process. The end result is that the category is extracted
    ## either from a file in curdir, or from the report number.
    ## If it's taken from the report number, the admin must configure the
    ## function to accept a regular expression that is used to find the
    ## category in the report number.
    ##
    if category_file is not None and category_rn_regexp is not None:
        ## It is not valid to have both a category file and a pattern
        ## describing how to extract the category from a report number.
        ## raise an InvenioWebSubmitFunctionError
        msg = "Error in Withdraw_Approval_Request function: received " \
              "instructions to search for the document's category in " \
              "both its report number AND in a category file. Could " \
              "not determine which to use - please notify " \
              "%(suppt-email)s." \
              % { 'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
        raise InvenioWebSubmitFunctionError(msg)
    elif category_file is not None:
        ## Attempt to recover the category information from a file in the
        ## current submission's working directory:
        category = ParamFromFile("%s/%s" % (curdir, category_file))
        if category is not None:
            category = category.strip()
        if category in (None, ""):
            ## The category cannot be resolved.
            msg = "Error in Withdraw_Approval_Request function: received " \
                  "instructions to search for the document's category in " \
                  "a category file, but could not recover the category " \
                  "from that file. The approval request therefore cannot " \
                  "be withdrawn for the document. Please report this " \
                  "problem to %(suppt-email)s." \
                  % { 'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
            raise InvenioWebSubmitFunctionError(msg)
    elif category_rn_regexp is not None:
        ## Attempt to recover the category information from the document's
        ## reference number using the regexp in category_rn_regexp:
        ##
        ## Does the category regexp contain the key-phrase "<CATEG>"?
        if category_rn_regexp.find("<CATEG>") != -1:
            ## Yes. Replace "<CATEG>" with "(?P<category>.+?)".
            ## For example, this:
            ##    ATL(-COM)?-<CATEG>-
            ## Will be transformed into this:
            ##    ATL(-COM)?-(?P<category>.+?)-
            category_rn_final_regexp = \
                category_rn_regexp.replace("<CATEG>", r"(?P<category>.+?)", 1)
        else:
            ## The regexp for category didn't contain "<CATEG>", but this is
            ## mandatory.
            msg = "Error in Withdraw_Approval_Request function: The " \
                  "[%(doctype)s] submission has been configured to search " \
                  "for the document type's category in its reference number, " \
                  "using a poorly formed search expression (no marker for " \
                  "the category was present.) Since the document's category " \
                  "therefore cannot be retrieved, its approval request " \
                  "cannot be withdrawn. Please report this problem to " \
                  "%(suppt-email)s." \
                  % { 'doctype'     : cgi.escape(doctype),
                      'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
            raise InvenioWebSubmitFunctionError(msg)
        ##
        try:
            ## Attempt to compile the regexp for finding the category:
            re_categ_from_rn = re.compile(category_rn_final_regexp)
        except sre_constants.error:
            ## The expression passed to this function could not be compiled
            ## into a regexp. Register this exception and raise an
            ## InvenioWebSubmitFunctionError:
            exception_prefix = "Error in Withdraw_Approval_Request function: " \
                               "The [%(doctype)s] submission has been " \
                               "configured to search for the document type's " \
                               "category in its reference number, using the " \
                               "following regexp: /%(regexp)s/. This regexp, " \
                               "however, could not be compiled correctly " \
                               "(created it from %(categ-search-term)s.)" \
                               % { 'doctype'       : doctype, \
                                   'regexp'        : category_rn_final_regexp, \
                                   'categ-search-term' : category_rn_regexp, }
            register_exception(prefix=exception_prefix)
            msg = "Error in Withdraw_Approval_Request function: The " \
                  "[%(doctype)s] submission has been configured to search " \
                  "for the document type's category in its reference number, " \
                  "using a poorly formed search expression. Since the " \
                  "document's category therefore cannot be retrieved, its " \
                  "approval request cannot be withdrawn. Please " \
                  "report this problem to %(suppt-email)s." \
                  % { 'doctype'     : cgi.escape(doctype),
                      'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
            raise InvenioWebSubmitFunctionError(msg)
        else:
            ## Now attempt to recover the category from the RN string:
            m_categ_from_rn = re_categ_from_rn.match(rn)
            if m_categ_from_rn is not None:
                ## The pattern matched in the string.
                ## Extract the category from the match:
                try:
                    category = m_categ_from_rn.group("category")
                except IndexError:
                    ## There was no "category" group. That group is mandatory.
                    exception_prefix = \
                       "Error in Withdraw_Approval_Request function: The " \
                       "[%(doctype)s] submission has been configured to " \
                       "search for the document type's category in its " \
                       "reference number using the following regexp: " \
                       "/%(regexp)s/. The search produced a match, but " \
                       "there was no \"category\" group in the match " \
                       "object although this group is mandatory. The " \
                       "regexp was compiled from the following string: " \
                       "[%(categ-search-term)s]." \
                       % { 'doctype'           : doctype, \
                           'regexp'            : category_rn_final_regexp, \
                           'categ-search-term' : category_rn_regexp, }
                    register_exception(prefix=exception_prefix)
                    msg = "Error in Withdraw_Approval_Request function: The " \
                          "[%(doctype)s] submission has been configured to " \
                          "search for the document type's category in its " \
                          "reference number, using a poorly formed search " \
                          "expression (there was no category marker). Since " \
                          "the document's category therefore cannot be " \
                          "retrieved, its approval request cannot be " \
                          "withdrawn. Please report this problem to " \
                          "%(suppt-email)s." \
                          % { 'doctype'     : cgi.escape(doctype),
                              'suppt-email' : \
                                  cgi.escape(CFG_SITE_SUPPORT_EMAIL),}
                    raise InvenioWebSubmitFunctionError(msg)
                else:
                    category = category.strip()
                    if category == "":
                        msg = "Error in Withdraw_Approval_Request function: " \
                              "The [%(doctype)s] submission has been " \
                              "configured to search for the document type's " \
                              "category in its reference number, but no " \
                              "category was found. The request for approval " \
                              "cannot be withdrawn. Please report this " \
                              "problem to %(suppt-email)s." \
                              % { 'doctype'     : cgi.escape(doctype),
                                  'suppt-email' : \
                                     cgi.escape(CFG_SITE_SUPPORT_EMAIL),}
                        raise InvenioWebSubmitFunctionError(msg)
            else:
                ## No match. Cannot find the category and therefore cannot
                ## continue:
                msg = "Error in Withdraw_Approval_Request function: The " \
                      "[%(doctype)s] submission has been configured to " \
                      "search for the document type's category in its " \
                      "reference number, but no match was made. The request " \
                      "for approval cannot be withdrawn. Please report " \
                      "this problem to %(suppt-email)s." \
                      % { 'doctype'     : cgi.escape(doctype),
                          'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL),}
                raise InvenioWebSubmitFunctionError(msg)
    else:
        ## The document type has no category.
        category = ""
    ##
    ## End of category recovery
    #######

    #######
    ##
    ## Query the "approvals" DB table to determine whether approval of this
    ## document has already been requested:
    approval_status = get_simple_approval_status(doctype, rn)
    if approval_status is None:
        ## Approval has never been requested for this document.
        ## One cannot withdraw an approval request that was never made.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> A request for the approval of the
 document [%s] has never been made.<br />
There is nothing to do.
</div>
""" % cgi.escape(rn)
        raise InvenioWebSubmitFunctionStop(msg)
    elif approval_status.lower() == "approved":
        ## This document has already been approved. It's too late to withdraw
        ## the approval request.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The document [%s] has already been
 approved.<br />
It is too late to withdraw the approval request.<br />
If you believe this to be an error, please contact %s, quoting the<br />
document's report-number [%s] and describing the problem.
</div>
""" % (cgi.escape(rn), cgi.escape(CFG_SITE_SUPPORT_EMAIL), cgi.escape(rn))
        raise InvenioWebSubmitFunctionStop(msg)
    elif approval_status.lower() == "rejected":
        ## This document has already been rejected. It's too late to withdraw
        ## the approval request.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The document [%s] has already been
 rejected.<br />
It is too late to withdraw the approval request.<br />
If you believe this to be an error, please contact %s, quoting the<br />
document's report-number [%s] and describing the problem.
</div>
""" % (cgi.escape(rn), cgi.escape(CFG_SITE_SUPPORT_EMAIL), cgi.escape(rn))
        raise InvenioWebSubmitFunctionStop(msg)
    elif approval_status.lower() == "withdrawn":
        ## The approval request for this document has already been withdrawn.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The approval request for the document
 [%s] has already been withdrawn.<br />
There is nothing to do.
</div>
""" % cgi.escape(rn)
        raise InvenioWebSubmitFunctionStop(msg)
    elif approval_status.lower() == "waiting":
        ## Mark the approval request as withdrawn:
        note = "Withdrawn by [%s]: %s\n#####\n" \
               % (cgi.escape(user_info['email']), \
                  cgi.escape(time.strftime("%d/%m/%Y %H:%M:%S", \
                                           time.localtime())))
        update_approval_request_status(doctype, \
                                       rn, \
                                       note=note, \
                                       status="withdrawn")
        info_out += """
<br />
<div>
The approval request for the document [%s] has been withdrawn.
</div>
""" % cgi.escape(rn)
    else:
        ## The document had an unrecognised "status". Raise an error.
        msg = "Error in Withdraw_Approval_Request function: The " \
              "[%(reportnum)s] document has an unknown approval status " \
              "(%(status)s). Unable to withdraw the request for its " \
              "approval. Please report this problem to the %(suppt-email)s." \
              % { 'reportnum'   : cgi.escape(rn),
                  'status'      : cgi.escape(approval_status),
                  'suppt-email' : cgi.escape(CFG_SITE_SUPPORT_EMAIL), }
        raise InvenioWebSubmitFunctionError(msg)
    ##
    ## Finished - return any message to be displayed on the user's screen.
    return info_out
def Register_Referee_Decision(parameters, curdir, form, user_info=None):
    """
    A referee may either "accept" or "reject" a refereed document.
    The referee's decision is stored in a file in the submission's working
    directory and it is this function's job to read the contents of that
    file and update the status of the document's entry in the approvals
    table (sbmAPPROVAL) to be either "accepted" or "rejected" depending
    upon the referee's decision.

    @param decision_file: (string) - the name of the file in which the
        referee's decision is to be found.
        NOTE: A referee's decision _MUST_ be either "accept" or "reject".
              If not, an InvenioWebSubmitFunctionError will be raised.
              If a document's "approval status" is not "waiting" at the
              time of the referee's decision, the decision will not be
              taken into account and the submission will be halted.
              (This is because it's not appropriate to approve a document
              that has already been approved or rejected, has been
              withdrawn, etc.)

    @return: empty string.

    @Exceptions raised: InvenioWebSubmitFunctionError on unexpected error.
                        InvenioWebSubmitFunctionStop in the case where the
                        approval should be stopped for whatever reason.
                        (E.g. when it has already been approved.)
    """
    global rn
    doctype = form['doctype']
    ########
    ## Get the parameters from the list:
    ########
    ## Get the name of the "decision" file and read its value:
    ########
    decision = "" ## variable to hold the referee's decision
    try:
        decision_file = parameters["decision_file"]
    except KeyError:
        ## No value given for the decision file:
        decision_file = None
    else:
        if decision_file is not None:
            decision_file = os.path.basename(decision_file).strip()
            if decision_file == "":
                decision_file = None
    if decision_file is None:
        ## Unable to obtain the name of the file in which the referee's
        ## decision is stored. Halt.
        err_msg = "Error in Register_Referee_Decision: Function was not " \
                  "configured with a valid value for decision_file - the " \
                  "file in which the referee's decision is stored. " \
                  "The referee's decision has not been processed for " \
                  "[%s]. Please inform the administrator." \
                  % rn
        raise InvenioWebSubmitFunctionError(err_msg)
    ## Read in the referee's decision:
    decision = ParamFromFile("%s/%s" % (curdir, decision_file)).lower()
    ##
    ########
    if decision not in ("approve", "reject"):
        ## Invalid value for the referee's decision.
        err_msg = "Error in Register_Referee_Decision: The value for the " \
                  "referee's decision (%s) was invalid. Please inform the " \
                  "administrator." % decision
        raise InvenioWebSubmitFunctionError(err_msg)
    ##
    ## Get the status of the approval request for this document from the DB:
    document_status = get_simple_approval_status(doctype, rn)
    if document_status is None:
        ## No information about this document in the approval database.
        ## Its approval has never been requested.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> No details about an approval request
 for the document [%s] have been found in the database.<br />
Before a decision can be made about it, a request for its approval must have
 been submitted.<br />
If you feel that there is a problem, please contact &lt;%s&gt;, quoting the
document's report number.
</div>""" % (cgi.escape(rn), cgi.escape(CFG_SITE_SUPPORT_EMAIL))
        raise InvenioWebSubmitFunctionStop(msg)
    elif document_status in ("approved", "rejected"):
        ## If a document was already approved or rejected, halt the approval
        ## process with a message for the referee:
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The document [%s] has
 already been %s.<br />
There is nothing more to be done in this case and your decision
 has <b>NOT</b> been taken into account.<br />
If you believe this to be an error, please contact &lt;%s&gt;, quoting the<br />
document's report-number [%s] and describing the problem.
</div>""" % (cgi.escape(rn), \
             cgi.escape(document_status), \
             cgi.escape(CFG_SITE_SUPPORT_EMAIL), \
             cgi.escape(rn))
        raise InvenioWebSubmitFunctionStop(msg)
    elif document_status == "withdrawn":
        ## Somebody had withdrawn the approval request for this document
        ## before the referee made this decision. Halt the approval process
        ## with a message for the referee:
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The request for the approval of the
 document [%s] had been withdrawn prior to the submission of your
 decision.<br />
Before a decision can be made regarding its status, a new request for its
 approval must be submitted by the author.<br />
Your decision has therefore <b>NOT</b> been taken into account.<br />
If you believe this to be an error, please contact &lt;%s&gt;, quoting the
 document's report-number [%s] and describing the problem.
</div>
""" % (cgi.escape(rn), \
       cgi.escape(CFG_SITE_SUPPORT_EMAIL), \
       cgi.escape(rn))
        raise InvenioWebSubmitFunctionStop(msg)
    elif document_status == "waiting":
        ## The document is awaiting approval. Register the referee's decision:
        if decision == "approve":
            ## Register the approval:
            update_approval_request_status(doctype, \
                                           rn, \
                                           note="",
                                           status="approved")
        else:
            ## Register the rejection:
            update_approval_request_status(doctype, \
                                           rn, \
                                           note="",
                                           status="rejected")
        ## Now retrieve the status of the document once more and check that
        ## it is either approved or rejected.  If not, the decision couldn't
        ## be registered and an error should be raised.
        status_after_update = get_simple_approval_status(doctype, rn)
        if status_after_update not in ("approved", "rejected"):
            msg = "Error in Register_Referee_Decision function: It was " \
                  "not possible to update the approvals database when " \
                  "trying to register the referee's descision of [%s] " \
                  "for the document [%s]. Please report this this " \
                  "problem to [%s], quoting the document's " \
                  "report-number [%s]." \
                  % (decision, rn, CFG_SITE_SUPPORT_EMAIL, rn)
            raise InvenioWebSubmitFunctionError(msg)
    else:
        ## The document had an unrecognised "status". Halt with an error.
        msg = "Error in Register_Referee_Decision function: The " \
              "document [%s] has an unknown approval status " \
              "[%s]. Unable to process the referee's decision. Please " \
              "report this problem to [%s], quoting the document's " \
              "report-number [%s] and describing the problem." \
              % (rn, document_status, CFG_SITE_SUPPORT_EMAIL, rn)
        raise InvenioWebSubmitFunctionError(msg)
    ## Finished.
    return ""
def Register_Referee_Decision(parameters, curdir, form, user_info=None):
    """
    A referee may either "accept" or "reject" a refereed document.
    The referee's decision is stored in a file in the submission's working
    directory and it is this function's job to read the contents of that
    file and update the status of the document's entry in the approvals
    table (sbmAPPROVAL) to be either "accepted" or "rejected" depending
    upon the referee's decision.

    @param decision_file: (string) - the name of the file in which the
        referee's decision is to be found.
        NOTE: A referee's decision _MUST_ be either "accept" or "reject".
              If not, an InvenioWebSubmitFunctionError will be raised.
              If a document's "approval status" is not "waiting" at the
              time of the referee's decision, the decision will not be
              taken into account and the submission will be halted.
              (This is because it's not appropriate to approve a document
              that has already been approved or rejected, has been
              withdrawn, etc.)

    @return: empty string.

    @Exceptions raised: InvenioWebSubmitFunctionError on unexpected error.
                        InvenioWebSubmitFunctionStop in the case where the
                        approval should be stopped for whatever reason.
                        (E.g. when it has already been approved.)
    """
    global rn
    doctype = form['doctype']
    ########
    ## Get the parameters from the list:
    ########
    ## Get the name of the "decision" file and read its value:
    ########
    decision = ""  ## variable to hold the referee's decision
    try:
        decision_file = parameters["decision_file"]
    except KeyError:
        ## No value given for the decision file:
        decision_file = None
    else:
        if decision_file is not None:
            decision_file = os.path.basename(decision_file).strip()
            if decision_file == "":
                decision_file = None
    if decision_file is None:
        ## Unable to obtain the name of the file in which the referee's
        ## decision is stored. Halt.
        err_msg = "Error in Register_Referee_Decision: Function was not " \
                  "configured with a valid value for decision_file - the " \
                  "file in which the referee's decision is stored. " \
                  "The referee's decision has not been processed for " \
                  "[%s]. Please inform the administrator." \
                  % rn
        raise InvenioWebSubmitFunctionError(err_msg)
    ## Read in the referee's decision:
    decision = ParamFromFile("%s/%s" % (curdir, decision_file)).lower()
    ##
    ########
    if decision not in ("approve", "reject"):
        ## Invalid value for the referee's decision.
        err_msg = "Error in Register_Referee_Decision: The value for the " \
                  "referee's decision (%s) was invalid. Please inform the " \
                  "administrator." % decision
        raise InvenioWebSubmitFunctionError(err_msg)
    ##
    ## Get the status of the approval request for this document from the DB:
    document_status = get_simple_approval_status(doctype, rn)
    if document_status is None:
        ## No information about this document in the approval database.
        ## Its approval has never been requested.
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> No details about an approval request
 for the document [%s] have been found in the database.<br />
Before a decision can be made about it, a request for its approval must have
 been submitted.<br />
If you feel that there is a problem, please contact &lt;%s&gt;, quoting the
document's report number.
</div>""" % (cgi.escape(rn), cgi.escape(CFG_SITE_SUPPORT_EMAIL))
        raise InvenioWebSubmitFunctionStop(msg)
    elif document_status in ("approved", "rejected"):
        ## If a document was already approved or rejected, halt the approval
        ## process with a message for the referee:
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The document [%s] has
 already been %s.<br />
There is nothing more to be done in this case and your decision
 has <b>NOT</b> been taken into account.<br />
If you believe this to be an error, please contact &lt;%s&gt;, quoting the<br />
document's report-number [%s] and describing the problem.
</div>""" % (cgi.escape(rn), \
             cgi.escape(document_status), \
             cgi.escape(CFG_SITE_SUPPORT_EMAIL), \
             cgi.escape(rn))
        raise InvenioWebSubmitFunctionStop(msg)
    elif document_status == "withdrawn":
        ## Somebody had withdrawn the approval request for this document
        ## before the referee made this decision. Halt the approval process
        ## with a message for the referee:
        msg = """
<br />
<div>
<span style="color: red;">Note:</span> The request for the approval of the
 document [%s] had been withdrawn prior to the submission of your
 decision.<br />
Before a decision can be made regarding its status, a new request for its
 approval must be submitted by the author.<br />
Your decision has therefore <b>NOT</b> been taken into account.<br />
If you believe this to be an error, please contact &lt;%s&gt;, quoting the
 document's report-number [%s] and describing the problem.
</div>
""" % (cgi.escape(rn), \
       cgi.escape(CFG_SITE_SUPPORT_EMAIL), \
       cgi.escape(rn))
        raise InvenioWebSubmitFunctionStop(msg)
    elif document_status == "waiting":
        ## The document is awaiting approval. Register the referee's decision:
        if decision == "approve":
            ## Register the approval:
            update_approval_request_status(doctype, \
                                           rn, \
                                           note="",
                                           status="approved")
        else:
            ## Register the rejection:
            update_approval_request_status(doctype, \
                                           rn, \
                                           note="",
                                           status="rejected")
        ## Now retrieve the status of the document once more and check that
        ## it is either approved or rejected.  If not, the decision couldn't
        ## be registered and an error should be raised.
        status_after_update = get_simple_approval_status(doctype, rn)
        if status_after_update not in ("approved", "rejected"):
            msg = "Error in Register_Referee_Decision function: It was " \
                  "not possible to update the approvals database when " \
                  "trying to register the referee's descision of [%s] " \
                  "for the document [%s]. Please report this this " \
                  "problem to [%s], quoting the document's " \
                  "report-number [%s]." \
                  % (decision, rn, CFG_SITE_SUPPORT_EMAIL, rn)
            raise InvenioWebSubmitFunctionError(msg)
    else:
        ## The document had an unrecognised "status". Halt with an error.
        msg = "Error in Register_Referee_Decision function: The " \
              "document [%s] has an unknown approval status " \
              "[%s]. Unable to process the referee's decision. Please " \
              "report this problem to [%s], quoting the document's " \
              "report-number [%s] and describing the problem." \
              % (rn, document_status, CFG_SITE_SUPPORT_EMAIL, rn)
        raise InvenioWebSubmitFunctionError(msg)
    ## Finished.
    return ""