Example #1
0
def add_tournament():
    add_url = url_for('.add_tournament')

    if request.method == 'POST':
        file = request.files['file']

        filename = file.filename
        if not filename:
            flash('Додайте файл з турнірними даними', 'isa_warning')
            return redirect(url_for('.add_tournament'))

        if not allowed_file(filename):
            flash('Файл має бути текстовим', 'isa_error')
            return redirect(url_for('.add_tournament'))

        insert_data = dict(request.form)
        insert_data['is_ranked'] = 'is_ranked' in insert_data.keys()
        insert_data['date_start'] = request.form.getlist('date_start')[0]
        insert_data['date_end'] = request.form.getlist('date_end')[0]
        tournament_id = None
        try:
            tournament_id = Tournament.execute_insert([insert_data])[0]
            parser = TournamentParser(file.read(), tournament_id)
            parser.run()
        except Exception:
            flash('Oops!', 'isa_error')
            if tournament_id:
                pairing_ids = Pairing.select_attrs(
                    ['id'], {'tp.tournament_id': tournament_id})
                existing_participant_ids = Participant.select_attrs(
                    ['id'], {'tournament_id': tournament_id})
                Pairing.execute_delete([p[0] for p in pairing_ids])
                Participant.execute_delete(
                    [p[0] for p in existing_participant_ids])
                Tournament.execute_delete([tournament_id])
            return redirect(url_for('.add_tournament'))
        return redirect(
            url_for('.tournament_info', tournament_id=tournament_id))

    cities = City.select_attrs(['id', 'name'])

    form = '\n'.join([
        '<div class="container">',
        f'<form action="{add_url}" enctype="multipart/form-data" method="post">',
        render_text_input_row("name", "Назва"),
        render_select_row('city_id', "Місто", cities),
        render_date_row('date_start', 'Дата початку',
                        str(datetime.date.today())),
        render_date_row('date_end', 'Дата закінчення',
                        str(datetime.date.today())),
        render_text_input_row("pin", "EGF PIN"),
        render_checkbox_row('is_ranked', "Рейтинговий"),
        render_file_row('file', 'Файл'),
        render_submit(),
        '</form>',
        '</div>',
    ])

    return render_template('add_tournament.html', form=form)
Example #2
0
def delete_tournament(tournament_id):
    try:
        pairing_ids = Pairing.select_attrs(['id'],
                                           {'tp.tournament_id': tournament_id})
        existing_participant_ids = Participant.select_attrs(
            ['id'], {'tournament_id': tournament_id})
        Pairing.execute_delete([str(p[0]) for p in pairing_ids])
        Participant.execute_delete(
            [str(p[0]) for p in existing_participant_ids])
        Tournament.execute_delete([str(tournament_id)])
        flash('Турнір видалений', 'isa_success')
        return redirect(url_for('.tournaments'))
    except mysql.connector.Error as err:
        flash(err.msg, 'isa_error')
        return redirect(
            url_for('.tournament_info', tournament_id=tournament_id))
Example #3
0
 def clean_tournament_data(self):
     pairing_ids = Pairing.select_attrs(['id'], {'tp.tournament_id': self.tournament_id})
     existing_participant_ids = Participant.select_attrs(['id'], {'tournament_id': self.tournament_id})
     Pairing.execute_delete([str(p[0]) for p in pairing_ids])
     Participant.execute_delete([str(p[0]) for p in existing_participant_ids])