1
0
forked from ScoDoc/ScoDoc

L'admin peut cacher/montrer des départements

This commit is contained in:
Emmanuel Viennet 2022-01-04 18:10:14 +01:00
parent 9b9b2f270b
commit be2227f8a3
5 changed files with 33 additions and 15 deletions

View File

@ -245,7 +245,7 @@ class DeptForm(FlaskForm):
def _make_dept_id_name():
"""Cette section assute que tous les départements sont traités (y compris ceux qu'ont pas de logo au départ)
"""Cette section assure que tous les départements sont traités (y compris ceux qu'ont pas de logo au départ)
et détermine l'ordre d'affichage des DeptForm (GLOBAL d'abord, puis par ordre alpha de nom de département)
-> [ (None, None), (dept_id, dept_name)... ]"""
depts = [(None, GLOBAL)]

View File

@ -31,16 +31,10 @@ Formulaires création département
from flask import flash, url_for, redirect, render_template
from flask_wtf import FlaskForm
from wtforms import SelectField, SubmitField, FormField, validators, FieldList
from wtforms.fields.simple import StringField, HiddenField
from wtforms import SubmitField, validators
from wtforms.fields.simple import StringField, BooleanField
from app import AccessDenied
from app.models import Departement
from app.models import ScoPreference
from app.models import SHORT_STR_LEN
from app.scodoc import sco_utils as scu
from flask_login import current_user
class CreateDeptForm(FlaskForm):
@ -60,5 +54,9 @@ class CreateDeptForm(FlaskForm):
validators.DataRequired("acronyme du département requis"),
],
)
visible = BooleanField(
"Visible sur page d'accueil",
default=True,
)
submit = SubmitField("Valider")
cancel = SubmitField("Annuler", render_kw={"formnovalidate": True})

View File

@ -49,11 +49,11 @@ class Departement(db.Model):
return dept
def create_dept(acronym: str) -> Departement:
def create_dept(acronym: str, visible=True) -> Departement:
"Create new departement"
from app.models import ScoPreference
departement = Departement(acronym=acronym)
departement = Departement(acronym=acronym, visible=visible)
p1 = ScoPreference(name="DeptName", value=acronym, departement=departement)
db.session.add(p1)
db.session.add(departement)

View File

@ -12,11 +12,21 @@
<ul class="main">
{% for dept in depts %}
{% if dept.visible or current_user.is_administrator() %}
<li>
<a class="stdlink {{'link_accessible' if current_user.has_permission(Permission.ScoView, dept=dept.acronym) else 'link_unauthorized'}}"
href="{{url_for('scolar.index_html', scodoc_dept=dept.acronym)}}">Département
{{dept.preferences.filter_by(name="DeptName").first().value}}</a>
{% if current_user.is_administrator() %}
<span class="dept_visibility">
{% if dept.visible %}visible{% else %}caché aux utilisateurs{% endif %}
<a href="{{ url_for('scodoc.toggle_dept_vis', dept_id=dept.id) }}">
{% if dept.visible %}cacher{% else %}rendre visible{% endif %}
</a>
</span>
{% endif %}
</li>
{% endif %}
{% else %}
<li>
<b>Aucun département défini !</b>

View File

@ -53,6 +53,7 @@ from wtforms.fields.simple import BooleanField, StringField, TextAreaField, Hidd
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
import app
from app import db
from app.forms.main import config_forms
from app.forms.main.create_dept import CreateDeptForm
from app.models import Departement, Identite
@ -82,9 +83,7 @@ from PIL import Image as PILImage
@bp.route("/ScoDoc/index")
def index():
"Page d'accueil: liste des départements"
depts = (
Departement.query.filter_by(visible=True).order_by(Departement.acronym).all()
)
depts = Departement.query.filter_by().order_by(Departement.acronym).all()
return render_template(
"scodoc.html",
title=sco_version.SCONAME,
@ -108,7 +107,7 @@ def create_dept():
if request.method == "POST" and form.cancel.data: # cancel button
return redirect(url_for("scodoc.index"))
if form.validate_on_submit():
departements.create_dept(form.acronym.data)
departements.create_dept(form.acronym.data, visible=form.visible.data)
flash(f"Département {form.acronym.data} créé.")
return redirect(url_for("scodoc.index"))
return render_template(
@ -118,6 +117,17 @@ def create_dept():
)
@bp.route("/ScoDoc/toggle_dept_vis/<dept_id>", methods=["GET", "POST"])
@admin_required
def toggle_dept_vis(dept_id):
"""Cache ou rend visible un dept"""
dept = Departement.query.get_or_404(dept_id)
dept.visible = not dept.visible
db.session.add(dept)
db.session.commit()
return redirect(url_for("scodoc.index"))
@bp.route("/ScoDoc/table_etud_in_accessible_depts", methods=["POST"])
@login_required
def table_etud_in_accessible_depts():