def remove_record_via_id(resource_id, record_id): """ Remove a record from a datastore/resource based on its '_id' """ ckan.delete_records( resource_id, { '_id': record_id } )
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)
def gemeente_stemlokaal_delete(stemlokaal_id=None): # Select a gemeente if none is currently selected if not 'selected_gemeente_code' in session: return redirect(url_for('gemeente_selectie')) gemeente = Gemeente.query.filter_by( gemeente_code=session['selected_gemeente_code']).first() elections = gemeente.elections.all() if stemlokaal_id: for election in [x.verkiezing for x in elections]: ckan.delete_records(ckan.elections[election]['draft_resource'], {'UUID': stemlokaal_id}) flash('Stembureau verwijderd') return redirect(url_for('gemeente_stemlokalen_overzicht'))
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')
def gemeente_stemlokalen_edit(stemlokaal_id=None): # Select a gemeente if none is currently selected if not session['selected_gemeente_code']: return redirect(url_for('gemeente_selectie')) gemeente = Gemeente.query.filter_by( gemeente_code=session['selected_gemeente_code']).first() elections = 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'] == gemeente.gemeente_code ] # Initialize the form with the data already available in the draft init_record = {} if stemlokaal_id: for record in gemeente_draft_records: if record['UUID'] == stemlokaal_id: # Split the Verkiezingen attribute into a list if record['Verkiezingen']: record['Verkiezingen'] = [ x.strip() for x in record['Verkiezingen'].split(';') ] init_record = Record( **{k.lower(): v for k, v in record.items()}).record form = EditForm(**init_record) # When the user clicked the 'Annuleren' button go back to the # overzicht page without doing anything if form.submit_annuleren.data: flash('Bewerking geannuleerd') return redirect(url_for('gemeente_stemlokalen_overzicht')) # When the user clicked the 'Verwijderen' button delete the # stembureau from the draft_resources of each election if form.submit_verwijderen.data: if stemlokaal_id: for election in [x.verkiezing for x in elections]: ckan.delete_records(ckan.elections[election]['draft_resource'], {'UUID': stemlokaal_id}) flash('Stembureau verwijderd') return redirect(url_for('gemeente_stemlokalen_overzicht')) # When the user clicked the 'Opslaan' button save the stembureau # to the draft_resources of each election if form.validate_on_submit(): if not stemlokaal_id: stemlokaal_id = uuid.uuid4().hex for election in [x.verkiezing for x in elections]: record = _create_record(form, stemlokaal_id, gemeente, election) ckan.save_records(ckan.elections[election]['draft_resource'], records=[record]) flash('Stembureau opgeslagen') return redirect(url_for('gemeente_stemlokalen_overzicht')) return render_template('gemeente-stemlokalen-edit.html', form=form, gemeente=gemeente, upload_deadline_passed=check_deadline_passed())
def gemeente_stemlokalen_dashboard(): # Select a gemeente if none is currently selected if not 'selected_gemeente_code' in session: return redirect(url_for('gemeente_selectie')) gemeente = Gemeente.query.filter_by( gemeente_code=session['selected_gemeente_code']).first() elections = 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_publish_records = ckan.get_records( ckan.elections[verkiezing]['publish_resource']) all_draft_records = ckan.get_records( ckan.elections[verkiezing]['draft_resource']) gemeente_publish_records = [ record for record in all_publish_records['records'] if record['CBS gemeentecode'] == gemeente.gemeente_code ] gemeente_draft_records = [ record for record in all_draft_records['records'] if record['CBS gemeentecode'] == gemeente.gemeente_code ] _remove_id(gemeente_publish_records) _remove_id(gemeente_draft_records) toon_stembureaus_pagina = False if gemeente_publish_records: toon_stembureaus_pagina = True show_publish_note = False if gemeente_draft_records != gemeente_publish_records: show_publish_note = True vooringevuld = '' vooringevuld_fn = ( 'files/deels_vooringevuld/waarismijnstemlokaal.nl_invulformulier_%s_' 'deels_vooringevuld.xlsx' % (gemeente.gemeente_naam)) if os.path.exists(vooringevuld_fn): vooringevuld = vooringevuld_fn form = FileUploadForm() # Save, parse and validate an uploaded spreadsheet and save the # stembureaus if form.validate_on_submit(): f = form.data_file.data filename = secure_filename(f.filename) filename = '%s__%s' % (gemeente.gemeente_code, filename) file_path = os.path.join( os.path.abspath(os.path.join(app.instance_path, '../upload')), filename) f.save(file_path) parser = UploadFileParser() app.logger.info('Processing uploaded file for %s' % (gemeente.gemeente_naam)) try: records = parser.parse(file_path) except ValueError as e: app.logger.warning('Upload failed: %s' % e) flash( Markup( '<span class="text-red">Uploaden mislukt</span>. Het ' 'lijkt er op dat u geen gebruik maakt van (de meest ' 'recente versie van) de stembureau-spreadsheet. Download ' 'een <a href="/files/waarismijnstemlokaal.nl_' 'invulformulier.xlsx"><b>leeg</b></a> of <a href="%s"><b>' 'deels vooringevuld</b></a> stembureau-spreadsheet en vul ' 'de gegevens volgens de instructies in de spreadsheet in ' 'om deze vervolgens op deze pagina te ' 'uploaden.' % (vooringevuld))) return render_template( 'gemeente-stemlokalen-dashboard.html', verkiezing_string=_format_verkiezingen_string(elections), gemeente=gemeente, total_publish_records=len(gemeente_publish_records), total_draft_records=len(gemeente_draft_records), form=form, show_publish_note=show_publish_note, vooringevuld=vooringevuld, toon_stembureaus_pagina=toon_stembureaus_pagina, upload_deadline_passed=check_deadline_passed()) validator = Validator() results = validator.validate(records) # If the spreadsheet did not validate then return the errors as # flash messages if not results['no_errors']: flash( Markup( '<span class="text-red">Uploaden mislukt</span>. Los de ' 'hieronder getoonde foutmeldingen op en upload de ' 'spreadsheet opnieuw.' '<br><br>')) for column_number, col_result in sorted( results['results'].items()): if col_result['errors']: error_flash = ( '<b>Foutmelding(en) in <span class="text-red">' 'invulveld %s (oftewel kolom "%s")</span></b>:' % (column_number - 5, _colnum2string(column_number))) error_flash += '<ul>' for column_name, error in col_result['errors'].items(): error_flash += '<li>%s: %s</li>' % (column_name, error[0]) error_flash += '</ul><br>' flash(Markup(error_flash)) # If there not a single value in the results then state that we # could not find any stembureaus elif not results['found_any_record_with_values']: flash( Markup( '<span class="text-red">Uploaden mislukt</span>. Er zijn geen ' 'stembureaus gevonden in de 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 # and finally redirect to the overzicht 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': 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'], gemeente, election)) ckan.save_records(ckan.elections[election]['draft_resource'], records=records) flash( 'Het uploaden van stembureaus is gelukt! Controleer in het ' 'overzicht hieronder of alles klopt en voer eventuele ' 'wijzigingen door. Klik vervolgens op de "Publiceer"-knop als ' 'alles klopt.') return redirect(url_for('gemeente_stemlokalen_overzicht')) return render_template( 'gemeente-stemlokalen-dashboard.html', verkiezing_string=_format_verkiezingen_string(elections), gemeente=gemeente, total_publish_records=len(gemeente_publish_records), total_draft_records=len(gemeente_draft_records), form=form, show_publish_note=show_publish_note, vooringevuld=vooringevuld, toon_stembureaus_pagina=toon_stembureaus_pagina, upload_deadline_passed=check_deadline_passed())
def gemeente_stemlokalen_edit(stemlokaal_id=None): # Select a gemeente if none is currently selected if not 'selected_gemeente_code' in session: return redirect(url_for('gemeente_selectie')) gemeente = Gemeente.query.filter_by( gemeente_code=session['selected_gemeente_code']).first() elections = gemeente.elections.all() # Need this to get a starting point for the clickmap; # Uses re.sub to remove provinces from some gemeenten which is how we write # gemeenten in WIMS, but which are not used in the BAG, e.g. 'Beek (L.)', # but keep 'Bergen (NH.)' and 'Bergen (L.)' as the BAG also uses that # spelling. # TODO this won't work for BES-eilanden as they don't exist in the BAG, so # exclude them from the filter below and initialize a custom bag_record # with coordinates for the BES-eilanden. bag_record = BAG.query.filter_by( gemeente=gemeente.gemeente_naam if 'Bergen (' in gemeente.gemeente_naam else re.sub(' \(.*\)$', '', gemeente.gemeente_naam)).order_by( 'openbareruimte').first() # 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 for 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'] == gemeente.gemeente_code ] # Initialize the form with the data already available in the draft init_record = {} if stemlokaal_id: for record in gemeente_draft_records: if record['UUID'] == stemlokaal_id: # Split the Verkiezingen attribute into a list if record['Verkiezingen']: record['Verkiezingen'] = [ x.strip() for x in record['Verkiezingen'].split(';') ] init_record = Record( **{k.lower(): v for k, v in record.items()}).record form = EditForm(**init_record) # When the user clicked the 'Annuleren' button go back to the # overzicht page without doing anything if form.submit_annuleren.data: flash('Bewerking geannuleerd') return redirect(url_for('gemeente_stemlokalen_overzicht')) # When the user clicked the 'Verwijderen' button delete the # stembureau from the draft_resources of each election if form.submit_verwijderen.data: if stemlokaal_id: for election in [x.verkiezing for x in elections]: ckan.delete_records(ckan.elections[election]['draft_resource'], {'UUID': stemlokaal_id}) flash('Stembureau verwijderd') return redirect(url_for('gemeente_stemlokalen_overzicht')) # When the user clicked the 'Opslaan' button save the stembureau # to the draft_resources of each election if form.validate_on_submit(): if not stemlokaal_id: stemlokaal_id = uuid.uuid4().hex for election in [x.verkiezing for x in elections]: record = _create_record(form, stemlokaal_id, gemeente, election) ckan.save_records(ckan.elections[election]['draft_resource'], records=[record]) flash('Stembureau opgeslagen') return redirect(url_for('gemeente_stemlokalen_overzicht')) return render_template('gemeente-stemlokalen-edit.html', form=form, gemeente=gemeente, bag_record=bag_record, upload_deadline_passed=check_deadline_passed())