From d8a6b942cd8d519f09b0d2871f7a6692220e29ab Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Tue, 16 Mar 2021 16:26:01 +0100 Subject: [PATCH] More install doc --- INSTALL.md | 43 ++++++++++++++++++++++++++++++++++++++----- app/__init__.py | 4 ++-- app/commands/__init__.py | 2 ++ app/commands/commands.py | 41 +++++++++++++++++++++++++++++------------ 4 files changed, 71 insertions(+), 19 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 4e5820f..a5569e7 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -3,6 +3,9 @@ ## Prérequis - python3 +- sqlite ( pour le développement et les tests unitaires ) +- chrome-driver et chromium ( pour les tests unitaires) +- postgresql ou mariadb (pour la production) ## Obtenir un répertoire fonctionnel @@ -13,14 +16,28 @@ python3 -m venv venv source venv/bin/activate + pip install --upgrade pip pip install -r requirements.txt ### Configurer l'application - cp resources/pdc_config.py . - $(EDITOR) pdc_config.py + # D'abord les accés base de donnée + cp resources/db_config.py . + $(EDITOR) db_config.py + # Ensuite l'appli elle même (ce fichier est utilisable tel quel) cp resources/flaskenv .flaskenv + $(EDITOR) .flaskenv + +### Créer la base de données + +Utiliser l'outil de ligne de commande fourni avec l'application. + + flask pdc_db --help + flask pdc_db create_db # créer la structure + flask pdc_db feed_from_lesia # renseigner des agent depuis une base type lesia + flask pdc_db feed_periods # entrer des périodes + flask pdc_db feed_random_charges # entrer des charges aléatoires (une centaine) ### Jouer les tests et exécuter un serveur local @@ -34,17 +51,33 @@ # ouvrir un serveur sur localhost:5000 flask run -## Configurer apache +## Configurer l'appli web avec apache Les fichiers concernés: - pdc_web.wsgi -- pdc_config.py +- db_config.py La procédure: - mkdir /var/www/html/pdc-web + # créer le répertoire pour le web + export WEB_DIR=/var/www/html/pdc-web + mkdir $WEB_DIR + + # le peupler avec le code + export GIT_DIR=/path/to/pdc_web/.git + export GIT_BRANCH=master + git --work-tree=$WEB_DIR --git-dir=$GIT_DIR checkout -f $GIT_BRANCH + + # le configurer : cf "Obtenir un répertoire fonctionnel" + cd $WEB_DIR + python -m venv venv + source venv/bin/activate + pip install -r requirements.txt + $(EDITOR) db_config.py .flaskenv .... + + # configurer le serveur web cp ./resources/apache2-virtual-host.conf /etc/apache2/sites-available/pdc-web.conf $(EDITOR) /etc/apache2/sites-available/pdc-web.conf # éditer les paramètres a2ensite pdc-web diff --git a/app/__init__.py b/app/__init__.py index e0d3eb9..bfa397f 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -18,12 +18,12 @@ def load_user(user_id): # Please, set a config file on top project dir -# find example in ressources/pdc_config.py +# see in ../pdc_config.py try: from pdc_config import Config, ProdConfig, DevConfig, TestConfig except ImportError: # TODO: use logging system - print("Please set an pdc_config.py file in you PYTHON_PATH") + print("Please set a pdc_config.py file in you PYTHON_PATH") print("See INSTALL.md for more info") sys.exit(-1) diff --git a/app/commands/__init__.py b/app/commands/__init__.py index 153a30c..ac3e5ab 100644 --- a/app/commands/__init__.py +++ b/app/commands/__init__.py @@ -2,4 +2,6 @@ from flask import Blueprint bp = Blueprint('pdc_db', __name__) +bp.cli.short_help ="Database utilities for pdc app" + from . import commands \ No newline at end of file diff --git a/app/commands/commands.py b/app/commands/commands.py index fa5fdd9..911c9ea 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -2,6 +2,7 @@ import sys import click import random +from sqlalchemy.exc import OperationalError from sqlalchemy.sql import func from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session @@ -9,19 +10,29 @@ from sqlalchemy import create_engine from app.models import db, User, Agent, Service, Project, Capacity, Period, Charge -from db_config import mysql_uri +from db_config import mysql_lesia_uri from . import bp @bp.cli.command("feed_from_lesia") def feed_from_lesia(): + """ Feed db with agents from a lesia like mysql database. + + configure that database uri in the db_config.py file. + """ Base = automap_base() - engine = create_engine(mysql_uri) + engine = create_engine(mysql_lesia_uri) # reflect the tables - Base.prepare(engine, reflect=True) + try: + Base.prepare(engine, reflect=True) + except OperationalError: + # TODO: use logging facility instead + print("Please, configure the mysql database (see db_config.py)") + sys.exit(-1) + # mapped classes are now created with names by default # matching that of the table name. @@ -58,6 +69,7 @@ def feed_from_lesia(): @bp.cli.command("feed_periods") def feed_periods(): + """ Fill in the periods name in the database. """ for y in range(2014, 2023): for s in ['S1', 'S2']: period_name = "{}_{}".format(y, s) @@ -66,9 +78,10 @@ def feed_periods(): db.session.commit() -@bp.cli.command("random_charges") -@click.option('--agent', '-a', 'agent', default=None) -def random_charges(agent): +@bp.cli.command("feed_random_charges") +@click.option('--agent', '-a', 'agent', default=None, help="the agent id you want to charge") +def feed_random_charges(agent): + """ Randomly fill in the agents charges. """ for i in range(0, 100): if agent is None: agent_id = random.choice([i for (i,) in db.session.query(Agent.id).all()]) @@ -98,9 +111,10 @@ def random_charges(agent): db.session.commit() -@bp.cli.command('delete_user') +@bp.cli.command('user_delete') @click.argument('user_id') -def delete_user(user_id): +def user_delete(user_id): + """Delete the user by given id (see user_show_all").""" user = User.query.get(user_id) db.session.delete(user) db.session.commit() @@ -108,26 +122,29 @@ def delete_user(user_id): @bp.cli.command('create_db') def create_db(): + """ Create the database structure.""" db.create_all() admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') db.session.add(admin) db.session.commit() -@bp.cli.command('add_user') +@bp.cli.command('user_add') @click.argument('email') @click.argument('name') @click.argument('login') @click.argument('password') -def add_user(email, name, login, password): +def user_add(email, name, login, password): + """ Add a new user in db.""" user = User(email=email, name=name, login=login, password=password) db.session.add(user) db.session.commit() print("added ", name) -@bp.cli.command('show_all') -def show_all(): +@bp.cli.command('user_show_all') +def user_show_all(): + """ Show all users in db.""" print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email')) print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15)) for user in User.query.all(): -- libgit2 0.21.2