Exemplo n.º 1
0
def upload():
    """
    Upload files page
    :return: "upload.html"
    """
    form = FileUploadForm()
    if form.validate_on_submit():
        upload = Upload(form=form)
        return redirect(upload.validate())
    return render_template('upload.html', title='Upload - Synco', form=form)
Exemplo n.º 2
0
def file_upload():
    form = FileUploadForm()
    if form.validate_on_submit():
        filename = secure_filename(form.file.data.filename)
        form.file.data.save('C:/Clikr/microblog2/uploads/' + filename)
        f= File(content='./uploads/'+filename)
        db.session.add(f)
        db.session.commit()
        return redirect(url_for('file_upload'))
    return render_template('file-upload.html', form=form)
def create_solution():
    """Route controlling creation of new solutions"""

    # dynamically load allowed file extensions
    file_upload_form = FileUploadForm()
    for extension in db.session.query(Language.extension).all():
        file_upload_form.allowed_extensions.append(extension[0])

    if file_upload_form.is_submitted():
        if file_upload_form.validate():

            problem_id = file_upload_form.data.get('problem_selection')

            # check problem_id doesn't already exist
            if Problem.query.filter_by(problem_id=problem_id).first() is not None:
                flash("A solution for that problem already exists - please specify an unsolved "
                      "problem or update the solution", "warning")

            else:
                title = file_upload_form.data.get('problem_title')
                contents = file_upload_form.file_upload.data.read()
                contents = contents.decode('utf-8').replace('\r\n', '\n')

                # check content is not null
                if not contents:
                    flash("File must not be empty", "warning")

                else:
                    # create objects to add to database
                    extension = file_upload_form.file_upload.data.filename.split('.')[-1]
                    language = Language.query.filter_by(extension=extension).first()
                    language_id = language.language_id
                    problem = Problem(problem_id=problem_id, contents=contents,
                                      language_id=language_id, title=title)

                    # add objects to database
                    db.session.add(problem)
                    db.session.commit()

                    # clear form values
                    file_upload_form.problem_selection.raw_data = ['']
                    file_upload_form.problem_title.data = None

                    flash("Solution created", "success")

        else:
            error_messages = [f.process_errors[0] for f in file_upload_form._fields.values() if f.process_errors]
            if error_messages:
                flash(error_messages[0], "warning")
            else:
                flash("An error was encountered - please review inputs and try again", "warning")

    return render_template('create_solution.html', file_upload_form=file_upload_form)
Exemplo n.º 4
0
def upload_org():
    org = defaultdict(list)
    form = FileUploadForm()
    if form.validate_on_submit():
        form.csv_file.data.save(
            os.path.join(app.config['UPLOAD_FOLDER'],
                         secure_filename(form.csv_file.data.filename)))
        with open(
                os.path.join(app.config['UPLOAD_FOLDER'],
                             secure_filename(
                                 form.csv_file.data.filename))) as input_file:
            data = csv.reader(input_file)

            for row in data:
                org[row[1]].append(row[0])

    return render_template('upload_org.html', form=form, organization=org)
Exemplo n.º 5
0
def uploader():
    form = FileUploadForm()

    if request.method == 'POST':

        if 'inputfile' not in request.files:
            flash('No File Part !', 'error')
            return redirect(request.url)

        f = request.files['inputfile']

        if f.filename == '':
            flash('No selected file', 'error')
            return redirect(request.url)
        if not allowed_file(f.filename):
            flash(
                'File type not Supported ! Please contact administrator if you want to do so.',
                'error')
            return redirect(request.url)

        # final check of file and saving and indexing
        if f and allowed_file(f.filename):
            f.filename = secure_filename(f.filename)

            fileindex = File(filename=f.filename,
                             filedata=f.read(),
                             user_id=current_user.id)

            db.session.add(fileindex)
            db.session.commit()

            fileID = File().query.filter_by(filename=f.filename).first()

            flash('File uploaded successfully !', 'success')
            return redirect(url_for('uploaded_file', fileID=fileID.id))
    return render_template('site/fileupload.html',
                           title='Upload Files',
                           form=form)
Exemplo n.º 6
0
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())