Skip to content
Snippets Groups Projects
Select Git revision
  • 4c7c4761b1cb02ca0158672edc6101b15cd6a20e
  • master default
2 results

ConfigParser.cs

Blame
  • org_users.py 5.50 KiB
    from operator import or_
    from flask import render_template, redirect, url_for, flash, request
    from flask_wtf import FlaskForm
    import werkzeug.exceptions
    import wtforms
    
    from typing import Optional, List
    
    import mo
    import mo.db as db
    import mo.rights
    import mo.util
    from mo.web import app
    
    
    class PagerForm(FlaskForm):
        limit = wtforms.IntegerField()
        offset = wtforms.IntegerField()
        next = wtforms.SubmitField("Další")
        previous = wtforms.SubmitField("Předchozí")
    
    
    class UsersFilterForm(PagerForm):
        # participants
        year = wtforms.IntegerField("Ročník")
        school_code = wtforms.StringField("Škola")
    
        # rounds->participations
        round_year = wtforms.IntegerField("Ročník")
        round_category = wtforms.SelectField("Kategorie", choices=['*'] + sorted(db.get_categories()))
        round_seq = wtforms.SelectField("Kolo", choices=['*'] + sorted(db.get_seqs()))
        contest_site_code = wtforms.StringField("Soutěžní místo")
        participation_state = wtforms.SelectField('Účast', choices=[('*', '*')] + list(db.PartState.choices()))
    
        submit = wtforms.SubmitField("Filtrovat")
    
    
    @app.route('/org/users/')
    def org_users():
        sess = db.get_session()
    
        q = sess.query(db.User).filter_by(is_admin=False, is_org=False)
        filter = UsersFilterForm(request.args)
    
        filter_errors = []
    
        participant_filter = sess.query(db.Participant.user_id)
        participant_filter_apply = False
        if filter.year.data:
            participant_filter = participant_filter.filter_by(year=filter.year.data)
            participant_filter_apply = True
        print(filter.school_code.data)
        if filter.school_code.data:
            place = db.place_by_code(filter.school_code.data)
            if place:
                participant_filter = participant_filter.filter_by(school=place.place_id)
                participant_filter_apply = True
            else:
                filter_errors.append("Neexistující kód školy")
    
        if participant_filter_apply:
            q = q.filter(db.User.user_id.in_(participant_filter))
    
        round_filter = sess.query(db.Round.round_id)
        round_filter_apply = False
        if filter.round_year.data:
            round_filter = round_filter.filter_by(year=filter.round_year.data)
            round_filter_apply = True
        if filter.round_category.data and filter.round_category.data != "*":
            round_filter = round_filter.filter_by(category=filter.round_category.data)
            round_filter_apply = True
        if filter.round_seq.data and filter.round_seq.data != "*":
            round_filter = round_filter.filter_by(seq=filter.round_seq.data)
            round_filter_apply = True
    
        contest_filter = sess.query(db.Contest.contest_id)
        contest_filter_apply = False
        if round_filter_apply:
            contest_filter = contest_filter.filter(db.Contest.round_id.in_(round_filter))
            contest_filter_apply = True
        if filter.contest_site_code.data:
            place = db.place_by_code(filter.contest_site_code.data)
            if place:
                contest_filter = contest_filter.filter_by(place_id=place.place_id)
                contest_filter_apply = True
            else:
                filter_errors.append("Neexistující kód soutěžního místa")
    
        participation_filter = sess.query(db.Participation.user_id)
        participation_filter_apply = False
        if contest_filter_apply:
            participation_filter = participation_filter.filter(db.Participation.contest_id.in_(contest_filter))
            participation_filter_apply = True
        if filter.participation_state.data and filter.participation_state.data != '*':
            participation_filter = participation_filter.filter_by(state=filter.participation_state.data)
            participation_filter_apply = True
    
        if participation_filter_apply:
            q = q.filter(db.User.user_id.in_(participation_filter))
    
        # print(str(q))
        count = q.count()
    
        if not filter.offset.data:
            filter.offset.data = 0
        if not filter.limit.data:
            filter.limit.data = 50
    
        if filter.previous.data:
            filter.offset.data = max(0, filter.offset.data - 50)
        if filter.next.data:
            filter.offset.data = min(count // 50 * 50, filter.offset.data + 50)
    
        q = q.offset(filter.offset.data)
        q = q.limit(filter.limit.data)
        users = q.all()
    
        return render_template('org_users.html', users=users, count=count, filter=filter, filter_errors=filter_errors)
    
    
    class OrgsFilterForm(PagerForm):
        # TODO: filtering by roles?
        submit = wtforms.SubmitField("Filtrovat")
    
    
    @app.route('/org/users/orgs/')
    def org_users_orgs():
        sess = db.get_session()
    
        q = sess.query(db.User).filter(or_(db.User.is_admin, db.User.is_org))
        filter = OrgsFilterForm(request.args)
        # TODO: filtering by roles?
    
        count = q.count()
    
        if not filter.offset.data:
            filter.offset.data = 0
        if not filter.limit.data:
            filter.limit.data = 50
    
        if filter.previous.data:
            filter.offset.data = max(0, filter.offset.data - 50)
        if filter.next.data:
            filter.offset.data = min(count // 50 * 50, filter.offset.data + 50)
    
        q = q.offset(filter.offset.data)
        q = q.limit(filter.limit.data)
        users = q.all()
    
        return render_template('org_users_orgs.html', users=users, count=count, filter=filter, filter_errors=None)
    
    
    @app.route('/org/user/<int:id>/')
    def org_user(id: int):
        return render_template('not_implemented.html')
    
    
    @app.route('/org/user/<int:id>/edit')
    def org_user_edit(id: int):
        return render_template('not_implemented.html')
    
    
    @app.route('/org/user/new/', defaults={'type': None})
    @app.route('/org/user/new/<type>/')
    def org_user_new(type: Optional[str]):
        return render_template('not_implemented.html')