Example #1
0
def oai_list_metadata_formats(argd):
    """Generates response to oai_list_metadata_formats verb."""

    if argd.get("identifier"):
        recid = oai_get_recid(argd["identifier"])
        _record_exists = record_exists(recid)
        if _record_exists != 1 and (_record_exists != -1 or CFG_OAI_DELETED_POLICY == "no"):
            return oai_error(argd, [("idDoesNotExist", "invalid record Identifier: %s" % argd["identifier"])])

    out = ""
    for prefix, (dummy, schema, namespace) in CFG_OAI_METADATA_FORMATS.items():
        out += X.metadataFormat()(X.metadataPrefix(prefix), X.schema(schema), X.metadataNamespace(namespace))

    return oai_header(argd, "ListMetadataFormats") + out + oai_footer("ListMetadataFormats")
Example #2
0
def oai_list_metadata_formats(argd):
    """Generates response to oai_list_metadata_formats verb."""

    if argd.get('identifier'):
        recid = oai_get_recid(argd['identifier'])
        _record_exists = record_exists(recid)
        if _record_exists != 1 and (_record_exists != -1 or CFG_OAI_DELETED_POLICY == "no"):
            return oai_error(argd, [("idDoesNotExist", "invalid record Identifier: %s" % argd['identifier'])])

    out = ""
    for prefix, (dummy, schema, namespace) in CFG_OAI_METADATA_FORMATS.items():
        out += X.metadataFormat()(
            X.metadataPrefix(prefix),
            X.schema(schema),
            X.metadataNamespace(namespace)
        )

    return oai_header(argd, "ListMetadataFormats") + out + oai_footer("ListMetadataFormats")
Example #3
0
def check_argd(argd):
    """
    Check OAI arguments
    Also transform them from lists to strings.
    """
    errors = []

    ## no several times the same argument
    bad_arguments_error = False
    for param, value in iteritems(argd):
        if len(value) > 1 and not bad_arguments_error:
            errors.append(("badArgument", "More than one value specified for the %s argument: %s" % (param, value)))
            bad_arguments_error = True ## This is needed only once
        if len(value) > 0:
            argd[param] = value[0]
        else:
            argd[param] = ''

    ## principal argument required
    if argd['verb'] not in CFG_VERBS:
        errors.append(("badVerb", "Illegal OAI verb: %s" % argd['verb']))

    ## defined argd
    for param in argd.keys():
        if not param in CFG_VERBS.get(argd['verb'], []) and param != 'verb' \
               and not bad_arguments_error:
            errors.append(("badArgument", "The request includes illegal arguments for the given verb: %s" % param))
            bad_arguments_error = True
            break # Indicate only once

    ## resumptionToken exclusive
    if argd.get('resumptionToken', '') != "" and \
           len(argd.keys()) != 2 and not bad_arguments_error:
        errors.append(("badArgument", "The resumptionToken was specified together with other arguments"))
        bad_arguments_error = True

    if argd.get('resumptionToken', None) == '':
        errors.append(("badResumptionToken", "ResumptionToken invalid: %s" % argd.get('resumptionToken', None)))

    ## datestamp formats
    if 'from' in argd and \
           'from' in CFG_VERBS.get(argd['verb'], []):
        from_length = len(argd['from'])
        if check_date(argd['from']) == "":
            errors.append(("badArgument", "Bad datestamp format in from: %s" % argd['from']))
    else:
        from_length = 0

    if 'until' in argd and \
           'until' in CFG_VERBS.get(argd['verb'], []):
        until_length = len(argd['until'])
        if check_date(argd['until']) == "":
            errors.append(("badArgument", "Bad datestamp format in until: %s" % argd['until']))
    else:
        until_length = 0

    if from_length != 0:
        if until_length != 0:
            if from_length != until_length:
                errors.append(("badArgument", "From and until have two different formats: %s Vs. %s" % (from_length, until_length)))

    if 'from' in argd and 'until' in argd \
           and argd['from'] > argd['until'] and \
           'from' in CFG_VERBS.get(argd['verb'], []) and \
           'until' in CFG_VERBS.get(argd['verb'], []):
        errors.append(("badArgument", "from argument comes after until argument: %s > %s" % (argd['from'], argd['until'])))

    ## Identify exclusive
    if argd['verb'] == "Identify" and \
           len(argd.keys()) != 1:
        if not bad_arguments_error: # Do not repeat this error
            errors.append(("badArgument", "The request includes illegal arguments"))
            bad_arguments_error = True

    ## parameters for GetRecord
    if argd['verb'] == "GetRecord" and \
           'identifier' not in argd:
        errors.append(("badArgument", "Record identifier missing"))

    if argd['verb'] == "GetRecord" and \
           'metadataPrefix' not in argd:
        errors.append(("badArgument", "Missing metadataPrefix"))

    ## parameters for ListRecords and ListIdentifiers
    if (argd['verb'] == "ListRecords" or argd['verb'] == "ListIdentifiers") and \
           ('resumptionToken' not in argd and 'metadataPrefix' not in argd):
        errors.append(("badArgument", "Missing metadataPrefix"))

    ## Metadata prefix defined and valid
    if 'metadataPrefix' in argd and \
           not argd['metadataPrefix'] in CFG_OAI_METADATA_FORMATS:
        errors.append(("cannotDisseminateFormat", "Chosen format is not supported. Valid formats are: %s" % ', '.join(CFG_OAI_METADATA_FORMATS.keys())))

    return errors
Example #4
0
def check_argd(argd):
    """
    Check OAI arguments
    Also transform them from lists to strings.
    """
    errors = []

    ## no several times the same argument
    bad_arguments_error = False
    for param, value in argd.iteritems():
        if len(value) > 1 and not bad_arguments_error:
            errors.append(
                ("badArgument",
                 "More than one value specified for the %s argument: %s" %
                 (param, value)))
            bad_arguments_error = True  ## This is needed only once
        if len(value) > 0:
            argd[param] = value[0]
        else:
            argd[param] = ''

    ## principal argument required
    if argd['verb'] not in CFG_VERBS:
        errors.append(("badVerb", "Illegal OAI verb: %s" % argd['verb']))

    ## defined argd
    for param in argd.keys():
        if not param in CFG_VERBS.get(argd['verb'], []) and param != 'verb' \
               and not bad_arguments_error:
            errors.append((
                "badArgument",
                "The request includes illegal arguments for the given verb: %s"
                % param))
            bad_arguments_error = True
            break  # Indicate only once

    ## resumptionToken exclusive
    if argd.get('resumptionToken', '') != "" and \
           len(argd.keys()) != 2 and not bad_arguments_error:
        errors.append((
            "badArgument",
            "The resumptionToken was specified together with other arguments"))
        bad_arguments_error = True

    if argd.get('resumptionToken', None) == '':
        errors.append(("badResumptionToken", "ResumptionToken invalid: %s" %
                       argd.get('resumptionToken', None)))

    ## datestamp formats
    if argd.has_key('from') and \
           'from' in CFG_VERBS.get(argd['verb'], []):
        from_length = len(argd['from'])
        if check_date(argd['from']) == "":
            errors.append(("badArgument",
                           "Bad datestamp format in from: %s" % argd['from']))
    else:
        from_length = 0

    if argd.has_key('until') and \
           'until' in CFG_VERBS.get(argd['verb'], []):
        until_length = len(argd['until'])
        if check_date(argd['until']) == "":
            errors.append(
                ("badArgument",
                 "Bad datestamp format in until: %s" % argd['until']))
    else:
        until_length = 0

    if from_length != 0:
        if until_length != 0:
            if from_length != until_length:
                errors.append(
                    ("badArgument",
                     "From and until have two different formats: %s Vs. %s" %
                     (from_length, until_length)))

    if argd.has_key('from') and argd.has_key('until') \
           and argd['from'] > argd['until'] and \
           'from' in CFG_VERBS.get(argd['verb'], []) and \
           'until' in CFG_VERBS.get(argd['verb'], []):
        errors.append(("badArgument",
                       "from argument comes after until argument: %s > %s" %
                       (argd['from'], argd['until'])))

    ## Identify exclusive
    if argd['verb'] == "Identify" and \
           len(argd.keys()) != 1:
        if not bad_arguments_error:  # Do not repeat this error
            errors.append(
                ("badArgument", "The request includes illegal arguments"))
            bad_arguments_error = True

    ## parameters for GetRecord
    if argd['verb'] == "GetRecord" and \
           not argd.has_key('identifier'):
        errors.append(("badArgument", "Record identifier missing"))

    if argd['verb'] == "GetRecord" and \
           not argd.has_key('metadataPrefix'):
        errors.append(("badArgument", "Missing metadataPrefix"))

    ## parameters for ListRecords and ListIdentifiers
    if (argd['verb'] == "ListRecords" or argd['verb'] == "ListIdentifiers") and \
           (not argd.has_key('resumptionToken') and not argd.has_key('metadataPrefix')):
        errors.append(("badArgument", "Missing metadataPrefix"))

    ## Metadata prefix defined and valid
    if argd.has_key('metadataPrefix') and \
           not argd['metadataPrefix'] in CFG_OAI_METADATA_FORMATS:
        errors.append(
            ("cannotDisseminateFormat",
             "Chosen format is not supported. Valid formats are: %s" %
             ', '.join(CFG_OAI_METADATA_FORMATS.keys())))

    return errors