def PUT(self, race_id, vehicle_id, station, **data): race_id = Validator.require_int(race_id) vehicle_id = Validator.require_int(vehicle_id) station = Validator.require_int(station) resource = self.find({ "race_id": race_id, "vehicle_id": vehicle_id, "station": station }) if resource is None: resource = { "race_id": race_id, "vehicle_id": vehicle_id, "station": station } data = Validator.validate(data, self.__class__.fields, self.__class__.defaults, require_all=True) self.resources.append(resource) else: data = Validator.validate(data, self.__class__.fields, self.__class__.defaults, require_all=False) for field in data: resource[field] = data[field] self.save() return self.response(resource)
def PUT(self, race_id, vehicle_id, **data): race_id = Validator.require_int(race_id) vehicle_id = Validator.require_int(vehicle_id) resource = self.find({"race_id": race_id, "vehicle_id": vehicle_id}) if resource is None: resource = {"race_id": race_id, "vehicle_id": vehicle_id} data = Validator.validate(data, self.__class__.fields, self.__class__.defaults, require_all=True) self.resources.append(resource) else: data = Validator.validate(data, self.__class__.fields, self.__class__.defaults, require_all=False) for field in data: resource[field] = data[field] self.save() return self.response(resource)
def api_create(self, **data): fillable = Validator.validate(data, self.__class__.fields, self.__class__.defaults) if self.find({"login": fillable["login"]}) is not None: Validator.fail('Der Benutzername ist bereits vergeben.') resource = self.create(fillable) return resource
class TestValidator(unittest.TestCase): def setUp(self): self.validator = Validator() self.test_records = [ Record(**x).record for x in [test_record, test_record] ] def test_parse_empty(self): results = self.validator.validate() self.assertEqual(results['no_errors'], True) self.assertEqual(results['results'], {}) def test_parse_one(self): with app.app.test_request_context('/'): results = self.validator.validate( records=self.test_records) self.assertEqual(results['no_errors'], True)
def api_create(self, **data): fillable = Validator.validate(data, self.__class__.fields, self.__class__.defaults) if self.find({"login": fillable["login"]}) is not None: Validator.fail('Der Benutzername ist bereits vergeben.'); resource = self.create(fillable) return resource
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_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())