diff --git a/static/images/save.png b/static/images/save.png new file mode 100644 index 0000000..1e981f3 Binary files /dev/null and b/static/images/save.png differ diff --git a/static/scripts/tetalab.js b/static/scripts/tetalab.js index 0061ee9..960107c 100644 --- a/static/scripts/tetalab.js +++ b/static/scripts/tetalab.js @@ -15,6 +15,33 @@ function setcookie(cname, cvalue, exdays) { document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } +function getcookie(cname) { + var value = "; " + document.cookie; + var parts = value.split("; " + cname + "="); + if (parts.length == 2) return parts.pop().split(";").shift(); +} + +/////////////////////////////////////////// +// Eye candies +/////////////////////////////////////////// + +function invalid_input(obj) { + obj.style.backgroundColor = light_red; + setTimeout( function() { + obj.style.backgroundColor = base_bg; + } + , 2000); +} + +function valid_input(obj) { + obj.style.backgroundColor = light_green; + obj.style.borderColor = base_border; + setTimeout( function() { + obj.style.backgroundColor = base_bg; + } + , 2000); +} + /////////////////////////////////////////// // Update result /////////////////////////////////////////// @@ -26,18 +53,16 @@ function update_componants() { }; xhttp.onload = function(){ - if (xhttp.status == 200) { - var response = xhttp.responseText; - obj.innerHTML = response; - return; + if (xhttp.status != 200) { + obj.innerHTML = "Erreur lors de la mise à jour de la liste (2)" } - obj.innerHTML = "Erreur lors de la mise à jour de la liste (2)" }; xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { var response = xhttp.responseText; obj.innerHTML = response; + return true; } }; xhttp.open('POST', '/componants/update', true); @@ -117,51 +142,86 @@ function confirm_delete() { return confirm(msg) } +/////////////////////////////////////////// +// New componant +/////////////////////////////////////////// +function new_componant() { + var err = false; + var obj = {}; + if (getcookie('c_count') > 0){ + var errr = true; + obj[0] = document.getElementById('reference'); + } + if (getcookie('c_designation').length < 1){ + var err = true; + obj[1] = document.getElementById('designation'); + } + if (getcookie('c_place').length < 1){ + var err = true; + obj[2] = document.getElementById('place'); + } + if (err == true) { + for (i in obj){ + invalid_input(obj[i]); + } + return; + } + create_componant(); + update_componants(); +} + +function create_componant() { + var MSG='Erreur lors de la creation de la référence.'; + var xhttp = new XMLHttpRequest(); + xhttp.onerror = function(){ + alert(MSG); + return false; + }; + + xhttp.onload = function(){ + if (xhttp.readyState == 4 && xhttp.status == 200) { + var response = xhttp.responseText; + if (response == 'OK'){ + return true; + } + alert(MSG); + return false; + } + }; + + xhttp.open('POST', '/componants/new', true); + xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + xhttp.send(); +} + /////////////////////////////////////////// // Update componant /////////////////////////////////////////// function update_componant(obj, componant_id, type) { if (type == 'numeric') { if (isNaN(obj.value)) { - alert('Valeur numérique uniquement'); + alert('Valeur numérique uniquement: '+obj.value); return; } } var xhttp = new XMLHttpRequest(); xhttp.onerror = function(){ - obj.style.backgroundColor = light_red; - setTimeout( function() { - obj.style.backgroundColor = base_bg; - } - , 2000); + invalid_input(obj); }; xhttp.onload = function(){ - var bg_color = light_red; - var border_color = red; - if (xhttp.status == 200) { + if (xhttp.readyState == 4 && xhttp.status == 200) { var response = xhttp.responseText; if (response == 'OK'){ - bg_color = light_green; - border_color = base_border; - } - } - obj.style.backgroundColor = bg_color; - obj.style.borderColor = border_color; - setTimeout( function() { - obj.style.backgroundColor = base_bg; + valid_input(obj); + return; } - , 2000); + obj.style.borderColor = red; + invalid_input(obj); + } }; - xhttp.onreadystatechange = function() { - if (xhttp.readyState == 4 && xhttp.status == 200) { - var reponse = xhttp.responseText; - if (xhttp.responseText != 'OK') - obj.style.backgroundColor = light_red; - } - }; xhttp.open('POST', '/componants/update/'+componant_id, true); xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhttp.send('field='+obj.id+'&value='+obj.value); diff --git a/static/style/style.css b/static/style/style.css index 7f3eefe..624c135 100644 --- a/static/style/style.css +++ b/static/style/style.css @@ -139,16 +139,9 @@ div.content { text-align: justify; } -div.git_desc, pre { - border-style: solid; - border-color: #FF5D00; - border-width: 1px; - background-color: #FFFFFF; - padding: 5px; -} - -div.git_desc { - text-align: justify; +div.note { + font-size: 12px; + line-height: 12px; } div.error { @@ -206,13 +199,17 @@ div.result_container { border-right-style: solid; } -div.block { +div.block, div.noborder { overflow: hidden; border-top-style: solid; border-top-width: 1px; height: 20px; } +div.noborder { + border-style: none; +} + div.inner{ overflow: hidden; float: center; @@ -260,10 +257,19 @@ div.block text.refresh { div.block text.search { background: url(../images/search.png); - margin-left: 275px; + margin-left: 255px; } -div.inner text.edit:hover, div.inner text.trash:hover, div.block text.refresh:hover, div.block text.search:hover{ +div.block text.save { + background: url(../images/save.png); + margin-left: 32px; +} + +div.inner text.edit:hover, +div.inner text.trash:hover, +div.block text.refresh:hover, +div.block text.search:hover, +div.block text.save:hover{ background-color: #FFB387; cursor: pointer; } @@ -277,7 +283,11 @@ div.inner text.edit { background-repeat: no-repeat; } -div.inner text.trash, div.inner text.edit, div.block text.refresh, div.block text.search { +div.inner text.trash, +div.inner text.edit, +div.block text.refresh, +div.block text.search, +div.block text.save { width: 8px; float:left; height: 16px; @@ -286,11 +296,11 @@ div.inner text.trash, div.inner text.edit, div.block text.refresh, div.block tex } div.even { - background-color: #FFFFFF; + background-color: #E5E5E5; } div.odd { - background-color: #E5E5E5; + background-color: #FFFFFF; } div.block label { diff --git a/stock.sql b/stock.sql index ac11f69..b5a7c0c 100644 --- a/stock.sql +++ b/stock.sql @@ -1,3 +1,5 @@ +\c postgres; + drop database tetalab_stock; create database tetalab_stock; @@ -29,10 +31,10 @@ CREATE TABLE stock_componants ( id serial primary key, reference varchar(20) unique not NULL, designation varchar(50) not NULL, - last_price NUMERIC not NULL, - mean_price NUMERIC not NULL, - quantity NUMERIC not NULL, - min_quantity NUMERIC not NULL, + last_price NUMERIC not NULL default 0, + mean_price NUMERIC not NULL default 0, + quantity NUMERIC not NULL default 0, + min_quantity NUMERIC not NULL default 0, place varchar(15) not NULL, provider_id integer REFERENCES stock_providers(id) ); @@ -51,59 +53,60 @@ alter table stock_componants owner to tetalab_user; alter table stock_kit_compositions owner to tetalab_user; alter database tetalab_stock owner to tetalab_user; - +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', 'Pas cher, mais...'); 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); + 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', 1); - -\c postgres; + values ('Res-20', 'Resistance 29KΩ', 13.34, 12.42, 73, 0, 'B 43', 3); diff --git a/templates/componant.html b/templates/componant.html index 04ee284..ecff4e7 100644 --- a/templates/componant.html +++ b/templates/componant.html @@ -25,12 +25,14 @@ {% block content %}

Note:

-

Cette page vous permet de modifier directement un composant.

-

À moins de vouloir corriger une erreur de saisie, ou de saisir un inventaire vous ne devez - PAS modifier directement - les quantités d'un composant.

-

Cliquez sur les liens suivants pour saisir des entrées - ou sorties de stock.

+
+

Cette page vous permet de modifier directement un composant.

+

À moins de vouloir corriger une erreur de saisie, ou de saisir un inventaire vous ne devez + PAS modifier directement + les quantités d'un composant.

+

Cliquez sur les liens suivants pour saisir des entrées + ou sorties de stock.

+

Composant:

@@ -125,7 +127,7 @@ {% if prov.id == provider.id %} {% set option_selected = 'selected="selected"' %} {% endif %} - + {% endfor %}
diff --git a/templates/componants.html b/templates/componants.html index 5f3c628..9ac068d 100644 --- a/templates/componants.html +++ b/templates/componants.html @@ -1,4 +1,7 @@ {% extends "index.html" %} +{% block bodyheader %} + +{% endblock %} {% block title %}Liste des composants{% endblock %} {% block top_menu %} Rechercher un composant @@ -69,7 +72,6 @@ id='provider_id' onchange='javascript:search_componants_by_provider(this, "numeric");' title='Fournisseur'> - {% for prov in providers %} {% set selected = '' %} {% if prov.id == provider_id %} @@ -81,6 +83,7 @@
+
+

Recherche:

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ +
+ +
+ {% endblock %} diff --git a/tetastock.py b/tetastock.py index 087b58e..24a9067 100755 --- a/tetastock.py +++ b/tetastock.py @@ -2,10 +2,12 @@ # -*- coding: utf-8 import math +import psycopg2 from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash from flask_sqlalchemy import SQLAlchemy from functools import wraps + ######################################################################## # App settings ######################################################################## @@ -66,8 +68,11 @@ def resume_session(func): order = 'asc' order_refresh = 0 c_empty = '' - c_provider = 1 + c_provider = 0 + c_count = 0 offset_reset = ['c_reference', 'c_designation', 'c_place', 'c_provider'] + if not 'session' in session: + session['session'] = '' if not 'c_limit' in session: session['c_limit'] = limit if not 'c_offest' in session: @@ -90,6 +95,8 @@ def resume_session(func): session['c_place'] = c_empty if not 'c_provider' in session: session['c_provider'] = c_provider + if not 'c_count' in session: + session['c_count'] = c_count # Cookies/session sync for key in request.cookies: old = str(session[key]) @@ -129,11 +136,11 @@ def authenticate(): # Componants ######################################################################## -## Main ## @app.route('/componants', methods=['GET', 'POST']) @resume_session -def get_componants(): - providers = Stock_providers.query.order_by(Stock_providers.name).all() +def componants(): + """ Main page """ + providers = Stock_providers.query.order_by(Stock_providers.id).all() return render_template('componants.html', providers=providers, reference=session['c_reference'], @@ -141,9 +148,9 @@ def get_componants(): place=session['c_place'], provider_id=session['c_provider']) -## Componant edition ## @app.route('/componants/') def get_componant(componant_id): + """ Edit componant """ try: componant_id = int(componant_id) except ValueError as e: @@ -156,50 +163,70 @@ def get_componant(componant_id): return render_template('componant.html', componant=componant, providers=providers, provider=provider) return render_template('error.html'), 404 -## Componant update ## @app.route('/componants/update/', methods=['POST']) def update_componant(componant_id): + """ Update componant field""" field = request.form['field'] value = request.form['value'] if field and value: try: componant = Stock_componants.query.filter_by(id=componant_id).first() setattr(componant, field, value) - db.session.commit() + commit = db.session.commit() + if commit == None: + return 'OK' except Exception as e: - return 'KO' - return 'OK' + pass + return 'KO' -## Componant deletion ## @app.route('/componants/delete/') def delete_componant(componant_id): + """ Delete componant """ try: componant_id = int(componant_id) except ValueError as e: return render_template('error.html'), 404 Stock_componants.query.filter_by(id=componant_id).delete() db.session.commit() - return get_componants() + return componants() -## Componant creation ## -@app.route('/componants/new/') -def add_componant(componant_id): - return render_template('wip.html') +@app.route('/componants/new', methods=['POST']) +@resume_session +def new_componant(): + """ Add componant """ + componant = Stock_componants(reference=session['c_reference'], + designation=session['c_designation'], + last_price=0, + mean_price=0, + quantity=0, + min_quantity=0, + place=session['c_place'], + provider_id=session['c_provider']) + try: + db.session.add(componant) + commit = db.session.commit() + except Exception as e: + print e + return 'KO' + if commit != None: + return 'KO' + return 'OK' -## Componants stock in ## @app.route('/componants/in/') def in_componants(): + """ Incoming order """ return render_template('wip.html') -## Componants stock out ## @app.route('/componants/out/') def out_componants(): + """ Outgoing order """ return render_template('wip.html') ## Componants update result set @app.route('/componants/update', methods=['POST']) @resume_session def update_componants(): + """ Display componants list """ # search by reference like = '%s%s%s' % ('%', str(session['c_reference']), '%') componants = Stock_componants.query.filter(Stock_componants.reference.like(like)) @@ -210,18 +237,18 @@ def update_componants(): like = '%s%s%s' % ('%', str(session['c_place']),'%') componants = componants.filter(Stock_componants.place.like(like)) # search by provider - if session['c_provider'] > 0: + if session['c_provider'] > 1: componants = componants.filter_by(provider_id=session['c_provider']) # Pages calculation - row_count = componants.count() - session['c_pagecount'] = int(math.ceil(row_count / float(session['c_limit']))) + session['c_count'] = componants.count() + session['c_pagecount'] = int(math.ceil(session['c_count'] / float(session['c_limit']))) session['c_page'] = int(math.ceil(float(float(session['c_offset']) + 1) / float(session['c_limit']))) if session['c_page'] > session['c_pagecount']: session['c_page'] = session['c_pagecount'] session['c_offset'] = 0 print "*****", componants.count(), session['c_pagecount'], session['c_page'] session['c_nexthop'] = session['c_offset'] + session['c_limit'] - if session['c_nexthop'] > row_count - 1: + if session['c_nexthop'] > session['c_count'] - 1: session['c_nexthop'] = int(session['c_offset']) session['c_prevhop'] = int(session['c_offset']) - session['c_limit'] if session['c_prevhop'] < 0: @@ -247,7 +274,7 @@ def update_componants(): page=session['c_page'], sort=session['c_sort'], order=session['c_order'], - row_count=row_count)) + row_count=session['c_count'])) for key in session: response.set_cookie(key, value=str(session[key])) if key != 'session':