Ejemplo n.º 1
0
def invalid_request_file_message(f, file_type):
    try:
        # Only validate JSON files.
        if file_type == 'csv xlsx zip':
            return

        data = json.load(f)

        if file_type == 'record-package':
            if not is_record_package(data):
                return _('Not a record package')
        elif file_type == 'release-package':
            if not is_release_package(data):
                return _('Not a release package')
        elif file_type == 'package release':
            if not is_release(data) and not is_package(data):
                return _('Not a release or package')
        elif file_type == 'package package-array':
            if (isinstance(data, list) and any(not is_package(item) for item in data) or
                    not isinstance(data, list) and not is_package(data)):
                return _('Not a package or list of packages')
        elif file_type == 'release release-array':
            if (isinstance(data, list) and any(not is_release(item) for item in data) or
                    not isinstance(data, list) and not is_release(data)):
                return _('Not a release or list of releases')
        else:
            return _('"%(type)s" not recognized') % {'type': file_type}
    except json.JSONDecodeError:
        return _('Error decoding JSON')
Ejemplo n.º 2
0
def upgrade_10_11(data):
    """
    Upgrades a record package, release package, record or release from 1.0 to 1.1.

    Retains the deprecated Amendment.changes, Budget.source and Milestone.documents fields.

    ``data`` must be an ``OrderedDict``. If you have only the parsed JSON, re-parse it with:

    ``upgrade_10_11(json.loads(json.dumps(data), object_pairs_hook=OrderedDict))``
    """
    version = get_ocds_minor_version(data)
    if version != '1.0':
        return data

    if is_package(data):
        data['version'] = '1.1'
        _move_to_top(data, ('uri', 'version'))

    if is_record_package(data):
        for record in data['records']:
            upgrade_record_10_11(record)
    elif is_release_package(data):
        for release in data['releases']:
            upgrade_release_10_11(release)
    elif is_record(data):
        upgrade_record_10_11(data)
    else:  # release
        upgrade_release_10_11(data)

    return data
Ejemplo n.º 3
0
def upgrade_10_11(data):
    """
    Upgrades a record package, release package or release from 1.0 to 1.1.

    Retains the deprecated Amendment.changes, Budget.source and Milestone.documents fields.
    """
    version = get_ocds_minor_version(data)
    if version != '1.0':
        return

    if is_package(data):
        data['version'] = '1.1'
        _move_to_top(data, ('uri', 'version'))

    if is_record_package(data):
        for record in data['records']:
            if 'releases' in record:
                for release in record['releases']:
                    upgrade_release_10_11(release)
            if 'compiledRelease' in record:
                upgrade_release_10_11(record['compiledRelease'])
    elif is_release_package(data):
        for release in data['releases']:
            upgrade_release_10_11(release)
    else:  # release
        upgrade_release_10_11(data)
Ejemplo n.º 4
0
    def handle(self):
        deref_schema = jsonref.load_uri(self.args.schema)
        metadata, engine = self.create_db(self.args.database_url, deref_schema, drop=self.args.drop)

        for data in self.items():
            if is_record_package(data):
                releases = []
                for record in data['records']:
                    releases.extend(record['releases'])
                    if 'compiledRelease' in record:
                        releases.append(record['compiledRelease'])
            elif is_release_package(data) or is_record(data):
                releases = data['releases']
            else:  # release
                releases = [data]

            self.upload_file(metadata, engine, releases)
Ejemplo n.º 5
0
def test_is_release_package(filename, expected):
    assert is_release_package(json.loads(read(filename))) == expected