From 9a61d14eb4dbead88e87caee08c65b91db688942 Mon Sep 17 00:00:00 2001 From: Doug Le Tough Date: Sun, 29 Oct 2017 08:31:14 +0100 Subject: [PATCH] "Beta release" --- .gitignore | 1 + data_migration.sh | 135 +++++++++++++++++++++++++++ static/scripts/tetalab.js | 4 +- static/style/style.css | 4 +- stock.sql | 77 +-------------- templates/componant.html | 6 +- templates/kit.html | 2 +- templates/result_componants.html | 8 +- templates/result_kit_componants.html | 6 +- templates/result_kits.html | 12 ++- templates/result_providers.html | 6 +- templates/wip.html | 6 +- tetastock.py | 75 +++++++-------- 13 files changed, 206 insertions(+), 136 deletions(-) create mode 100644 data_migration.sh diff --git a/.gitignore b/.gitignore index 16d837e..2546afb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.swp *.pyc config.tetalab.py +pg_* diff --git a/data_migration.sh b/data_migration.sh new file mode 100644 index 0000000..0c006c8 --- /dev/null +++ b/data_migration.sh @@ -0,0 +1,135 @@ +#!/bin/bash +DB_SRC_HOST=marian.local.tetalab.org +DB_SRC=/var/www/stock.tetalab.org/stock +DB_DST=tetastock +DB_DST_USER=tetastock +DB_DST_HOST=sonny.local.tetalab.org + +PSQL_OPTS="-t -U ${DB_DST_USER} -h ${DB_DST_HOST} -d ${DB_DST}" + +function log { + echo -e "\033[93m$@\033[0m" +} + +function red { + echo -e "\033[91m$@\033[0m" +} + +function out { + echo -e "\033[91m$@\033[0m" + exit 1 +} + +log "[+] Suppression des données existantes" +psql -t ${PSQL_OPTS} -c "delete from stock_kit_compositions where id > 0;" +psql -t ${PSQL_OPTS} -c "delete from stock_componants where id > 0;" +psql -t ${PSQL_OPTS} -c "delete from stock_kits where id > 0;" + +log "[+] Récupération des composants" + +IFS_BAK=${IFS} +IFS=$'\n' +for LINE in $(ssh ${DB_SRC_HOST} sqlite3 ${DB_SRC} "'select distinct composants.id, composants.groupe, composants.type, composants.taille, composants.couleur, composants.valeur, composants.quantité , composants.emplacement from composants order by composants.groupe;'") +do + IFS=${IFS_BAK} + COMP_ID=$(echo ${LINE} | awk -F'|' '{print $1}') + RAND=$(head -n40 /dev/urandom | md5sum | cut -d' ' -f1) + REF=$(echo ${LINE} | awk -F'|' '{print $2}') + REF=$(echo "${REF:0:3}-${RAND:0:5}" | tr [a-z] [A-Z] | sed 's/É/E/') + GROUP=$(echo ${LINE} | awk -F'|' '{print $2}' | sed 's/^ *//;s/ *$//') + TYPE=$(echo ${LINE} | awk -F'|' '{print $3}' | sed 's/^ *//;s/ *$//') + SIZE=$(echo ${LINE} | awk -F'|' '{print $4}' | sed 's/^ *//;s/ *$//') + COLOR=$(echo ${LINE} | awk -F'|' '{print $5}' | sed 's/^ *//;s/ *$//') + if [ "${COLOR}" == "0" ]; then + COLOR='' + fi + VALUE=$(echo ${LINE} | awk -F'|' '{print $6}' | sed 's/^ *//;s/ *$//') + QUANTITY=$(echo ${LINE} | awk -F'|' '{print $7}' | sed 's/^ *//;s/ *$//') + DESIGNATION=$(echo "${GROUP} ${TYPE} ${SIZE} ${COLOR} ${VALUE}" | sed 's/^ *//;s/ *$//;s/ +*/ /g;s/ */ /g') + if [ ${#QUANTITY} -eq 0 ]; then + QUANTITY=0 + fi + PLACE=$(echo ${LINE} | awk -F'|' '{print $8}') + log " [+] Vérification de l'existence d'un doublon avec ${DESIGNATION}" + SQLREQ="select id from stock_componants where designation='${DESIGNATION}';" + EXIST=$(psql ${PSQL_OPTS} -c "${SQLREQ}") + if [ ! ${#EXIST} -eq 0 ]; then + red " => Composant ${EXIST} a déjà une désignation égale à ${DESIGNATION}" + continue + fi + log " [+] Enregistrement du nouveau composant: ${REF} / ${DESIGNATION}" + SQLREQ="insert into stock_componants (reference, designation, place, quantity) values ('${REF}', '${DESIGNATION}', '${PLACE}', ${QUANTITY});" + psql ${PSQL_OPTS} -c "${SQLREQ}" || out "${SQLREQ}" + IFS=$'\n' +done + +log "[+] Récupération des ID des kits" +for KIT_ID in $(ssh ${DB_SRC_HOST} sqlite3 ${DB_SRC} "'select distinct id from kit;'") +do + IFS=${IFS_BAK} + # On recupere le kit + log " [+] Récupération du nom pour KIT_ID: ${KIT_ID}" + SQLREQ="select description from kit where ID=${KIT_ID};" + NAME=$(ssh ${DB_SRC_HOST} sqlite3 ${DB_SRC} "'${SQLREQ}'") + if [ ${#NAME} -lt 1 ]; then + log "[-] Trace: ${SQLREQ}" + out "Nom introuvable pour ${KIT_ID}" + fi + DESIGNATION=${NAME} + # On cree le kit + log " [+] Enregistrement du kit ${NAME} / ${DESIGNATION}" + SQLREQ="insert into stock_kits (name, designation) values ('${NAME}', '${DESIGNATION}');" + psql ${PSQL_OPTS} -c "${SQLREQ}" || out "${SQLREQ}" + log " [+] Récupération du nouvel ID du kit" + # On recupere l'ID du nouveau kit + SQLREQ="select id from stock_kits where name='${NAME}' and designation='${DESIGNATION}';" + NKIT_ID=$(psql ${PSQL_OPTS} -c "${SQLREQ}") + if [ ${#NKIT_ID} -lt 1 ]; then + log "[-] Trace: ${SQLREQ}" + out "Pas de NKIT_ID pour ${NAME}" + fi + log " => ${NKIT_ID}" + # On recupere les composants du kit + log " [+] Récupération de la liste des composants du kit" + SQLREQ="select id_composant, quantite from composants_kit where id_kit='${KIT_ID}';" + RECS=$(ssh ${DB_SRC_HOST} sqlite3 ${DB_SRC} "'${SQLREQ}'") + IFS=$'\n' + for REC in ${RECS} + do + IFS=${IFS_BAK} + COMP_ID=$(echo ${REC} | awk -F'|' '{print $1}') + QUANTITY=$(echo ${REC} | awk -F'|' '{print $2}') + IFS=${IFS_BAK} + log " [+] Récupération du composant: ${COMP_ID}" + SQLREQ="select composants.id, composants.groupe, composants.type, composants.taille, composants.couleur, composants.valeur, composants.quantité , composants.emplacement from composants where id=${COMP_ID} order by composants.groupe;" + COMP=$(ssh ${DB_SRC_HOST} sqlite3 ${DB_SRC} "'${SQLREQ}'") + if [ ${#COMP} -lt 1 ]; then + red "[-] Trace: ${SQLREQ}" + red "Pas de composant avec ID ${COMP_ID}" + continue + fi + GROUP=$(echo ${COMP} | awk -F'|' '{print $2}' | sed 's/^ *//;s/ *$//') + TYPE=$(echo ${COMP} | awk -F'|' '{print $3}' | sed 's/^ *//;s/ *$//') + SIZE=$(echo ${COMP} | awk -F'|' '{print $4}' | sed 's/^ *//;s/ *$//') + COLOR=$(echo ${COMP} | awk -F'|' '{print $5}' | sed 's/^ *//;s/ *$//') + if [ "${COLOR}" == "0" ]; then + COLOR='' + fi + VALUE=$(echo ${COMP} | awk -F'|' '{print $6}' | sed 's/^ *//;s/ *$//') + DESIGNATION=$(echo "${GROUP} ${TYPE} ${SIZE} ${COLOR} ${VALUE}" | sed 's/^ *//;s/ *$//;s/ +*/ /g;s/ */ /g') + log " [+] Récupération du nouvel ID du composant" + SQLREQ="select ID from stock_componants where designation='${DESIGNATION}';" + NCOMP_ID=$(psql ${PSQL_OPTS} -c "${SQLREQ}") + if [ ${#NCOMP_ID} -lt 1 ]; then + log "[-] Trace: ${SQLREQ}" + out "Pas de composant avec NCOMP_ID ${NCOMP_ID}" + fi + log " [+] ${NCOMP_ID}" + log " [+] Enregistrement du composant ${NCOMP_ID} dans la composition du kit ${NKIT_ID} (q: ${QUANTITY})" + SQLREQ="insert into stock_kit_compositions (kit_id, componant_id, quantity) values (${NKIT_ID}, ${NCOMP_ID}, ${QUANTITY})" + psql ${PSQL_OPTS} -c "${SQLREQ}" || out "${SQLREQ}" + IFS=$'\n' + done + IFS=$'\n' +done +IFS=${IFS_BAK} diff --git a/static/scripts/tetalab.js b/static/scripts/tetalab.js index a26f29e..9a95e8f 100644 --- a/static/scripts/tetalab.js +++ b/static/scripts/tetalab.js @@ -67,6 +67,7 @@ function login() { function logout() { setcookie('token', '', 30); + setcookie('session', '', 30); document.location='/'; } @@ -140,7 +141,6 @@ function c_next_page(nexthop) { update_componants(); } - // Search componants function search_componants_by_reference(obj) { setcookie('c_reference', obj.value, 30); @@ -651,7 +651,7 @@ function add_kit_componant(kit_id, componant_id) { } }; - xhttp.open('POST', '/kits/add/'+kit_id+'/'+componant_id, true); + xhttp.open('POST', '/kits/composition/add/'+kit_id, true); xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhttp.send(); } diff --git a/static/style/style.css b/static/style/style.css index 6d6e276..40ba174 100644 --- a/static/style/style.css +++ b/static/style/style.css @@ -162,7 +162,7 @@ div.error { background-repeat: no-repeat; background-position: center center; background-color: #000000; - height: 476px; + height: 531px; padding: 10px; text-align: center; font-size: 70px; @@ -175,7 +175,7 @@ div.wip { background-repeat: no-repeat; background-position: center center; background-color: #000000; - height: 476px; + height: 531px; padding: 10px; text-align: center; font-size: 70px; diff --git a/stock.sql b/stock.sql index 1cee582..64a6f99 100644 --- a/stock.sql +++ b/stock.sql @@ -34,7 +34,7 @@ CREATE TABLE stock_providers ( CREATE TABLE stock_componants ( id serial primary key, reference varchar(20) unique not NULL, - designation varchar(50) not NULL, + designation varchar(100) not NULL, last_price NUMERIC not NULL default 0, mean_price NUMERIC not NULL default 0, quantity NUMERIC not NULL default 0, @@ -70,77 +70,4 @@ insert into stock_users (mail, password, name) values ('doug.letough@free.fr', '$2a$08$578910202124252729313uTyggq4ANEjMljcClFriOqcsttB2fnAW', 'Doug Le Tough'); insert into stock_users (mail, password, name) values ('doug@redatomik.org', '$2a$08$578910202124252729313uTyggq4ANEjMljcClFriOqcsttB2fnAW', 'Doug Le Tough'); - -insert into stock_providers (name, address, mail, url, comment) - values ('Tous', 'N/A', 'N/A', 'N/A', 'N/A'); -insert into stock_providers (name, address, mail, url, comment) - values ('Aucun', 'N/A', 'N/A', 'N/A', 'N/A'); -insert into stock_providers (name, address, mail, url, comment) - values ('China Elec Co', 'Beijing', 'sales@chinaelecco.cc', 'https://chinaelecco.cc', 'Pas cher mais délai excessif et composants pourris'); - -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-01', 'Resistance 10KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Lor-01', '1 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-02', 'Resistance 11KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-03', 'Resistance 12KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-04', 'Resistance 13KΩ', 13.34, 12.42, 73, 0, 'B 43', 2); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-05', 'Resistance 14KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Lor-02', '2 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-06', 'Resistance 15KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-07', 'Resistance 16KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-08', 'Resistance 17KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-09', 'Resistance 18KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Lor-03', '3 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-10', 'Resistance 19KΩ', 13.34, 12.42, 73, 0, 'B 43', 2); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-11', 'Resistance 20KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-12', 'Resistance 21KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-13', 'Resistance 22KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Lor-04', '4 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-14', 'Resistance 23KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-15', 'Resistance 24KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-16', 'Resistance 25KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-17', 'Resistance 26KΩ', 13.34, 12.42, 73, 0, 'B 43', 2); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Lor-05', '5 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-18', 'Resistance 27KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-19', 'Resistance 28KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); -insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id) - values ('Res-20', 'Resistance 29KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); - -insert into stock_kits (name, designation) values ('Kit_test', 'Kit de test'); -insert into stock_kits (name, designation) values ('Kit2_test2', 'Kit de test 2'); -insert into stock_kits (name, designation) values ('MF_K', 'Mother fucking kit'); - -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (1, 5, 1); -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (1, 8, 2); -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (1, 10, 2); -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (1, 12, 7); - -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 5, 2); -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 4, 1); -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 9, 3); -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 22, 2); -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 1, 5); -insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 14, 8); +insert into stock_users (mail, password, name) values ('test', '$2b$08$OkfihuGRyLdpftBpGhnpeeeUhUTQS0oXvR2NFByC.65XCKKvPBWHS'); diff --git a/templates/componant.html b/templates/componant.html index 3dc5da9..d2aa4cf 100644 --- a/templates/componant.html +++ b/templates/componant.html @@ -54,9 +54,9 @@ id='designation' type='text' onchange='javascript:update_componant(this, {{ componant.id }}, "text");' - maxlength='50' - title='Désignation (max. 50)' - placeholder='Désignation (max. 50)' + maxlength='100' + title='Désignation (max. 100)' + placeholder='Désignation (max. 100)' value='{{ componant.designation }}' />
diff --git a/templates/kit.html b/templates/kit.html index ec4758c..7e58db5 100644 --- a/templates/kit.html +++ b/templates/kit.html @@ -4,7 +4,7 @@ {% endblock %} {% block title %}Éditer un kit{% endblock %} {% block top_menu %} - Gérer les kits + Gérer les kits Éditer un kit {% endblock %} {% block left_menu %} diff --git a/templates/result_componants.html b/templates/result_componants.html index 92a5f90..ff165eb 100644 --- a/templates/result_componants.html +++ b/templates/result_componants.html @@ -16,10 +16,10 @@ {% set row_class = cycler('odd', 'even') %} {% for componant in componants %}
- {{ componant.reference }} - {{ componant.designation }} - {{ componant.quantity }} - {{ componant.place }} + {{ componant.reference }} + {{ componant.designation }} + {{ componant.quantity }} + {{ componant.place }}
- {{ componant.reference }} - {{ componant.designation }} + {{ componant.reference }} + {{ componant.designation }} {{ componant.quantity }} - {{ componant.place }} + {{ componant.place }}
- + +
{% set row_class = cycler('odd', 'even') %} {% for kit in kits %}
- {{ kit.name }} - {{ kit.designation }} + {{ kit.name }} + {{ kit.designation }} + {% set sclass='red' %} + {% if kit. max_kit > 1 %} + {% set sclass='' %} + {% endif %} + {{ kit.max_kit }}
{{ provider.name }} - {{ provider.address }} - {{ provider.mail }} - {{ provider.url }} + {{ provider.address }} + {{ provider.mail }} + {{ provider.url }}
- Référentiel Infrastructure Tetalab - {% block title %}How The Fuck{% endblock %} + Tetastock @@ -10,8 +10,8 @@
-