from app.utils.priv_required import priv_required
from flask_wtf import FlaskForm
from wtforms import SubmitField
from app.models.user import Member, GroupMember, Group, GroupPrivilege
from app.models.priv import SpecialPrivilege
from app.utils.render import render
from app import app, db
import yaml
import os


@app.route('/admin/groupes', methods=['GET', 'POST'])
@priv_required('misc.admin-panel')
def adm_groups():
    # Users with either groups or special privileges
    users_groups = Member.query.join(GroupMember)
    users_special = Member.query \
        .join(SpecialPrivilege, Member.id == SpecialPrivilege.member_id)
    users = users_groups.union(users_special)
    users = sorted(users, key = lambda x: x.name)

    groups = Group.query.all()
    return render('admin/groups_privileges.html', users=users, groups=groups)
