Commit d8a6b942cd8d519f09b0d2871f7a6692220e29ab

Authored by hitier
1 parent d086fdc1

More install doc

and changed cli commands names
@@ -3,6 +3,9 @@ @@ -3,6 +3,9 @@
3 ## Prérequis 3 ## Prérequis
4 4
5 - python3 5 - python3
  6 +- sqlite ( pour le développement et les tests unitaires )
  7 +- chrome-driver et chromium ( pour les tests unitaires)
  8 +- postgresql ou mariadb (pour la production)
6 9
7 ## Obtenir un répertoire fonctionnel 10 ## Obtenir un répertoire fonctionnel
8 11
@@ -13,14 +16,28 @@ @@ -13,14 +16,28 @@
13 16
14 python3 -m venv venv 17 python3 -m venv venv
15 source venv/bin/activate 18 source venv/bin/activate
  19 + pip install --upgrade pip
16 pip install -r requirements.txt 20 pip install -r requirements.txt
17 21
18 ### Configurer l'application 22 ### Configurer l'application
19 23
20 - cp resources/pdc_config.py .  
21 - $(EDITOR) pdc_config.py 24 + # D'abord les accés base de donnée
  25 + cp resources/db_config.py .
  26 + $(EDITOR) db_config.py
22 27
  28 + # Ensuite l'appli elle même (ce fichier est utilisable tel quel)
23 cp resources/flaskenv .flaskenv 29 cp resources/flaskenv .flaskenv
  30 + $(EDITOR) .flaskenv
  31 +
  32 +### Créer la base de données
  33 +
  34 +Utiliser l'outil de ligne de commande fourni avec l'application.
  35 +
  36 + flask pdc_db --help
  37 + flask pdc_db create_db # créer la structure
  38 + flask pdc_db feed_from_lesia # renseigner des agent depuis une base type lesia
  39 + flask pdc_db feed_periods # entrer des périodes
  40 + flask pdc_db feed_random_charges # entrer des charges aléatoires (une centaine)
24 41
25 42
26 ### Jouer les tests et exécuter un serveur local 43 ### Jouer les tests et exécuter un serveur local
@@ -34,17 +51,33 @@ @@ -34,17 +51,33 @@
34 # ouvrir un serveur sur localhost:5000 51 # ouvrir un serveur sur localhost:5000
35 flask run 52 flask run
36 53
37 -## Configurer apache 54 +## Configurer l'appli web avec apache
38 55
39 Les fichiers concernés: 56 Les fichiers concernés:
40 57
41 - pdc_web.wsgi 58 - pdc_web.wsgi
42 -- pdc_config.py 59 +- db_config.py
43 60
44 La procédure: 61 La procédure:
45 62
46 - mkdir /var/www/html/pdc-web 63 + # créer le répertoire pour le web
  64 + export WEB_DIR=/var/www/html/pdc-web
  65 + mkdir $WEB_DIR
  66 +
  67 + # le peupler avec le code
  68 + export GIT_DIR=/path/to/pdc_web/.git
  69 + export GIT_BRANCH=master
  70 + git --work-tree=$WEB_DIR --git-dir=$GIT_DIR checkout -f $GIT_BRANCH
  71 +
  72 + # le configurer : cf "Obtenir un répertoire fonctionnel"
  73 + cd $WEB_DIR
  74 + python -m venv venv
  75 + source venv/bin/activate
  76 + pip install -r requirements.txt
  77 + $(EDITOR) db_config.py .flaskenv ....
  78 +
47 79
  80 + # configurer le serveur web
48 cp ./resources/apache2-virtual-host.conf /etc/apache2/sites-available/pdc-web.conf 81 cp ./resources/apache2-virtual-host.conf /etc/apache2/sites-available/pdc-web.conf
49 $(EDITOR) /etc/apache2/sites-available/pdc-web.conf # éditer les paramètres 82 $(EDITOR) /etc/apache2/sites-available/pdc-web.conf # éditer les paramètres
50 a2ensite pdc-web 83 a2ensite pdc-web
app/__init__.py
@@ -18,12 +18,12 @@ def load_user(user_id): @@ -18,12 +18,12 @@ def load_user(user_id):
18 18
19 19
20 # Please, set a config file on top project dir 20 # Please, set a config file on top project dir
21 -# find example in ressources/pdc_config.py 21 +# see in ../pdc_config.py
22 try: 22 try:
23 from pdc_config import Config, ProdConfig, DevConfig, TestConfig 23 from pdc_config import Config, ProdConfig, DevConfig, TestConfig
24 except ImportError: 24 except ImportError:
25 # TODO: use logging system 25 # TODO: use logging system
26 - print("Please set an pdc_config.py file in you PYTHON_PATH") 26 + print("Please set a pdc_config.py file in you PYTHON_PATH")
27 print("See INSTALL.md for more info") 27 print("See INSTALL.md for more info")
28 sys.exit(-1) 28 sys.exit(-1)
29 29
app/commands/__init__.py
@@ -2,4 +2,6 @@ from flask import Blueprint @@ -2,4 +2,6 @@ from flask import Blueprint
2 2
3 bp = Blueprint('pdc_db', __name__) 3 bp = Blueprint('pdc_db', __name__)
4 4
  5 +bp.cli.short_help ="Database utilities for pdc app"
  6 +
5 from . import commands 7 from . import commands
6 \ No newline at end of file 8 \ No newline at end of file
app/commands/commands.py
@@ -2,6 +2,7 @@ import sys @@ -2,6 +2,7 @@ import sys
2 import click 2 import click
3 import random 3 import random
4 4
  5 +from sqlalchemy.exc import OperationalError
5 from sqlalchemy.sql import func 6 from sqlalchemy.sql import func
6 from sqlalchemy.ext.automap import automap_base 7 from sqlalchemy.ext.automap import automap_base
7 from sqlalchemy.orm import Session 8 from sqlalchemy.orm import Session
@@ -9,19 +10,29 @@ from sqlalchemy import create_engine @@ -9,19 +10,29 @@ from sqlalchemy import create_engine
9 10
10 from app.models import db, User, Agent, Service, Project, Capacity, Period, Charge 11 from app.models import db, User, Agent, Service, Project, Capacity, Period, Charge
11 12
12 -from db_config import mysql_uri 13 +from db_config import mysql_lesia_uri
13 14
14 from . import bp 15 from . import bp
15 16
16 17
17 @bp.cli.command("feed_from_lesia") 18 @bp.cli.command("feed_from_lesia")
18 def feed_from_lesia(): 19 def feed_from_lesia():
  20 + """ Feed db with agents from a lesia like mysql database.
  21 +
  22 + configure that database uri in the db_config.py file.
  23 + """
19 Base = automap_base() 24 Base = automap_base()
20 25
21 - engine = create_engine(mysql_uri) 26 + engine = create_engine(mysql_lesia_uri)
22 27
23 # reflect the tables 28 # reflect the tables
24 - Base.prepare(engine, reflect=True) 29 + try:
  30 + Base.prepare(engine, reflect=True)
  31 + except OperationalError:
  32 + # TODO: use logging facility instead
  33 + print("Please, configure the mysql database (see db_config.py)")
  34 + sys.exit(-1)
  35 +
25 36
26 # mapped classes are now created with names by default 37 # mapped classes are now created with names by default
27 # matching that of the table name. 38 # matching that of the table name.
@@ -58,6 +69,7 @@ def feed_from_lesia(): @@ -58,6 +69,7 @@ def feed_from_lesia():
58 69
59 @bp.cli.command("feed_periods") 70 @bp.cli.command("feed_periods")
60 def feed_periods(): 71 def feed_periods():
  72 + """ Fill in the periods name in the database. """
61 for y in range(2014, 2023): 73 for y in range(2014, 2023):
62 for s in ['S1', 'S2']: 74 for s in ['S1', 'S2']:
63 period_name = "{}_{}".format(y, s) 75 period_name = "{}_{}".format(y, s)
@@ -66,9 +78,10 @@ def feed_periods(): @@ -66,9 +78,10 @@ def feed_periods():
66 db.session.commit() 78 db.session.commit()
67 79
68 80
69 -@bp.cli.command("random_charges")  
70 -@click.option('--agent', '-a', 'agent', default=None)  
71 -def random_charges(agent): 81 +@bp.cli.command("feed_random_charges")
  82 +@click.option('--agent', '-a', 'agent', default=None, help="the agent id you want to charge")
  83 +def feed_random_charges(agent):
  84 + """ Randomly fill in the agents charges. """
72 for i in range(0, 100): 85 for i in range(0, 100):
73 if agent is None: 86 if agent is None:
74 agent_id = random.choice([i for (i,) in db.session.query(Agent.id).all()]) 87 agent_id = random.choice([i for (i,) in db.session.query(Agent.id).all()])
@@ -98,9 +111,10 @@ def random_charges(agent): @@ -98,9 +111,10 @@ def random_charges(agent):
98 db.session.commit() 111 db.session.commit()
99 112
100 113
101 -@bp.cli.command('delete_user') 114 +@bp.cli.command('user_delete')
102 @click.argument('user_id') 115 @click.argument('user_id')
103 -def delete_user(user_id): 116 +def user_delete(user_id):
  117 + """Delete the user by given id (see user_show_all")."""
104 user = User.query.get(user_id) 118 user = User.query.get(user_id)
105 db.session.delete(user) 119 db.session.delete(user)
106 db.session.commit() 120 db.session.commit()
@@ -108,26 +122,29 @@ def delete_user(user_id): @@ -108,26 +122,29 @@ def delete_user(user_id):
108 122
109 @bp.cli.command('create_db') 123 @bp.cli.command('create_db')
110 def create_db(): 124 def create_db():
  125 + """ Create the database structure."""
111 db.create_all() 126 db.create_all()
112 admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') 127 admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin')
113 db.session.add(admin) 128 db.session.add(admin)
114 db.session.commit() 129 db.session.commit()
115 130
116 131
117 -@bp.cli.command('add_user') 132 +@bp.cli.command('user_add')
118 @click.argument('email') 133 @click.argument('email')
119 @click.argument('name') 134 @click.argument('name')
120 @click.argument('login') 135 @click.argument('login')
121 @click.argument('password') 136 @click.argument('password')
122 -def add_user(email, name, login, password): 137 +def user_add(email, name, login, password):
  138 + """ Add a new user in db."""
123 user = User(email=email, name=name, login=login, password=password) 139 user = User(email=email, name=name, login=login, password=password)
124 db.session.add(user) 140 db.session.add(user)
125 db.session.commit() 141 db.session.commit()
126 print("added ", name) 142 print("added ", name)
127 143
128 144
129 -@bp.cli.command('show_all')  
130 -def show_all(): 145 +@bp.cli.command('user_show_all')
  146 +def user_show_all():
  147 + """ Show all users in db."""
131 print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email')) 148 print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email'))
132 print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15)) 149 print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15))
133 for user in User.query.all(): 150 for user in User.query.all():