ScoDoc/scodoc.py

242 lines
6.4 KiB
Python
Raw Normal View History

2021-05-29 18:22:51 +02:00
# -*- coding: UTF-8 -*
"""Application Flask: ScoDoc
"""
from __future__ import print_function
2021-06-24 10:59:03 +02:00
import os
2021-05-29 18:22:51 +02:00
from pprint import pprint as pp
import sys
2021-05-29 18:22:51 +02:00
import click
import flask
2021-06-24 10:59:03 +02:00
from flask.cli import with_appcontext
2021-08-10 12:57:38 +02:00
from app import create_app, cli, db
from app import initialize_scodoc_database
from app import clear_scodoc_cache
2021-06-24 10:59:03 +02:00
2021-05-29 18:22:51 +02:00
from app.auth.models import User, Role, UserRole
from app import models
from app.models import ScoPreference
2021-08-09 11:33:04 +02:00
2021-06-14 18:08:52 +02:00
from app.views import notes, scolar, absences
import tools
2021-05-29 18:22:51 +02:00
from config import RunningConfig
2021-05-29 18:22:51 +02:00
app = create_app(RunningConfig)
2021-05-29 18:22:51 +02:00
cli.register(app)
@app.shell_context_processor
def make_shell_context():
from app.scodoc import notesdb as ndb
from app.scodoc import sco_utils as scu
from flask_login import login_user, logout_user, current_user
import app as mapp # le package app
2021-05-29 18:22:51 +02:00
return {
"db": db,
"User": User,
"Role": Role,
"UserRole": UserRole,
2021-06-14 18:08:52 +02:00
"notes": notes,
"scolar": scolar,
"ndb": ndb,
"scu": scu,
2021-05-29 18:22:51 +02:00
"pp": pp,
"flask": flask,
"current_app": flask.current_app,
"current_user": current_user,
"login_user": login_user,
"logout_user": logout_user,
"ctx": app.test_request_context(),
"models": models,
"mapp": mapp,
2021-05-29 18:22:51 +02:00
}
# ctx.push()
# login_user(admin)
2021-05-29 18:22:51 +02:00
@app.cli.command()
def sco_db_init(): # sco-db-init
"""Initialize the database.
Starts from an existing database and create all
the necessary SQL tables and functions.
"""
2021-08-19 23:43:14 +02:00
if not app.config.get("SCODOC_ADMIN_MAIL"):
sys.stderr.write(
"""La variable SCODOC_ADMIN_MAIL n'est pas positionnée: vérifier votre .env"""
)
return 100
initialize_scodoc_database()
2021-05-29 18:22:51 +02:00
@app.cli.command()
def user_db_clear():
"""Erase all users and roles from the database !"""
click.echo("Erasing the users database !")
_clear_users_db()
2021-05-29 18:22:51 +02:00
def _clear_users_db():
2021-05-29 18:22:51 +02:00
"""Erase (drop) all tables of users database !"""
click.confirm(
"This will erase all users and roles.\nAre you sure you want to continue?",
abort=True,
)
2021-05-29 18:22:51 +02:00
db.reflect()
try:
db.session.query(UserRole).delete()
db.session.query(User).delete()
db.session.query(User).delete()
db.session.commit()
except:
db.session.rollback()
raise
@app.cli.command()
@click.argument("username")
@click.argument("role")
@click.argument("dept")
2021-08-08 09:50:10 +02:00
@click.option("-n", "--nom", "nom")
@click.option("-p", "--prenom", "prenom")
def user_create(username, role, dept, nom=None, prenom=None): # user-create
"Create a new user"
r = Role.get_named_role(role)
if not r:
sys.stderr.write("user_create: role {r} does not exists\n".format(r=role))
return 1
u = User.query.filter_by(user_name=username).first()
if u:
sys.stderr.write("user_create: user {u} already exists\n".format(u=u))
return 2
if dept == "@all":
dept = None
2021-08-08 09:50:10 +02:00
u = User(user_name=username, dept=dept, nom=nom, prenom=prenom)
u.add_role(r, dept)
db.session.add(u)
db.session.commit()
click.echo(
"created user, login: {u.user_name}, with role {r} in dept. {dept}".format(
u=u, r=r, dept=dept
)
)
@app.cli.command()
@click.argument("username")
@click.password_option()
def user_password(username, password=None): # user-password
"Set (or change) user's password"
if not password:
sys.stderr.write("user_password: missing password")
return 1
u = User.query.filter_by(user_name=username).first()
if not u:
sys.stderr.write("user_password: user {} does not exists".format(username))
return 1
u.set_password(password)
db.session.add(u)
db.session.commit()
click.echo("changed password for user {}".format(u))
2021-06-24 10:59:03 +02:00
@app.cli.command()
@click.argument("dept")
def delete_dept(dept): # delete-dept
2021-08-13 00:34:58 +02:00
"""Delete existing departement"""
from app.scodoc import notesdb as ndb
from app.scodoc import sco_dept
2021-08-13 00:34:58 +02:00
click.confirm(
f"""Attention: Cela va effacer toutes les données du département {dept}
(étudiants, notes, formations, etc)
Voulez-vous vraiment continuer ?
""",
abort=True,
)
db.reflect()
ndb.open_db_connection()
2021-08-13 00:34:58 +02:00
d = models.Departement.query.filter_by(acronym=dept).first()
if d is None:
sys.stderr.write(f"Erreur: le departement {dept} n'existe pas !\n")
2021-08-13 00:34:58 +02:00
return 2
sco_dept.delete_dept(d.id)
2021-08-13 00:34:58 +02:00
db.session.commit()
2021-06-24 10:59:03 +02:00
return 0
@app.cli.command()
@click.argument("dept")
def create_dept(dept): # create-dept
2021-06-24 10:59:03 +02:00
"Create new departement"
2021-08-13 00:34:58 +02:00
d = models.Departement(acronym=dept)
p1 = ScoPreference(name="DeptName", value=dept, departement=d)
db.session.add(p1)
2021-08-13 00:34:58 +02:00
db.session.add(d)
db.session.commit()
2021-06-24 10:59:03 +02:00
return 0
@app.cli.command()
@click.argument("filename")
@with_appcontext
def test_interactive(filename=None):
"Run interactive test"
import flask_login
from app import decorators
click.echo("Executing {}".format(filename))
with app.test_request_context(""):
u = User.query.first()
flask_login.login_user(u)
REQUEST = decorators.ZRequest()
exec(open(filename).read())
click.echo("Done.")
2021-07-05 00:07:17 +02:00
@app.cli.command()
@with_appcontext
def import_scodoc7_users(): # import-scodoc7-users
2021-08-25 23:51:07 +02:00
"""Import used defined in ScoDoc7 postgresql database into ScoDoc 9
2021-07-05 00:07:17 +02:00
The old database SCOUSERS must be alive and readable by the current user.
2021-07-27 16:07:03 +02:00
This script is typically run as unix user "scodoc".
The original SCOUSERS database is left unmodified.
2021-07-05 00:07:17 +02:00
"""
messages = tools.import_scodoc7_user_db()
click.echo("----")
click.echo(f"import terminé: {len(messages)} warnings\n")
click.echo("\n".join(messages) + "\n")
@app.cli.command()
@click.argument("dept")
@click.argument("dept_db_name")
@with_appcontext
def import_scodoc7_dept(dept: str, dept_db_name: str): # import-scodoc7-dept
"""Import département ScoDoc 7: dept: InfoComm, dept_db_name: SCOINFOCOMM"""
dept_db_uri = f"postgresql:///{dept_db_name}"
tools.import_scodoc7_dept(dept, dept_db_uri)
2021-07-29 10:30:13 +02:00
@app.cli.command()
@with_appcontext
def clear_cache(): # clear-cache
"""Clear ScoDoc cache
This cache (currently Redis) is persistent between invocation
and it may be necessary to clear it during developement or tests.
"""
2021-08-10 12:57:38 +02:00
clear_scodoc_cache()
2021-07-29 10:30:13 +02:00
click.echo("Redis caches flushed.")