2017-10-19 11:57:28 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8
|
|
|
|
|
2017-10-21 10:44:11 +02:00
|
|
|
import math
|
2017-10-19 11:57:28 +02:00
|
|
|
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_pyfile('config.py')
|
2017-10-22 09:01:42 +02:00
|
|
|
app.secret_key = '446307a5f61c2bb810436b2ee0f903f2'
|
2017-10-19 11:57:28 +02:00
|
|
|
app.debug = True
|
|
|
|
app.static_url_path='/static'
|
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
|
|
|
class Stock_users(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
mail = db.Column(db.Text, nullable=False)
|
|
|
|
password = db.Column(db.Text, nullable=False)
|
|
|
|
name = db.Column(db.Text, nullable=False)
|
|
|
|
|
|
|
|
class Stock_componants(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2017-10-21 10:44:11 +02:00
|
|
|
reference = db.Column(db.Text, nullable=False)
|
|
|
|
designation = db.Column(db.Text, nullable=False)
|
2017-10-19 11:57:28 +02:00
|
|
|
last_price = db.Column(db.Float)
|
|
|
|
mean_price = db.Column(db.Float)
|
|
|
|
quantity = db.Column(db.Integer)
|
|
|
|
min_quantity = db.Column(db.Integer)
|
|
|
|
place = db.Column(db.Text, nullable=False)
|
|
|
|
provider_id = db.Column(db.Integer, nullable=False)
|
|
|
|
|
|
|
|
class Stock_providers(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.Text, nullable=False)
|
2017-10-21 10:44:11 +02:00
|
|
|
address = db.Column(db.Text)
|
2017-10-19 11:57:28 +02:00
|
|
|
url = db.Column(db.Text)
|
|
|
|
comment = db.Column(db.Text)
|
|
|
|
|
|
|
|
class Stock_kits(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.Text, nullable=False)
|
|
|
|
description = db.Column(db.Text, nullable=False)
|
|
|
|
|
|
|
|
class Stock_kit_compositions(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
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)
|
|
|
|
component_quantity = db.Column(db.Integer, nullable=False)
|
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(e):
|
|
|
|
return render_template('error.html'), 404
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def authenticate():
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
# Componants
|
|
|
|
########################################################################
|
2017-10-21 10:44:11 +02:00
|
|
|
@app.route('/componants', methods=['GET', 'POST'])
|
2017-10-19 11:57:28 +02:00
|
|
|
def get_componants():
|
2017-10-22 09:01:42 +02:00
|
|
|
if not session or not session['plop']:
|
|
|
|
session['plop']='plip'
|
|
|
|
limit = 8
|
2017-10-21 10:44:11 +02:00
|
|
|
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
|
2017-10-22 09:01:42 +02:00
|
|
|
providers = Stock_providers.query.order_by(Stock_providers.name).all()
|
2017-10-21 10:44:11 +02:00
|
|
|
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,
|
2017-10-22 09:01:42 +02:00
|
|
|
providers = providers,
|
2017-10-21 10:44:11 +02:00
|
|
|
offset=offset,
|
|
|
|
nexthop=nexthop,
|
|
|
|
prevhop=prevhop,
|
|
|
|
page_count=page_count,
|
|
|
|
page=page,
|
|
|
|
sort=sort,
|
|
|
|
order=order,
|
|
|
|
norder=norder)
|
2017-10-19 11:57:28 +02:00
|
|
|
|
|
|
|
@app.route('/componants/<componant_id>')
|
|
|
|
def get_componant(componant_id):
|
2017-10-22 09:01:42 +02:00
|
|
|
print session['plop']
|
2017-10-21 10:44:11 +02:00
|
|
|
try:
|
|
|
|
componant_id = int(componant_id)
|
|
|
|
except ValueError as e:
|
|
|
|
return render_template('error.html'), 404
|
|
|
|
componant = Stock_componants.query.filter_by(id=componant_id).first()
|
|
|
|
if componant:
|
|
|
|
providers = Stock_providers.query.order_by(Stock_providers.name).all()
|
|
|
|
provider = componant.provider_id
|
|
|
|
provider = Stock_providers.query.filter_by(id=provider).first()
|
|
|
|
return render_template('componant.html', componant=componant, providers=providers, provider=provider)
|
|
|
|
return render_template('error.html'), 404
|
2017-10-19 11:57:28 +02:00
|
|
|
|
2017-10-21 10:44:11 +02:00
|
|
|
@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()
|
|
|
|
|
|
|
|
@app.route('/componants/new/<componant_id>')
|
2017-10-19 11:57:28 +02:00
|
|
|
def add_componant(componant_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
2017-10-21 10:44:11 +02:00
|
|
|
@app.route('/componants/in/<componant_id>')
|
|
|
|
def in_componants():
|
2017-10-19 11:57:28 +02:00
|
|
|
return render_template('wip.html')
|
|
|
|
|
2017-10-21 10:44:11 +02:00
|
|
|
@app.route('/componants/out/<componant_id>')
|
|
|
|
def out_componants():
|
2017-10-19 11:57:28 +02:00
|
|
|
return render_template('wip.html')
|
|
|
|
|
2017-10-21 10:44:11 +02:00
|
|
|
@app.route('/componant/update/<componant_id>', methods=['POST'])
|
|
|
|
def update_componants(componant_id):
|
|
|
|
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()
|
|
|
|
except Exception as e:
|
|
|
|
return 'KO'
|
|
|
|
return 'OK'
|
|
|
|
|
|
|
|
|
2017-10-19 11:57:28 +02:00
|
|
|
########################################################################
|
|
|
|
# Kits
|
|
|
|
########################################################################
|
|
|
|
@app.route('/kits')
|
|
|
|
def get_kits():
|
2017-10-21 10:44:11 +02:00
|
|
|
return render_template('kits.html')
|
2017-10-19 11:57:28 +02:00
|
|
|
|
|
|
|
@app.route('/kits/<kit_id>')
|
|
|
|
def get_kit(kit_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/kits/add/<kit_id>')
|
|
|
|
def add_kit(kit_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/kits/edit/<kit_id>')
|
|
|
|
def edit_kit(kit_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/kits/delete/<kit_id>')
|
|
|
|
def delete_kit(kit_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
2017-10-21 10:44:11 +02:00
|
|
|
@app.route('/kits/search')
|
|
|
|
def search_kits():
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
2017-10-19 11:57:28 +02:00
|
|
|
########################################################################
|
|
|
|
# Providers
|
|
|
|
########################################################################
|
|
|
|
@app.route('/providers')
|
|
|
|
def get_providers():
|
2017-10-21 10:44:11 +02:00
|
|
|
return render_template('providers.html')
|
2017-10-19 11:57:28 +02:00
|
|
|
|
|
|
|
@app.route('/providers/<provider_id>')
|
|
|
|
def get_provider(provider_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/providers/add/<provider_id>')
|
|
|
|
def add_provider(provider_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/providers/edit/<provider_id>')
|
|
|
|
def edit_provider(provider_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/providers/delete/<provider_id>')
|
|
|
|
def delete_provider(provider_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
2017-10-21 10:44:11 +02:00
|
|
|
@app.route('/providers/search')
|
|
|
|
def search_providers():
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
2017-10-19 11:57:28 +02:00
|
|
|
########################################################################
|
|
|
|
# Users
|
|
|
|
########################################################################
|
|
|
|
@app.route('/users')
|
|
|
|
def get_users():
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/users/<user_id>')
|
|
|
|
def get_user(user_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/users/add/<user_id>')
|
|
|
|
def add_user(user_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/users/edit/<user_id>')
|
|
|
|
def edit_user(user_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
|
|
|
@app.route('/users/delete/<user_id>')
|
|
|
|
def delete_user(user_id):
|
|
|
|
return render_template('wip.html')
|
|
|
|
|
2017-10-21 10:44:11 +02:00
|
|
|
@app.route('/users/search')
|
|
|
|
def search_users():
|
|
|
|
return render_template('wip.html')
|
2017-10-19 11:57:28 +02:00
|
|
|
|
|
|
|
##############################################
|
2017-10-22 09:01:42 +02:00
|
|
|
# SQL
|
|
|
|
##############################################
|
|
|
|
def sql_get_distinct(query):
|
2017-10-19 11:57:28 +02:00
|
|
|
results = []
|
|
|
|
for result in query:
|
|
|
|
result = result[0]
|
|
|
|
if result not in results:
|
|
|
|
results.append(result)
|
|
|
|
return results
|
|
|
|
|
|
|
|
# Main #######################################
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-10-22 09:01:42 +02:00
|
|
|
app.run(host='0.0.0.0')
|