"Support UTF-8 + gestion de base des kits"

This commit is contained in:
Doug Le Tough 2017-10-28 00:43:19 +02:00
parent a0b4516f98
commit 0a63c23c49
15 changed files with 856 additions and 249 deletions

View File

@ -1,2 +1,2 @@
SQLALCHEMY_TRACK_MODIFICATIONS = True SQLALCHEMY_TRACK_MODIFICATIONS = True
SQLALCHEMY_DATABASE_URI = "postgresql://tetalab_user:tetalab@localhost/tetalab_stock" SQLALCHEMY_DATABASE_URI = "postgresql://tetastock:tetastock@localhost/tetastock"

BIN
static/images/login.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 B

BIN
static/images/logout.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 327 B

View File

@ -46,6 +46,36 @@ function valid_input(obj) {
, 2000); , 2000);
} }
/* **************************************************************************************
* LOGIN
* **************************************************************************************/
function login() {
err = false;
username = document.getElementById('login');
password = document.getElementById('password');
if (username.value.length < 1) {
err = true;
invalid_input(username);
}
if (password.value.length < 1){
err = true;
invalid_input(password);
}
if (err)
return;
setcookie('login', username.value, 30);
setcookie('password', password.value, 30);
document.location='/';
}
function logout() {
setcookie('token', '', 30);
document.location='/';
}
/* ************************************************************************************** /* **************************************************************************************
* COMPONTANTS * COMPONTANTS
* **************************************************************************************/ * **************************************************************************************/
@ -143,7 +173,7 @@ function search_componants_by_provider(obj) {
function confirm_componant_delete() { function confirm_componant_delete() {
var msg="La suppression est définitive \net n'est pas autorisée si le \ncomposant fait partie d'un Kit.\n\nConfirmer ?"; 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) return confirm(msg);
} }
// New componant // New componant
@ -383,7 +413,7 @@ function create_provider() {
function confirm_provider_delete() { function confirm_provider_delete() {
var msg="La suppression est définitive \net n'est pas autorisée si le \nfournisseur est référencé \npar un composant.\n\nConfirmer ?"; var msg="La suppression est définitive \net n'est pas autorisée si le \nfournisseur est référencé \npar un composant.\n\nConfirmer ?";
return confirm(msg) return confirm(msg);
} }
// Update provider // Update provider
@ -410,3 +440,162 @@ function update_provider(obj, provider_id, type) {
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);
} }
/* **************************************************************************************
* KITS
* **************************************************************************************/
// Update result
function update_kits() {
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) {
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', '/kits/update', true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send();
}
// Result ordering
function update_kits_by_name(order) {
setcookie('k_order', order, 30);
setcookie('k_sort', 'name', 30);
setcookie('k_order_refresh', '1', 30);
update_kits();
}
function update_kits_by_name(order) {
setcookie('k_order', order, 30);
setcookie('k_sort', 'name', 30);
setcookie('k_order_refresh', '1', 30);
update_kits();
}
function update_kits_by_designation(order) {
setcookie('k_order', order, 30);
setcookie('k_sort', 'designation', 30);
setcookie('k_order_refresh', '1', 30);
update_kits();
}
function k_krevious_kage(prevhop) {
setcookie('k_offset', prevhop, 30);
update_kits();
}
function k_next_kage(nexthop) {
setcookie('k_offset', nexthop, 30);
update_kits();
}
// Search kits
function search_kits_by_name(obj) {
setcookie('k_name', obj.value, 30);
update_kits();
}
function search_kits_by_designation(obj) {
setcookie('k_designation', obj.value, 30);
update_kits();
}
// New kit
function new_kit() {
var err = false;
var obj = {};
if (getcookie('k_count') > 0){
var err = true;
obj[0] = document.getElementById('name');
}
if (getcookie('k_name').length < 1){
var err = true;
obj[0] = document.getElementById('name');
}
if (getcookie('k_designation').length < 1){
var err = true;
obj[1] = document.getElementById('designation');
}
if (err == true) {
for (i in obj){
invalid_input(obj[i]);
}
return;
}
create_kit();
update_kits();
}
function create_kit() {
var MSG='Erreur lors de la creation du kit.';
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', '/kits/new', true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send();
}
// Delete kit
function confirm_kit_delete() {
var msg="La suppression d'un kit est définitive.\n\nConfirmer ?";
return confirm(msg);
}
// Update kit
function update_kit(obj, kit_id, type) {
var xhttp = new XMLHttpRequest();
xhttp.onerror = function(){
invalid_input(obj);
};
xhttp.onload = function(){
if (xhttp.readyState == 4 && xhttp.status == 200) {
var response = xhttp.responseText;
if (response == 'OK'){
valid_input(obj);
return;
}
obj.style.borderColor = red;
invalid_input(obj);
}
};
xhttp.open('POST', '/kits/update/'+kit_id, true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send('field='+obj.id+'&value='+obj.value);
}

View File

@ -44,6 +44,19 @@ div.top_menu {
border-width: 1px 0 0 0; border-width: 1px 0 0 0;
} }
div.top_menu input {
position: relative;
top: 4px;
width: 16px;
height: 16px;
border-radius: 2px;
margin-right: 4px;
}
div.top_menu input:hover {
background-color: #FF5D00;
}
span.top_menu_item { span.top_menu_item {
margin: 5px 2px 0 0; margin: 5px 2px 0 0;
font-size: 12px; font-size: 12px;

View File

@ -1,9 +1,13 @@
\c postgres; \c postgres;
drop database tetalab_stock; drop database tetastock;
create database tetalab_stock; drop role tetastock;
\c tetalab_stock; create role tetastock with LOGIN ENCRYPTED PASSWORD 'tetastock';
create database tetastock;
\c tetastock;
CREATE TABLE stock_users ( CREATE TABLE stock_users (
id serial primary key, id serial primary key,
@ -14,8 +18,8 @@ CREATE TABLE stock_users (
CREATE TABLE stock_kits ( CREATE TABLE stock_kits (
id serial primary key, id serial primary key,
name text not NULL, name text not NULL unique,
description text not NULL designation text not NULL
); );
CREATE TABLE stock_providers ( CREATE TABLE stock_providers (
@ -46,12 +50,26 @@ CREATE TABLE stock_kit_compositions (
quantity integer not NULL quantity integer not NULL
); );
alter table stock_users owner to tetalab_user; CREATE TABLE stock_orders (
alter table stock_kits owner to tetalab_user; id serial primary key,
alter table stock_providers owner to tetalab_user; componant_id integer REFERENCES stock_componants(id),
alter table stock_componants owner to tetalab_user; quantity integer not NULL,
alter table stock_kit_compositions owner to tetalab_user; price NUMERIC not NULL default 0,
alter database tetalab_stock owner to tetalab_user; date timestamp not NULL
);
alter table stock_users owner to tetastock;
alter table stock_kits owner to tetastock;
alter table stock_providers owner to tetastock;
alter table stock_componants owner to tetastock;
alter table stock_kit_compositions owner to tetastock;
alter table stock_orders owner to tetastock;
alter database tetastock owner to tetastock;
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) insert into stock_providers (name, address, mail, url, comment)
values ('Tous', 'N/A', 'N/A', 'N/A', 'N/A'); values ('Tous', 'N/A', 'N/A', 'N/A', 'N/A');

View File

@ -6,7 +6,7 @@
<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 %}
{% block left_menu %} {% block left_menu %}
<div class='left_menu_item' onclick='javascript:document.location="/";'> <div class='left_menu_item' onclick='javascript:document.location="/";'>
Accueil Accueil

View File

@ -17,6 +17,7 @@
</div> </div>
<div class='top_menu'> <div class='top_menu'>
{% block top_menu %} <span class='top_menu_item_selected' onclick='javascript:document.location="./";'>Accueil</span> {% endblock %} {% block top_menu %} <span class='top_menu_item_selected' onclick='javascript:document.location="./";'>Accueil</span> {% endblock %}
<input type='image' src='/static/images/logout.png' title='Se deconnecter' value='' onclick='javascript:logout();' />
</div> </div>
<div class='content_wrapper'> <div class='content_wrapper'>
<div class='left_menu'> <div class='left_menu'>
@ -38,6 +39,7 @@
{% block content %} {% block content %}
<h1>Tetastock</h1> <h1>Tetastock</h1>
<div class='note'> <div class='note'>
<p>Bienvenue {{ user }}</p>
<p>Ceci est l'outil de gestion de stock du <a href='https://www.tetalab.org'>Tetalab</a>.</p> <p>Ceci est l'outil de gestion de stock du <a href='https://www.tetalab.org'>Tetalab</a>.</p>
<p>Cet outil vous permet: <p>Cet outil vous permet:
<ul> <ul>

47
templates/kit.html Normal file
View File

@ -0,0 +1,47 @@
{% extends "index.html" %}
{% block title %}Éditer un kit{% endblock %}
{% block top_menu %}
<span class='top_menu_item' onclick='javascript:document.location="/providers";'>Gérer les kits</span>
<span class='top_menu_item_selected' onclick='javascript:document.location="/kits/{{ kit.id }}";'>Éditer un kit</span>
{% endblock %}
{% block left_menu %}
<div class='left_menu_item' onclick='javascript:document.location="/";'>
Accueil
</div>
<div class='left_menu_item' onclick='javascript:document.location="/componants";'>
Composants
</div>
<div class='left_menu_item_selected' onclick='javascript:document.location="/kits";'>
Kits
</div>
<div class='left_menu_item' onclick='javascript:document.location="/providers";'>
Fournisseurs
</div>
{% endblock %}
{% block content %}
<div id='edit' class='main_block'>
<text>Kit:</text>
<div class='input_block'>
<label>Nom (unique)</label>
<input
id='name'
type='text'
onchange='javascript:update_kit(this, {{ kit.id }}, "text");'
maxlength='20'
title='Nom unique (max. 20)'
placeholder='Nom unique (max. 20)'
value='{{ kit.name }}' />
</div>
<div class='input_block'>
<label>Designation</label>
<input
id='designation'
type='text'
onchange='javascript:update_kit(this, {{ kit.id }}, "text");'
title='Adresse'
placeholder='Adresse'
value='{{ kit.designation }}' />
</div>
</div>
{% endblock %}

70
templates/kits.html Normal file
View File

@ -0,0 +1,70 @@
{% extends "index.html" %}
{% block bodyheader %}
<body onload="javascript:update_kits();">
{% endblock %}
{% block title %}Liste des fournisseurs{% endblock %}
{% block top_menu %}
<span class='top_menu_item_selected' onclick='javascript:document.location="/kits";'>Gérer les kits</span>
{% endblock %}
{% block left_menu %}
<div class='left_menu_item' onclick='javascript:document.location="/";'>
Accueil
</div>
<div class='left_menu_item' onclick='javascript:document.location="/componants";'>
Composants
</div>
<div class='left_menu_item_selected' onclick='javascript:document.location="/kits";'>
Kits
</div>
<div class='left_menu_item' onclick='javascript:document.location="/providers";'>
Fournisseurs
</div>
{% endblock %}
{% block content %}
<!-- ----------------------------------------------------
Recherche
----------------------------------------------------- -->
<div id='search' class='main_block'>
<text>Recherche:</text>
<div class='input_block'>
<label>Nom (unique)</label>
<input
id='name'
type='text'
onkeyup='javascript:search_kits_by_name(this, "text");'
title='Nom unique'
placeholder='Nom unique'
value='{{ name }}' />
</div>
<div class='input_block'>
<label>Désignation</label>
<input
id='designation'
type='text'
onkeyup='javascript:search_kits_by_designation(this, "text");'
title='Désignation'
placeholder='Désignation'
value='{{ designation }}' />
</div>
<div class='button_block'>
<input
type='image'
src='/static/images/search.png'
title='Rechercher'
onclick='javascript:update_kits("name");'/>
<input
type='image'
src='/static/images/save.png'
title='Enregistrer'
onclick='javascript:new_kit();'/>
</div>
</div>
<!-- ----------------------------------------------------
Resultat
----------------------------------------------------- -->
<div id='result_container' class='result_container'>
</div>
{% endblock %}

50
templates/login.html Normal file
View File

@ -0,0 +1,50 @@
{% extends "index.html" %}
{% block title %}Éditer un fournisseur{% endblock %}
{% block top_menu %}
<span class='top_menu_item_selected'>Authentification</span>
{% endblock %}
{% block left_menu %}
<div class='left_menu_item_selected' onclick='javascript:document.location="/";'>
Accueil
</div>
<div class='left_menu_item' onclick='javascript:document.location="/componants";'>
Composants
</div>
<div class='left_menu_item' onclick='javascript:document.location="/kits";'>
Kits
</div>
<div class='left_menu_item' onclick='javascript:document.location="/providers";'>
Fournisseurs
</div>
{% endblock %}
{% block content %}
<div id='edit' class='main_block'>
<text>Authentification:<text>
<div class='input_block'>
<label>Login</label>
<input
id='login'
type='text'
title='Login'
placeholder='Login'
value='' />
</div>
<div class='input_block'>
<label>Mot de passe</label>
<input
id='password'
type='password'
title='Mot de passe'
placeholder='Mot de passe'
value='' />
</div>
<div class='button_block'>
<input
type='image'
src='/static/images/login.png'
title='Se connecter'
onclick='javascript:login();'/>
</div>
</div>
{% endblock %}

View File

@ -21,7 +21,7 @@
{% block content %} {% block content %}
<div id='edit' class='main_block'> <div id='edit' class='main_block'>
<text>Composant:</text> <text>Fournisseur:</text>
<div class='input_block'> <div class='input_block'>
<label>Nom (unique)</label> <label>Nom (unique)</label>
<input <input

View File

@ -0,0 +1,40 @@
<div class='main_block'>
<text>Résultat:
{% set ss='' %}
{% if row_count > 1 %}
{% set ss='s' %}
{% endif %}
{{ row_count }} objet{{ ss }} selectionné{{ ss }}
</text>
<div class='row_block border_bottom'>
<label style='width: 209px;' onclick='javascript:update_kits_by_name("{{ order }}");'>Nom</label>
<label class='border_left' style='width: 700px;' onclick='javascript:update_kits_by_designation("{{ order }}");'>Désignation</label>
<label class='border_left' style='width: 89px;' style='cursor: default;'>Action</label>
</div>
{% set row_class = cycler('odd', 'even') %}
{% for kit in kits %}
<div class='row_block {{ row_class.next() }}'>
<text style='width: 199px;'>{{ kit.name }}</text>
<text class='border_left' style='width: 690px;'>{{ kit.designation }}</text>
<div class='action_bar_block border_left' style='width: 89px;'>
<input
type='image'
src='/static/images/edit.png'
title='Éditer'
onclick='javascript:document.location="/kits/{{ kit.id }}"'/>
<input
type='image'
src='/static/images/trash.png'
title='Supprimer'
onclick='javascript:confirm_kit_delete()?document.location="/kits/delete/{{ kit.id }}":false;'/>
</div>
</div>
{% endfor %}
<div class='nav_page_block border_top'>
<text>
<span class='prev_page' onclick='javascript:k_previous_page({{ prevhop }});'>&lt;</span>
<span class='page_num'>{{ page }} / {{ page_count }}</span>
<span class='next_page' onclick='javascript:k_next_page({{ nexthop }})'>&gt;</span>
</text>
</div>
</div>

View File

@ -1,56 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr" dir="ltr">
<head>
<meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.5.21" />
<title>Stock Tetalab - {% block title %}Accueil{% endblock %}</title>
<meta name="viewport" content="initial-scale=1.0" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/static/style/style.css" />
<script type="text/javascript" src="/static/scripts/tetalab.js"></script>
</head>
<body>
<div class='main_wrapper'>
<div class='center'>
<div class='banner' title='Stock Tetalab'>
Stock Tetalab
</div>
<div class='top_menu'>
{% block top_menu %} <span class='top_menu_item_selected' onclick='javascript:document.location="./";'>Accueil</span> {% endblock %}
</div>
<div class='content_wrapper'>
<div class='left_menu'>
{% block left_menu %}
<div class='left_menu_item_selected' onclick='javascript:document.location="/";'>
Accueil
</div>
<div class='left_menu_item' onclick='javascript:document.location="/componants";'>
Composants
</div>
<div class='left_menu_item' onclick='javascript:document.location="/kits";'>
Kits
</div>
<div class='left_menu_item' onclick='javascript:document.location="/providers";'>
Fournisseurs
</div>{% endblock %}
</div>
<div class='content'>
{% block content %}
<h1>Gestion du stock</h1>
<p>Ceci est l'outil de gestion de stock du <a href='https://www.tetalab.org'>Tetalab</a>.</p>
<p>Cet outil vous permet:
<ul>
<li>De gérer la liste des composants électroniques en possession du Tetalab</li>
<li>De gérer la liste des fournisseurs de composants</li>
<li>De gérer la liste des kits de montage</li>
</ul>
</p>
{% endblock %}
</div>
</div>
<div class='footer'>
Tetalab - Le Hacker Space Toulousaing' Putaing' Cong' -
</div>
</div>
</div>
</body>
</html>

View File

@ -1,6 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 # -*- coding: utf-8
import os
import datetime
import bcrypt
import binascii
import math import math
import psycopg2 import psycopg2
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
@ -49,111 +53,211 @@ class Stock_providers(db.Model):
class Stock_kits(db.Model): class Stock_kits(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text, nullable=False) name = db.Column(db.Text, nullable=False)
description = db.Column(db.Text, nullable=False) designation = db.Column(db.Text, nullable=False)
class Stock_kit_compositions(db.Model): class Stock_kit_compositions(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
kit_id = db.Column(db.Integer, db.ForeignKey('Stock_kits.id'), nullable=False) kit_id = db.Column(db.Integer, db.ForeignKey('Stock_kits.id'), nullable=False)
component_id = db.Column(db.Integer, db.ForeignKey('Stock_componants.id'), nullable=False) componant_id = db.Column(db.Integer, db.ForeignKey('Stock_componants.id'), nullable=False)
component_quantity = db.Column(db.Integer, nullable=False) quantity = db.Column(db.Integer, nullable=False)
price = db.Column(db.Integer, nullable=False)
class Stock_orders(db.Model):
id = db.Column(db.Integer, primary_key=True)
componant_id = db.Column(db.Integer, db.ForeignKey('Stock_componants.id'))
componant_quantity = db.Column(db.Integer, nullable=False)
date = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
injected = db.Column(db.Boolean, nullable=False, default=False)
def sync_cookies(response, session):
""" Sync cookies from session """
for key in session:
response.set_cookie(key, value=str(session[key]))
if key != u'session':
print '[c]', key, session[key]
def sync_session(request, session, offset_reset):
""" Sync session with cookies"""
for key in request.cookies:
try:
old = str(session[key])
session[key] = int(str(request.cookies[key].encode('utf8')))
except ValueError:
# Value is not an int, will be treated as string
session[key] = str(request.cookies[key].encode('utf8'))
except KeyError:
# Key does not exist in session
try:
session[key] = int(str(request.cookies[key].encode('utf8')))
except ValueError:
# Value is not an int, will be treated as string
session[key] = str(request.cookies[key].encode('utf8'))
if key in offset_reset and len(str(session[key])) > 0 and old != str(session[key]):
session['c_offset'] = 0
session['p_offset'] = 0
session['k_offset'] = 0
if key != u'session':
print '[s]', key, request.cookies[key]
def check_user(request, session):
""" Check user credentials """
if session['token'] and request.cookies['token']:
if len(session['token']) > 0 and len(request.cookies['token']) > 0:
if session['token'] == request.cookies['token']:
return True
return False
if 'login' not in request.cookies or 'password' not in request.cookies:
return False
if request.cookies['login'] and request.cookies['password']:
if request.cookies['login'] > 0 and request.cookies['password'] > 0:
hashed = Stock_users.query.filter_by(mail=request.cookies['login']).with_entities(Stock_users.password).first()
if hashed is None:
# User is unknown
return False
password = request.cookies['password'].encode('utf8')
hashed = hashed[0].encode('utf8')
if bcrypt.checkpw(password, hashed):
session['password'] = ''
session['login'] = ''
session['token'] = binascii.hexlify(os.urandom(42))
return True
# Password mismatch
return False
def resume_session(func): def resume_session(func):
@wraps(func) @wraps(func)
def check(*args, **kwargs): def check(*args, **kwargs):
# default # Motherfuckin' bunch of defaults values
empty=u''
limit = 10 limit = 10
offset = 0 offset = 0
page = 1 page = 1
nexthop = offset + limit nexthop = offset + limit
prevhop = offset prevhop = offset
order = 'asc' order = u'asc'
order_refresh = 0 order_refresh = 0
c_empty = '' c_sort = u'reference'
c_sort = 'reference'
c_provider = 0 c_provider = 0
c_count = 0 c_count = 0
p_empty = '' p_sort = u'name'
p_sort = 'name' p_count = 0
offset_reset = ['c_reference', 'c_designation', 'c_place', 'c_provider'] k_sort = u'name'
if not 'session' in session: k_count = 0
session['session'] = '' offset_reset = [u'c_reference', u'c_designation', u'c_place',
if not 'c_limit' in session: u'c_provider', u'p_name', u'p_address', u'p_mail', u'p_url',
session['c_limit'] = limit u'k_name', u'k_designation']
if not 'c_offest' in session: if not u'token' in session:
session['c_offset'] = offset session[u'token'] = empty
if not 'c_sort' in session: if not u'password' in session:
session[u'password'] = empty
if not u'login' in session:
session[u'login'] = empty
if not u'session' in session:
session[u'session'] = empty
if not u'c_limit' in session:
session[u'c_limit'] = limit
if not u'c_offest' in session:
session[u'c_offset'] = offset
if not u'c_sort' in session:
session['c_sort'] = c_sort session['c_sort'] = c_sort
if not 'c_order' in session: if not u'c_order' in session:
session['c_order'] = order session[u'c_order'] = order
if not 'c_order_refresh' in session: if not u'c_order_refresh' in session:
session['c_order_refresh'] = order_refresh session[u'c_order_refresh'] = order_refresh
if not 'c_page' in session: if not u'c_page' in session:
session['c_page'] = page session[u'c_page'] = page
if not 'c_nexthop' in session: if not u'c_nexthop' in session:
session['c_nexthop'] = nexthop session[u'c_nexthop'] = nexthop
if not 'c_prevhop' in session: if not u'c_prevhop' in session:
session['c_prevhop'] = prevhop session['c_prevhop'] = prevhop
if not 'c_reference' in session: if not u'c_reference' in session:
session['c_reference'] = c_empty session[u'c_reference'] = empty
if not 'c_designation' in session: if not u'c_designation' in session:
session['c_designation'] = c_empty session[u'c_designation'] = empty
if not 'c_place' in session: if not u'c_place' in session:
session['c_place'] = c_empty session[u'c_place'] = empty
if not 'c_provider' in session: if not u'c_provider' in session:
session['c_provider'] = c_provider session[u'c_provider'] = c_provider
if not 'c_count' in session: if not u'c_count' in session:
session['c_count'] = c_count session[u'c_count'] = c_count
if not 'p_sort' in session: if not u'p_sort' in session:
session['p_sort'] = p_sort session[u'p_sort'] = p_sort
if not 'p_order' in session: if not u'p_order' in session:
session['p_order'] = order session[u'p_order'] = order
if not 'p_order_refresh' in session: if not u'p_order_refresh' in session:
session['p_order_refresh'] = order_refresh session[u'p_order_refresh'] = order_refresh
if not 'p_page' in session: if not u'p_page' in session:
session['p_page'] = page session[u'p_page'] = page
if not 'p_nexthop' in session: if not u'p_nexthop' in session:
session['p_nexthop'] = nexthop session[u'p_nexthop'] = nexthop
if not 'p_prevhop' in session: if not u'p_prevhop' in session:
session['p_prevhop'] = prevhop session[u'p_prevhop'] = prevhop
if not 'p_offset' in session: if not u'p_offset' in session:
session['p_offset'] = offset session[u'p_offset'] = offset
if not 'p_limit' in session: if not u'p_limit' in session:
session['p_limit'] = limit session[u'p_limit'] = limit
if not 'p_name' in session: if not u'p_name' in session:
session['p_name'] = p_empty session[u'p_name'] = empty
if not 'p_address' in session: if not u'p_address' in session:
session['p_address'] = p_empty session[u'p_address'] = empty
if not 'p_mail' in session: if not u'p_mail' in session:
session['p_mail'] = p_empty session[u'p_mail'] = empty
if not 'p_url' in session: if not u'p_url' in session:
session['p_url'] = p_empty session[u'p_url'] = empty
if not 'p_comment' in session: if not u'p_comment' in session:
session['p_comment'] = p_empty session[u'p_comment'] = empty
if not u'p_count' in session:
session['p_count'] = p_count
if not u'k_sort' in session:
session[u'k_sort'] = k_sort
if not u'k_order' in session:
session[u'k_order'] = order
if not u'k_order_refresh' in session:
session[u'k_order_refresh'] = order_refresh
if not u'k_page' in session:
session[u'k_page'] = page
if not u'k_nexthop' in session:
session[u'k_nexthop'] = nexthop
if not u'k_prevhop' in session:
session[u'k_prevhop'] = prevhop
if not u'k_offset' in session:
session[u'k_offset'] = offset
if not u'k_limit' in session:
session[u'k_limit'] = limit
if not u'k_name' in session:
session[u'k_name'] = empty
if not u'k_address' in session:
session[u'k_address'] = empty
if not u'k_mail' in session:
session[u'k_mail'] = empty
if not u'k_url' in session:
session[u'k_url'] = empty
if not u'k_designation' in session:
session[u'k_designation'] = empty
if not u'k_count' in session:
session[u'k_count'] = k_count
# Cookies/session sync # Cookies/session sync
for key in request.cookies: sync_session(request, session, offset_reset)
old = str(session[key])
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 in offset_reset and len(str(session[key])) > 0 and old != str(session[key]):
session['c_offset'] = 0
if key != 'session':
print '[s]', key, request.cookies[key]
# Switch sort order # Switch sort order
if session['c_order_refresh'] == 1: refresh = {u'desc': u'asc', u'asc': u'desc'}
refresh = {'desc': 'asc', 'asc': 'desc'} if session[u'c_order_refresh'] == 1:
session['c_order'] = refresh[session['c_order']] session[u'c_order'] = refresh[session[u'c_order']]
session['c_order_refresh'] = 0 session[u'c_order_refresh'] = 0
if session['p_order_refresh'] == 1: if session[u'p_order_refresh'] == 1:
refresh = {'desc': 'asc', 'asc': 'desc'} session[u'p_order'] = refresh[session[u'p_order']]
session['p_order'] = refresh[session['p_order']] session[u'p_order_refresh'] = 0
session['p_order_refresh'] = 0 if session[u'k_order_refresh'] == 1:
session[u'k_order'] = refresh[session[u'k_order']]
session[u'k_order_refresh'] = 0
# Check for valid session
if not check_user(request, session):
# User is not logged in, send him back to login page
return render_template('login.html')
# Everything's fine
return func(*args, **kwargs) return func(*args, **kwargs)
return check return check
@ -164,9 +268,12 @@ def resume_session(func):
def page_not_found(e): def page_not_found(e):
return render_template('error.html'), 404 return render_template('error.html'), 404
@app.route("/") @app.route("/", methods=['GET', 'POST'])
@resume_session
def authenticate(): def authenticate():
return render_template('index.html') response = app.make_response(render_template('index.html'))
sync_cookies(response, session)
return response
######################################################################## ########################################################################
@ -180,10 +287,10 @@ def componants():
providers = Stock_providers.query.order_by(Stock_providers.id).all() providers = Stock_providers.query.order_by(Stock_providers.id).all()
return render_template('componants.html', return render_template('componants.html',
providers=providers, providers=providers,
reference=session['c_reference'], reference=session[u'c_reference'].decode('utf8'),
designation=session['c_designation'], designation=session[u'c_designation'].decode('utf8'),
place=session['c_place'], place=session[u'c_place'].decode('utf8'),
provider_id=session['c_provider']) provider_id=session[u'c_provider'])
@app.route('/componants/<componant_id>') @app.route('/componants/<componant_id>')
@resume_session @resume_session
@ -229,26 +336,32 @@ def delete_componant(componant_id):
except ValueError as e: except ValueError as e:
return render_template('error.html'), 404 return render_template('error.html'), 404
except Exception as e: except Exception as e:
print "[+] Error at delete_componant:\n------------------------------\n%s------------------------------" % e.message print "[+] Error at delete_componant:"
print "------------------------------"
print "%s" % e.message
print "------------------------------"
return componants() return componants()
@app.route('/componants/new', methods=['POST']) @app.route('/componants/new', methods=['POST'])
@resume_session @resume_session
def new_componant(): def new_componant():
""" Add componant """ """ Add componant """
componant = Stock_componants(reference=session['c_reference'], componant = Stock_componants(reference=session[u'c_reference'].decode('utf8'),
designation=session['c_designation'], designation=session[u'c_designation'].decode('utf8'),
last_price=0, last_price=0,
mean_price=0, mean_price=0,
quantity=0, quantity=0,
min_quantity=0, min_quantity=0,
place=session['c_place'], place=session[u'c_place'].decode('utf8'),
provider_id=session['c_provider']) provider_id=session[u'c_provider'])
try: try:
db.session.add(componant) db.session.add(componant)
commit = db.session.commit() commit = db.session.commit()
except Exception as e: except Exception as e:
print "[+] Error at new_componant:\n------------------------------\n%s------------------------------" % e.message print "[+] Error at new_componant:"
print "------------------------------"
print "%s" % e.message
print "------------------------------"
return 'KO' return 'KO'
if commit != None: if commit != None:
return 'KO' return 'KO'
@ -272,56 +385,53 @@ def out_componants():
def update_componants(): def update_componants():
""" Display componants list """ """ Display componants list """
# search by reference # search by reference
like = '%s%s%s' % ('%', str(session['c_reference']), '%') like = '%s%s%s' % ('%', str(session[u'c_reference']), '%')
componants = Stock_componants.query.filter(Stock_componants.reference.like(like)) componants = Stock_componants.query.filter(Stock_componants.reference.like(like))
# search by designation # search by designation
like = '%s%s%s' % ('%', str(session['c_designation']), '%') like = '%s%s%s' % ('%', str(session[u'c_designation']), '%')
componants = componants.filter(Stock_componants.designation.like(like)) componants = componants.filter(Stock_componants.designation.like(like))
# search by place # search by place
like = '%s%s%s' % ('%', str(session['c_place']),'%') like = '%s%s%s' % ('%', str(session[u'c_place']),'%')
componants = componants.filter(Stock_componants.place.like(like)) componants = componants.filter(Stock_componants.place.like(like))
# search by provider # search by provider
if session['c_provider'] > 1: if session['c_provider'] > 1:
componants = componants.filter_by(provider_id=session['c_provider']) componants = componants.filter_by(provider_id=session[u'c_provider'])
# Pages calculation # Pages calculation
session['c_count'] = componants.count() session[u'c_count'] = componants.count()
session['c_pagecount'] = int(math.ceil(session['c_count'] / float(session['c_limit']))) session[u'c_pagecount'] = int(math.ceil(session[u'c_count'] / float(session[u'c_limit'])))
session['c_page'] = int(math.ceil(float(float(session['c_offset']) + 1) / float(session['c_limit']))) session[u'c_page'] = int(math.ceil(float(float(session[u'c_offset']) + 1) / float(session[u'c_limit'])))
if session['c_page'] > session['c_pagecount']: if session[u'c_page'] > session[u'c_pagecount']:
session['c_page'] = session['c_pagecount'] session[u'c_page'] = session[u'c_pagecount']
session['c_offset'] = 0 session[u'c_offset'] = 0
session['c_nexthop'] = session['c_offset'] + session['c_limit'] session[u'c_nexthop'] = session[u'c_offset'] + session[u'c_limit']
if session['c_nexthop'] > session['c_count'] - 1: if session[u'c_nexthop'] > session[u'c_count'] - 1:
session['c_nexthop'] = int(session['c_offset']) session[u'c_nexthop'] = int(session[u'c_offset'])
session['c_prevhop'] = int(session['c_offset']) - session['c_limit'] session[u'c_prevhop'] = int(session[u'c_offset']) - session[u'c_limit']
if session['c_prevhop'] < 0: if session[u'c_prevhop'] < 0:
session['c_prevhop'] = 0 session[u'c_prevhop'] = 0
# Sorting # Sorting
sort = getattr(Stock_componants, session['c_sort']) sort = getattr(Stock_componants, session[u'c_sort'])
if session['c_order'] == 'desc': if session[u'c_order'] == u'desc':
sort = getattr(Stock_componants, session['c_sort']).desc() sort = getattr(Stock_componants, session[u'c_sort']).desc()
componants = componants.order_by(sort) componants = componants.order_by(sort)
# Applying offset # Applying offset
componants = componants.offset(session['c_offset']) componants = componants.offset(session[u'c_offset'])
# Applying limit # Applying limit
componants = componants.limit(session['c_limit']) componants = componants.limit(session[u'c_limit'])
# Get result # Get result
componants = componants.all() componants = componants.all()
response = app.make_response(render_template('result_componants.html', response = app.make_response(render_template('result_componants.html',
componants=componants, componants=componants,
offset=session['c_offset'] , offset=session[u'c_offset'] ,
nexthop=session['c_nexthop'], nexthop=session[u'c_nexthop'],
prevhop=session['c_prevhop'], prevhop=session[u'c_prevhop'],
page_count=session['c_pagecount'], page_count=session[u'c_pagecount'],
page=session['c_page'], page=session[u'c_page'],
sort=session['c_sort'], sort=session[u'c_sort'].decode('utf8'),
order=session['c_order'], order=session[u'c_order'].decode('utf8'),
row_count=session['c_count'])) row_count=session[u'c_count']))
for key in session: sync_cookies(response, session)
response.set_cookie(key, value=str(session[key]))
if key != 'session':
print '[c]', key, session[key]
return response return response
######################################################################## ########################################################################
@ -331,11 +441,11 @@ def update_componants():
@resume_session @resume_session
def providers(): def providers():
return render_template('providers.html', return render_template('providers.html',
name=session['p_name'], name=session[u'p_name'].decode('utf8'),
address=session['p_address'], address=session[u'p_address'].decode('utf8'),
mail=session['p_mail'], mail=session[u'p_mail'].decode('utf8'),
url=session['p_url'], url=session[u'p_url'].decode('utf8'),
comment=session['p_comment']) comment=session[u'p_comment'].decode('utf8'))
@app.route('/providers/<provider_id>') @app.route('/providers/<provider_id>')
@resume_session @resume_session
@ -372,17 +482,19 @@ def update_provider(provider_id):
@resume_session @resume_session
def new_provider(): def new_provider():
""" Add provider """ """ Add provider """
provider = Stock_providers(name=session['p_name'], provider = Stock_providers(name=session[u'p_name'].decode('utf8'),
address=session['p_address'], address=session[u'p_address'].decode('utf8'),
mail=session['p_mail'], mail=session[u'p_mail'].decode('utf8'),
url=session['p_url'], url=session[u'p_url'].decode('utf8'),
comment=session['p_comment']) comment=session[u'p_comment'].decode('utf8'))
try: try:
db.session.add(provider) db.session.add(provider)
commit = db.session.commit() commit = db.session.commit()
except Exception as e: except Exception as e:
print 'Error at new_provider():', e print "[+] Error at new_provider:"
print 'Commit = ', commit print "------------------------------"
print "%s" % e.message
print "------------------------------"
return 'KO' return 'KO'
if commit != None: if commit != None:
return 'KO' return 'KO'
@ -399,7 +511,10 @@ def delete_provider(provider_id):
except ValueError as e: except ValueError as e:
return render_template('error.html'), 404 return render_template('error.html'), 404
except Exception as e: except Exception as e:
print "[+] Error at delete_provider:\n-----------------------------\n%s-----------------------------" % e.message print "[+] Error at delete_provider:"
print "------------------------------"
print "%s" % e.message
print "------------------------------"
return providers() return providers()
@app.route('/providers/update', methods=['POST']) @app.route('/providers/update', methods=['POST'])
@ -407,69 +522,188 @@ def delete_provider(provider_id):
def search_providers(): def search_providers():
""" Display componants list """ """ Display componants list """
# search by reference # search by reference
like = '%s%s%s' % ('%', str(session['p_name']), '%') like = '%s%s%s' % ('%', str(session[u'p_name']), '%')
providers = Stock_providers.query.filter(Stock_providers.name.like(like)) providers = Stock_providers.query.filter(Stock_providers.name.like(like))
# search by designation # search by address
like = '%s%s%s' % ('%', str(session['p_address']), '%') like = '%s%s%s' % ('%', str(session[u'p_address']), '%')
providers = providers.filter(Stock_providers.address.like(like)) providers = providers.filter(Stock_providers.address.like(like))
# search by place # search by place
like = '%s%s%s' % ('%', str(session['p_mail']),'%') like = '%s%s%s' % ('%', str(session[u'p_mail']),'%')
providers = providers.filter(Stock_providers.mail.like(like)) providers = providers.filter(Stock_providers.mail.like(like))
# search by place # search by place
like = '%s%s%s' % ('%', str(session['p_url']),'%') like = '%s%s%s' % ('%', str(session[u'p_url']),'%')
providers = providers.filter(Stock_providers.url.like(like)) providers = providers.filter(Stock_providers.url.like(like))
# search by place # search by place
like = '%s%s%s' % ('%', str(session['p_comment']),'%') like = '%s%s%s' % ('%', str(session[u'p_comment']),'%')
providers = providers.filter(Stock_providers.comment.like(like)) providers = providers.filter(Stock_providers.comment.like(like))
# Don't take 'all' and 'none' entry # Don't take 'all' and 'none' entry
providers = providers.filter(Stock_providers.id > 2) providers = providers.filter(Stock_providers.id > 2)
# Pages calculation # Pages calculation
session['p_count'] = providers.count() session[u'p_count'] = providers.count()
session['p_pagecount'] = int(math.ceil(session['p_count'] / float(session['p_limit']))) session[u'p_pagecount'] = int(math.ceil(session[u'p_count'] / float(session[u'p_limit'])))
session['p_page'] = int(math.ceil(float(float(session['p_offset']) + 1) / float(session['p_limit']))) session[u'p_page'] = int(math.ceil(float(float(session[u'p_offset']) + 1) / float(session[u'p_limit'])))
if session['p_page'] > session['p_pagecount']: if session[u'p_page'] > session[u'p_pagecount']:
session['p_page'] = session['p_pagecount'] session[u'p_page'] = session[u'p_pagecount']
session['p_offset'] = 0 session[u'p_offset'] = 0
session['p_nexthop'] = session['p_offset'] + session['p_limit'] session[u'p_nexthop'] = session[u'p_offset'] + session[u'p_limit']
if session['p_nexthop'] > session['p_count'] - 1: if session[u'p_nexthop'] > session[u'p_count'] - 1:
session['p_nexthop'] = int(session['p_offset']) session[u'p_nexthop'] = int(session[u'p_offset'])
session['p_prevhop'] = int(session['p_offset']) - session['p_limit'] session[u'p_prevhop'] = int(session[u'p_offset']) - session[u'p_limit']
if session['p_prevhop'] < 0: if session[u'p_prevhop'] < 0:
session['p_prevhop'] = 0 session[u'p_prevhop'] = 0
# Sorting # Sorting
sort = getattr(Stock_providers, session['p_sort']) sort = getattr(Stock_providers, session[u'p_sort'])
if session['p_order'] == 'desc': if session[u'p_order'] == u'desc':
sort = getattr(Stock_providers, session['p_sort']).desc() sort = getattr(Stock_providers, session[u'p_sort']).desc()
providers = providers.order_by(sort) providers = providers.order_by(sort)
# Applying offset # Applying offset
providers = providers.offset(session['p_offset']) providers = providers.offset(session[u'p_offset'])
# Applying limit # Applying limit
providers = providers.limit(session['p_limit']) providers = providers.limit(session[u'p_limit'])
# Get result # Get result
providers = providers.all() providers = providers.all()
response = app.make_response(render_template('result_providers.html', response = app.make_response(render_template('result_providers.html',
providers=providers, providers=providers,
offset=session['p_offset'] , offset=session[u'p_offset'] ,
nexthop=session['p_nexthop'], nexthop=session[u'p_nexthop'],
prevhop=session['p_prevhop'], prevhop=session[u'p_prevhop'],
page_count=session['p_pagecount'], page_count=session[u'p_pagecount'],
page=session['p_page'], page=session[u'p_page'],
sort=session['p_sort'], sort=session[u'p_sort'].decode('utf8'),
order=session['p_order'], order=session[u'p_order'].decode('utf8'),
row_count=session['p_count'])) row_count=session[u'p_count']))
for key in session: sync_cookies(response, session)
response.set_cookie(key, value=str(session[key]))
if key != 'session':
print '[c]', key, session[key]
return response return response
######################################################################## ########################################################################
# Kits # Kits
######################################################################## ########################################################################
@app.route('/kits', methods=['GET', 'POST'])
@resume_session
def kits():
return render_template('kits.html',
name=session[u'k_name'].decode('utf8'),
designation=session[u'k_designation'].decode('utf8'))
@app.route('/kits/<kit_id>')
@resume_session
def get_kit(kit_id):
""" Edit kit """
try:
kit_id = int(kit_id)
except ValueError as e:
return render_template('error.html'), 404
kit = Stock_kits.query.filter_by(id=kit_id).first()
if kit:
return render_template('kit.html', kit=kit)
return render_template('error.html'), 404
@app.route('/kits/update/<kit_id>', methods=['POST'])
@resume_session
def update_kit(kit_id):
""" Update kit field"""
field = request.form['field']
value = request.form['value']
if field and value:
try:
kit = Stock_kits.query.filter_by(id=kit_id).first()
setattr(kit, field, value)
commit = db.session.commit()
if commit == None:
return 'OK'
except Exception as e:
pass
return 'KO'
@app.route('/kits/new', methods=['POST'])
@resume_session
def new_kit():
""" Add kit """
kit = Stock_kits(name=session[u'k_name'].decode('utf8'),
designation=session[u'k_designation'].decode('utf8'))
try:
db.session.add(kit)
commit = db.session.commit()
except Exception as e:
print "[+] Error at new_kit:"
print "------------------------------"
print "%s" % e.message
print "------------------------------"
return 'KO'
if commit != None:
return 'KO'
return 'OK'
@app.route('/kits/delete/<kit_id>')
@resume_session
def delete_kit(kit_id):
""" Delete kit """
try:
kit_id = int(kit_id)
Stock_kit_compositions.query.filter_by(kit_id=kit_id).delete()
Stock_kits.query.filter_by(id=kit_id).delete()
db.session.commit()
except ValueError as e:
return render_template('error.html'), 404
except Exception as e:
print "[+] Error at delete_kit:"
print "------------------------------"
print "%s" % e.message
print "------------------------------"
return kits()
@app.route('/kits/update', methods=['POST'])
@resume_session
def search_kits():
""" Display componants list """
# search by name
like = '%s%s%s' % ('%', str(session[u'k_name']), '%')
kits = Stock_kits.query.filter(Stock_kits.name.like(like))
# search by designation
like = '%s%s%s' % ('%', str(session[u'k_designation']), '%')
kits = kits.filter(Stock_kits.designation.like(like))
# Pages calculation
session[u'k_count'] = kits.count()
session[u'k_pagecount'] = int(math.ceil(session[u'k_count'] / float(session[u'k_limit'])))
session[u'k_page'] = int(math.ceil(float(float(session[u'k_offset']) + 1) / float(session[u'k_limit'])))
if session[u'k_page'] > session[u'k_pagecount']:
session[u'k_page'] = session[u'k_pagecount']
session[u'k_offset'] = 0
session[u'k_nexthop'] = session[u'k_offset'] + session[u'k_limit']
if session[u'k_nexthop'] > session[u'k_count'] - 1:
session[u'k_nexthop'] = int(session[u'k_offset'])
session[u'k_prevhop'] = int(session[u'k_offset']) - session[u'k_limit']
if session[u'k_prevhop'] < 0:
session[u'k_prevhop'] = 0
# Sorting
sort = getattr(Stock_kits, session[u'k_sort'].decode('utf8'))
if session[u'k_order'] == 'desc':
sort = getattr(Stock_kits, session[u'k_sort'].decode('utf8')).desc()
kits = kits.order_by(sort)
# Applying offset
kits = kits.offset(session[u'k_offset'])
# Applying limit
kits = kits.limit(session[u'k_limit'])
# Get result
kits = kits.all()
response = app.make_response(render_template('result_kits.html',
kits=kits,
offset=session[u'k_offset'] ,
nexthop=session[u'k_nexthop'],
prevhop=session[u'k_prevhop'],
page_count=session[u'k_pagecount'],
page=session[u'k_page'],
sort=session[u'k_sort'].decode('utf8'),
order=session[u'k_order'].decode('utf8'),
row_count=session[u'k_count']))
for key in session:
response.set_cookie(key, value=str(session[key]))
if key != 'session':
print '[c]', key, session[key]
return response
######################################################################## ########################################################################
# Users # Users