"New stable"

This commit is contained in:
Doug Le Tough 2017-10-23 13:42:49 +02:00
parent 2e04a88d9a
commit 5fbfd38218
6 changed files with 338 additions and 119 deletions

View File

@ -5,6 +5,121 @@ var light_green = "#D5FCD8";
var base_bg = "#FEFEFE"; var base_bg = "#FEFEFE";
var base_border = "#555555"; var base_border = "#555555";
///////////////////////////////////////////
// Cookies
///////////////////////////////////////////
function setcookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
///////////////////////////////////////////
// Update result
///////////////////////////////////////////
function update_componants() {
obj = document.getElementById('result_container');
var xhttp = new XMLHttpRequest();
xhttp.onerror = function(){
obj.innerHTML = "Erreur lors de la mise à jour de la liste (1)"
};
xhttp.onload = function(){
if (xhttp.status == 200) {
var response = xhttp.responseText;
obj.innerHTML = response;
return;
}
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;
}
};
xhttp.open('POST', '/componants/update', true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send();
}
///////////////////////////////////////////
// Result ordering
///////////////////////////////////////////
function update_componants_by_reference(order) {
setcookie('c_order', order, 30);
setcookie('c_sort', 'reference', 30);
setcookie('c_order_refresh', '1', 30);
update_componants();
}
function update_componants_by_designation(order) {
setcookie('c_order', order, 30);
setcookie('c_sort', 'designation', 30);
setcookie('c_order_refresh', '1', 30);
update_componants();
}
function update_componants_by_quantity(order) {
setcookie('c_order', order, 30);
setcookie('c_sort', 'quantity', 30);
setcookie('c_order_refresh', '1', 30);
update_componants();
}
function update_componants_by_place(order) {
setcookie('c_order', order, 30);
setcookie('c_sort', 'place', 30);
setcookie('c_order_refresh', '1', 30);
update_componants();
}
function previous_page(prevhop) {
setcookie('c_offset', prevhop, 30);
update_componants();
}
function next_page(nexthop) {
setcookie('c_offset', nexthop, 30);
update_componants();
}
///////////////////////////////////////////
// Search componants
///////////////////////////////////////////
function search_componants_by_reference(obj) {
setcookie('c_reference', obj.value, 30);
update_componants();
}
function search_componants_by_designation(obj) {
setcookie('c_designation', obj.value, 30);
update_componants();
}
function search_componants_by_place(obj) {
setcookie('c_place', obj.value, 30);
update_componants();
}
function search_componants_by_provider(obj) {
setcookie('c_provider', obj.value, 30);
update_componants();
}
///////////////////////////////////////////
// Delete componant
///////////////////////////////////////////
function confirm_delete() {
var msg="La suppression est définitive \net n'est pas autorisée si le \ncomposant fait partie d'un Kit.\n\nConfirmer ?";
return confirm(msg)
}
///////////////////////////////////////////
// Update componant
///////////////////////////////////////////
function update_componant(obj, componant_id, type) { function update_componant(obj, componant_id, type) {
if (type == 'numeric') { if (type == 'numeric') {
if (isNaN(obj.value)) { if (isNaN(obj.value)) {
@ -47,13 +162,7 @@ function update_componant(obj, componant_id, type) {
obj.style.backgroundColor = light_red; obj.style.backgroundColor = light_red;
} }
}; };
xhttp.open('POST', '/componants/update/'+componant_id, true);
xhttp.open('POST', '/componant/update/'+componant_id, true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send('field='+obj.id+'&value='+obj.value); xhttp.send('field='+obj.id+'&value='+obj.value);
} }
function confirm_delete() {
var msg="La suppression est définitive \net n'est pas autorisée si le \ncomposant fait partie d'un Kit.\n\nConfirmer ?";
return confirm(msg)
}

View File

@ -185,9 +185,17 @@ div.footer {
} }
/* *********************************** /* ***********************************
* Tables * Résultats
*********************************** */ *********************************** */
div.row_count {
padding: 5px;
}
div.result_container {
text-align: left;
}
.border_left { .border_left {
border-left-width: 1px; border-left-width: 1px;
border-left-style: solid; border-left-style: solid;
@ -200,7 +208,6 @@ div.footer {
div.block { div.block {
overflow: hidden; overflow: hidden;
width: 1000px;
border-top-style: solid; border-top-style: solid;
border-top-width: 1px; border-top-width: 1px;
height: 20px; height: 20px;
@ -379,6 +386,7 @@ div.block input {
width: 300px; width: 300px;
} }
/* *********************************** /* ***********************************
* Classes génériques * Classes génériques
*********************************** */ *********************************** */

View File

@ -1,9 +1,9 @@
{% extends "index.html" %} {% extends "index.html" %}
{% block title %}Éditer un composant{% endblock %} {% block title %}Éditer un composant{% endblock %}
{% block top_menu %} {% block top_menu %}
<span class='top_menu_item' onclick='javascript:document.location="/componants";'>Rechercher un composant</span>
<span class='top_menu_item_selected' onclick='javascript:document.location="/componants/{{ componant.id }}";'>Éditer un composant</span>
<span class='top_menu_item' onclick='javascript:document.location="/componants/new";'>Nouveau composant</span> <span class='top_menu_item' onclick='javascript:document.location="/componants/new";'>Nouveau composant</span>
<span class='top_menu_item' onclick='javascript:document.location="/componants/search";'>Rechercher un composant</span>
<span class='top_menu_item_selected' onclick='javascript:document.location="/componants/search";'>Éditer un composant</span>
<span class='top_menu_item' onclick='javascript:document.location="/componants/in";'>Entrée de stock</span> <span class='top_menu_item' onclick='javascript:document.location="/componants/in";'>Entrée de stock</span>
<span class='top_menu_item' onclick='javascript:document.location="/componants/out";'>Sortie de stock</span> <span class='top_menu_item' onclick='javascript:document.location="/componants/out";'>Sortie de stock</span>
{% endblock %} {% endblock %}

View File

@ -1,8 +1,8 @@
{% extends "index.html" %} {% extends "index.html" %}
{% block title %}Liste des composants{% endblock %} {% block title %}Liste des composants{% endblock %}
{% block top_menu %} {% block top_menu %}
<span class='top_menu_item_selected' onclick='javascript:document.location="/componants/search";'>Rechercher un composant</span>
<span class='top_menu_item' onclick='javascript:document.location="/componants/new";'>Nouveau composant</span> <span class='top_menu_item' onclick='javascript:document.location="/componants/new";'>Nouveau composant</span>
<span class='top_menu_item' onclick='javascript:document.location="/componants/search";'>Rechercher un composant</span>
<span class='top_menu_item' onclick='javascript:document.location="/componants/in";'>Entrée de stock</span> <span class='top_menu_item' onclick='javascript:document.location="/componants/in";'>Entrée de stock</span>
<span class='top_menu_item' onclick='javascript:document.location="/componants/out";'>Sortie de stock</span> <span class='top_menu_item' onclick='javascript:document.location="/componants/out";'>Sortie de stock</span>
{% endblock %} {% endblock %}
@ -33,11 +33,11 @@
id='reference' id='reference'
type='text' type='text'
class='editable' class='editable'
onchange='javascript:search_componant(this, "text");' onkeyup='javascript:search_componants_by_reference(this, "text");'
maxlength='20' maxlength='20'
title='Référence interne unique (max. 20)' title='Référence interne unique (max. 20)'
placeholder='Référence interne unique (max. 20)' placeholder='Référence interne unique (max. 20)'
value='' /> value='{{ reference }}' />
</div> </div>
<div class='block margin_bottom no_border'> <div class='block margin_bottom no_border'>
<label class='editable'>Désignation</label> <label class='editable'>Désignation</label>
@ -45,11 +45,11 @@
id='designation' id='designation'
type='text' type='text'
class='editable' class='editable'
onchange='javascript:search_componant(this, "text");' onkeyup='javascript:search_componants_by_designation(this, "text");'
maxlength='50' maxlength='50'
title='Désignation (max. 50)' title='Désignation (max. 50)'
placeholder='Désignation (max. 50)' placeholder='Désignation (max. 50)'
value='' /> value='{{ designation }}' />
</div> </div>
<div class='block margin_bottom no_border'> <div class='block margin_bottom no_border'>
<label class='editable'>Emplacement</label> <label class='editable'>Emplacement</label>
@ -57,54 +57,35 @@
id='place' id='place'
type='text' type='text'
class='editable' class='editable'
onchange='javascript:search_componant(this, "text");' onkeyup='javascript:search_componants_by_place(this, "text");'
maxlength='15' maxlength='15'
title='Emplacement (max. 15)' title='Emplacement (max. 15)'
placeholder='Emplacement (max. 15)' placeholder='Emplacement (max. 15)'
value='' /> value='{{ place }}' />
</div> </div>
<div class='block margin_bottom no_border'> <div class='block margin_bottom no_border'>
<label class='editable'>Fournisseur</label> <label class='editable'>Fournisseur</label>
<select <select
id='provider_id' id='provider_id'
onchange='javascript:search_componant(this, "numeric");' onchange='javascript:search_componants_by_provider(this, "numeric");'
title='Fournisseur'> title='Fournisseur'>
<option value='0' selected>Tous</option> <option value='0' selected>Tous</option>
{% for prov in providers %} {% for prov in providers %}
<option value='prov.id'>{{ prov.name }}</option> {% set selected = '' %}
{% if prov.id == provider_id %}
{% set selected = 'selected' %}
{% endif %}
<option value='{{ prov.id }}' {{ selected }}>{{ prov.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
</div> </div>
<div class='block margin_bottom no_border center'> <div class='block margin_bottom no_border center'>
<text title='Rechercher' class='search' onclick='javascript:search_componant();'></text> <text title='Rechercher' class='search' onclick='javascript:update_componants("reference");'></text>
</div> </div>
<!-- ---------------------------------------------------- <!-- ----------------------------------------------------
Resultat Resultat
----------------------------------------------------- --> ----------------------------------------------------- -->
<h3>Résultat</h3> <div id='result_container' class='result_container'>
<div class='block' style='border-top-style: none;'>
<label onclick='document.location="/componants?offset={{ offset }}&s=reference&o={{ norder }}";'>Référence</label>
<label onclick='document.location="/componants?offset={{ offset }}&s=designation&o={{ norder }}";' class='border_left' style='width: 440px;'>Désignation</label>
<label onclick='document.location="/componants?offset={{ offset }}&s=quantity&o={{ norder }}";' class='border_left' style='width: 100px;'>Quantité</label>
<label onclick='document.location="/componants?offset={{ offset }}&s=place&o={{ norder }}";' class='border_right border_left' style='width: 160px;'>Emplacement</label>
<label style='width: 95px; cursor: default;'>Action</label>
</div>
{% set row_class = cycler('odd', 'even') %}
{% for componant in componants %}
<div class='block {{ row_class.next() }}'>
<text style='width: 192px;'>{{ componant.reference }}</text>
<text class='border_left' style='width: 432px;'>{{ componant.designation }}</text>
<text class='num border_left' style='width: 92px;'>{{ componant.quantity }}</text>
<text class='num border_right border_left' style='width: 152px;'>{{ componant.place }}</text>
<div class='inner' style='width: 95px;'>
<text class='edit' title='Éditer' onclick='javascript:document.location="/componants/{{ componant.id }}"'></text>
<text class='trash' title='Supprimer' onclick='javascript:confirm_delete()?document.location="/componants/delete/{{ componant.id }}":false;'></text>
</div>
</div>
{% endfor %}
<div class='pages_nav_bar'>
<text><span class='prev_page' onclick='javascript:document.location="/componants?offset={{ prevhop }}&s={{ sort }}&o={{ order }}"'><</span></text>
<text><span class='page_num'>{{ page }} / {{ page_count }}</span></text>
<text><span class='next_page' onclick='javascript:document.location="/componants?offset={{ nexthop }}&s={{ sort }}&o={{ order }}"'>></span></text>
</div> </div>
{% endblock %} {% endblock %}

31
templates/result.html Normal file
View File

@ -0,0 +1,31 @@
<h3>Résultat:</h3>
{% set ss='' %}
{% if row_count > 1 %}
{% set ss='s' %}
{% endif %}
<div class='row_count'>{{ row_count }} objet{{ ss }} selectionné{{ ss }}</div>
<div class='block' style='border-top-style: none;'>
<label onclick='javascript:update_componants_by_reference("{{ order }}");'>Référence</label>
<label onclick='javascript:update_componants_by_designation("{{ order }}");' class='border_left' style='width: 440px;'>Désignation</label>
<label onclick='javascript:update_componants_by_quantity("{{ order }}");' class='border_left' style='width: 100px;'>Quantité</label>
<label onclick='javascript:update_componants_by_place("{{ order }}");' class='border_right border_left' style='width: 160px;'>Emplacement</label>
<label style='width: 106px; cursor: default;'>Action</label>
</div>
{% set row_class = cycler('odd', 'even') %}
{% for componant in componants %}
<div class='block {{ row_class.next() }}'>
<text style='width: 192px;'>{{ componant.reference }}</text>
<text class='border_left' style='width: 432px;'>{{ componant.designation }}</text>
<text class='num border_left' style='width: 92px;'>{{ componant.quantity }}</text>
<text class='num border_right border_left' style='width: 152px;'>{{ componant.place }}</text>
<div class='inner' style='width: 95px;'>
<text class='edit' title='Éditer' onclick='javascript:document.location="/componants/{{ componant.id }}"'></text>
<text class='trash' title='Supprimer' onclick='javascript:confirm_delete()?document.location="/componants/delete/{{ componant.id }}":false;'></text>
</div>
</div>
{% endfor %}
<div class='pages_nav_bar'>
<text><span class='prev_page' onclick='javascript:previous_page({{ prevhop }});'><</span></text>
<text><span class='page_num'>{{ page }} / {{ page_count }}</span></text>
<text><span class='next_page' onclick='javascript:next_page({{ nexthop }})'>></span></text>
</div>

View File

@ -4,7 +4,11 @@
import math import math
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from functools import wraps
########################################################################
# App settings
########################################################################
app = Flask(__name__) app = Flask(__name__)
app.config.from_pyfile('config.py') app.config.from_pyfile('config.py')
app.secret_key = '446307a5f61c2bb810436b2ee0f903f2' app.secret_key = '446307a5f61c2bb810436b2ee0f903f2'
@ -12,6 +16,9 @@ app.debug = True
app.static_url_path='/static' app.static_url_path='/static'
db = SQLAlchemy(app) db = SQLAlchemy(app)
########################################################################
# Database
########################################################################
class Stock_users(db.Model): class Stock_users(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
mail = db.Column(db.Text, nullable=False) mail = db.Column(db.Text, nullable=False)
@ -47,6 +54,62 @@ class Stock_kit_compositions(db.Model):
component_id = db.Column(db.Integer, db.ForeignKey('Stock_componants.id'), nullable=False) component_id = db.Column(db.Integer, db.ForeignKey('Stock_componants.id'), nullable=False)
component_quantity = db.Column(db.Integer, nullable=False) component_quantity = db.Column(db.Integer, nullable=False)
def resume_session(func):
@wraps(func)
def check(*args, **kwargs):
# default
limit = 8
offset = 0
nexthop = offset + limit
prevhop = offset
sort = 'reference'
order = 'asc'
order_refresh = 0
c_empty = ''
c_provider = 1
if not 'c_limit' in session:
session['c_limit'] = limit
if not 'c_offest' in session:
session['c_offset'] = offset
if not 'c_sort' in session:
session['c_sort'] = sort
if not 'c_order' in session:
session['c_order'] = order
if not 'c_order_refresh' in session:
session['c_order_refresh'] = order_refresh
if not 'c_nexthop' in session:
session['c_nexthop'] = nexthop
if not 'c_prevhop' in session:
session['c_prevhop'] = prevhop
if not 'c_reference' in session:
session['c_reference'] = c_empty
if not 'c_designation' in session:
session['c_designation'] = c_empty
if not 'c_place' in session:
session['c_place'] = c_empty
if not 'c_provider' in session:
session['c_provider'] = c_provider
# Cookies/session sync
for key in request.cookies:
try:
session[key] = int(str(request.cookies[key]))
except ValueError:
# Value is not an int, will be treated as string
session[key] = str(request.cookies[key])
if key != 'session':
print '+', key, request.cookies[key]
# Switch sort order
if session['c_order_refresh'] == 1:
refresh = {'desc': 'asc', 'asc': 'desc'}
session['c_order'] = refresh[session['c_order']]
session['c_order_refresh'] = 0
return func(*args, **kwargs)
return check
########################################################################
# Routes
########################################################################
@app.errorhandler(404) @app.errorhandler(404)
def page_not_found(e): def page_not_found(e):
return render_template('error.html'), 404 return render_template('error.html'), 404
@ -59,58 +122,22 @@ def authenticate():
######################################################################## ########################################################################
# Componants # Componants
######################################################################## ########################################################################
@app.route('/componants', methods=['GET', 'POST'])
def get_componants():
if not session or not session['plop']:
session['plop']='plip'
limit = 8
offset = 0
sort = 'reference'
order = 'asc'
norder = 'desc'
print '*', sort
if len(request.args):
if 'offset' in request.args:
offset = request.args['offset']
if 's' in request.args:
sort = request.args['s']
if 'o' in request.args:
order = request.args['o']
try:
offset = int(offset)
except ValueError:
offset = 0
providers = Stock_providers.query.order_by(Stock_providers.name).all()
row_count = db.session.query(Stock_componants.id).count()
page_count = int(math.ceil(row_count / float(limit)))
page = int(math.ceil(float(offset + 1) / float(limit)))
nexthop = offset + limit
if nexthop > row_count - 1:
nexthop = offset
prevhop = offset - limit
if prevhop < 0:
prevhop = 0
if order == 'asc':
norder = 'desc'
componants = Stock_componants.query.order_by(getattr(Stock_componants, sort)).offset(offset).limit(limit).all()
else:
norder = 'asc'
componants = Stock_componants.query.order_by(getattr(Stock_componants, sort).desc()).offset(offset).limit(limit).all()
return render_template('componants.html',
componants=componants,
providers = providers,
offset=offset,
nexthop=nexthop,
prevhop=prevhop,
page_count=page_count,
page=page,
sort=sort,
order=order,
norder=norder)
## Main ##
@app.route('/componants', methods=['GET', 'POST'])
@resume_session
def get_componants():
providers = Stock_providers.query.order_by(Stock_providers.name).all()
return render_template('componants.html',
providers=providers,
reference=session['c_reference'],
designation=session['c_designation'],
place=session['c_place'],
provider_id=session['c_provider'])
## Componant edition ##
@app.route('/componants/<componant_id>') @app.route('/componants/<componant_id>')
def get_componant(componant_id): def get_componant(componant_id):
print session['plop']
try: try:
componant_id = int(componant_id) componant_id = int(componant_id)
except ValueError as e: except ValueError as e:
@ -123,30 +150,9 @@ def get_componant(componant_id):
return render_template('componant.html', componant=componant, providers=providers, provider=provider) return render_template('componant.html', componant=componant, providers=providers, provider=provider)
return render_template('error.html'), 404 return render_template('error.html'), 404
@app.route('/componants/delete/<componant_id>') ## Componant update ##
def delete_componant(componant_id): @app.route('/componants/update/<componant_id>', methods=['POST'])
try: def update_componant(componant_id):
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()
@app.route('/componants/new/<componant_id>')
def add_componant(componant_id):
return render_template('wip.html')
@app.route('/componants/in/<componant_id>')
def in_componants():
return render_template('wip.html')
@app.route('/componants/out/<componant_id>')
def out_componants():
return render_template('wip.html')
@app.route('/componant/update/<componant_id>', methods=['POST'])
def update_componants(componant_id):
field = request.form['field'] field = request.form['field']
value = request.form['value'] value = request.form['value']
if field and value: if field and value:
@ -158,6 +164,90 @@ def update_componants(componant_id):
return 'KO' return 'KO'
return 'OK' return 'OK'
## Componant deletion ##
@app.route('/componants/delete/<componant_id>')
def delete_componant(componant_id):
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()
## Componant creation ##
@app.route('/componants/new/<componant_id>')
def add_componant(componant_id):
return render_template('wip.html')
## Componants stock in ##
@app.route('/componants/in/<componant_id>')
def in_componants():
return render_template('wip.html')
## Componants stock out ##
@app.route('/componants/out/<componant_id>')
def out_componants():
return render_template('wip.html')
## Componants update result set
@app.route('/componants/update', methods=['POST'])
@resume_session
def update_componants():
# search by reference
like = '%'+str(session['c_reference'])+'%'
componants = Stock_componants.query.filter(Stock_componants.reference.like(like))
# search by designation
like = '%'+str(session['c_designation'])+'%'
componants = componants.filter(Stock_componants.designation.like(like))
# search by place
like = '%'+str(session['c_place'])+'%'
componants = componants.filter(Stock_componants.place.like(like))
# search by provider
if session['c_provider'] > 0:
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_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:
session['c_nexthop'] = int(session['c_offset'])
session['c_prevhop'] = int(session['c_offset']) - session['c_limit']
if session['c_prevhop'] < 0:
session['c_prevhop'] = 0
# Sorting
sort = getattr(Stock_componants, session['c_sort'])
if session['c_order'] == 'desc':
sort = getattr(Stock_componants, session['c_sort']).desc()
componants = componants.order_by(sort)
# Applying offset
componants = componants.offset(session['c_offset'])
# Applying limit
componants = componants.limit(session['c_limit'])
# Get result
componants = componants.all()
response = app.make_response(render_template('result.html',
componants=componants,
offset=session['c_offset'] ,
nexthop=session['c_nexthop'],
prevhop=session['c_prevhop'],
page_count=session['c_pagecount'],
page=session['c_page'],
sort=session['c_sort'],
order=session['c_order'],
row_count=row_count))
for key in session:
response.set_cookie(key, value=str(session[key]))
if key != 'session':
print '++', key, session[key]
return response
######################################################################## ########################################################################
# Kits # Kits