Esempio n. 1
0
def copy_gemeente_resource(gemeente_code, source_resource, dest_resource,
                           dest_id=None, dest_hoofdstembureau=None,
                           dest_kieskring_id=None):
    """
    Copies the records of a gemeente from one resource (source) to another
    (dest). Note: this removes all records for the gemeente in dest first.
    If dest contains no records then you need to specify the ID,
    Hoofdstembureau and Kieskring ID value for the gemeente in the dest
    resource.
    """
    all_resource_records = ckan.get_records(source_resource)
    gemeente_resource_records = [
        record for record in all_resource_records['records']
        if record['CBS gemeentecode'] == gemeente_code
    ]
    _remove_id(gemeente_resource_records)

    # If either one of these parameters is not set then try to get the
    # values from the dest_resource
    if not dest_id or not dest_hoofdstembureau or not dest_kieskring_id:
        all_dest_resource_records = ckan.get_records(dest_resource)
        gemeente_dest_resource_records = [
            record for record in all_dest_resource_records['records']
            if record['CBS gemeentecode'] == gemeente_code
        ]
        if gemeente_dest_resource_records:
            dest_id = gemeente_dest_resource_records[0]['ID']
            dest_hoofdstembureau = gemeente_dest_resource_records[0][
                'Hoofdstembureau'
            ]
            dest_kieskring_id = gemeente_dest_resource_records[0][
                'Kieskring ID'
            ]

    # If either of these is still not set, abort!
    if not dest_id or not dest_hoofdstembureau or not dest_kieskring_id:
        print(
            'Could not retrieve dest_id or dest_hoofdstembureau or '
            'dest_kieskring_id'
        )

    for record in gemeente_resource_records:
        record['ID'] = dest_id
        record['Hoofdstembureau'] = dest_hoofdstembureau
        record['Kieskring ID'] = dest_kieskring_id

    ckan.delete_records(
        dest_resource,
        {'CBS gemeentecode': gemeente_code}
    )
    ckan.save_records(dest_resource, gemeente_resource_records)
Esempio n. 2
0
def publish_gemeente(gemeente_code):
    """
    Publishes the saved (draft) stembureaus of a gemeente
    """
    current_gemeente = _get_gemeente(gemeente_code)

    elections = current_gemeente.elections.all()

    for election in [x.verkiezing for x in elections]:
        temp_all_draft_records = ckan.get_records(
            ckan.elections[election]['draft_resource']
        )
        temp_gemeente_draft_records = [
            record for record in temp_all_draft_records['records']
            if record['CBS gemeentecode'] == current_gemeente.gemeente_code
        ]
        _remove_id(temp_gemeente_draft_records)
        ckan.publish(election, current_gemeente.gemeente_code, temp_gemeente_draft_records)
Esempio n. 3
0
def upload_stembureau_spreadsheet(gemeente_code, file_path):
    """
    Uploads a stembureau spreadheet, specify full absolute file_path
    """
    current_gemeente = _get_gemeente(gemeente_code)

    elections = current_gemeente.elections.all()
    # Pick the first election. In the case of multiple elections we only
    # retrieve the stembureaus of the first election as the records for
    # both elections are the same (at least the GR2018 + referendum
    # elections on March 21st 2018).
    verkiezing = elections[0].verkiezing
    all_draft_records = ckan.get_records(
        ckan.elections[verkiezing]['draft_resource']
    )
    gemeente_draft_records = [
        record for record in all_draft_records['records']
        if record['CBS gemeentecode'] == current_gemeente.gemeente_code
    ]
    _remove_id(gemeente_draft_records)

    parser = UploadFileParser()
    app.logger.info(
        'Manually (CLI) uploading file for '
        '%s' % (current_gemeente.gemeente_naam)
    )
    try:
        records = parser.parse(file_path)
    except ValueError as e:
        app.logger.warning('Manual upload failed: %s' % e)
        return

    validator = Validator()
    results = validator.validate(records)

    # If the spreadsheet did not validate then return the errors
    if not results['no_errors']:
        print(
            'Upload failed. Fix the errors shown below and try again.\n\n'
        )
        for column_number, col_result in sorted(
                results['results'].items()):
            if col_result['errors']:
                print(
                    'Error(s) in '
                    'invulveld %s:' % (
                        column_number - 5
                    )
                )
                for column_name, error in col_result['errors'].items():
                    print(
                        '%s: %s\n' % (
                            column_name, error[0]
                        )
                    )
    # If there is not a single value in the results then state that we
    # could not find any stembureaus
    elif not results['found_any_record_with_values']:
        print(
            'Upload failed. No stembureaus have been found in this '
            'spreadsheet.'
        )
    # If the spreadsheet did validate then first delete all current
    # stembureaus from the draft_resource and then save the newly
    # uploaded stembureaus to the draft_resources of each election
    else:
        # Delete all stembureaus of current gemeente
        if gemeente_draft_records:
            for election in [x.verkiezing for x in elections]:
                ckan.delete_records(
                    ckan.elections[election]['draft_resource'],
                    {
                        'CBS gemeentecode': current_gemeente.gemeente_code
                    }
                )

        # Create and save records
        for election in [x.verkiezing for x in elections]:
            records = []
            for _, result in results['results'].items():
                if result['form']:
                    records.append(
                        _create_record(
                            result['form'],
                            result['uuid'],
                            current_gemeente,
                            election
                        )
                    )
            ckan.save_records(
                ckan.elections[election]['draft_resource'],
                records=records
            )
        print('Upload succesful!')
    print('\n\n')