L'outil de gestion de stock de composants électroniques du Tetalab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

257 lines
7.8 KiB

#!/usr/bin/env python
# -*- coding: utf-8
import math
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')
app.secret_key = '446307a5f61c2bb810436b2ee0f903f2'
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)
reference = db.Column(db.Text, nullable=False)
designation = db.Column(db.Text, nullable=False)
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)
address = db.Column(db.Text)
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
########################################################################
@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)
@app.route('/componants/<componant_id>')
def get_componant(componant_id):
print session['plop']
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
@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>')
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']
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'
########################################################################
# Kits
########################################################################
@app.route('/kits')
def get_kits():
return render_template('kits.html')
@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')
@app.route('/kits/search')
def search_kits():
return render_template('wip.html')
########################################################################
# Providers
########################################################################
@app.route('/providers')
def get_providers():
return render_template('providers.html')
@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')
@app.route('/providers/search')
def search_providers():
return render_template('wip.html')
########################################################################
# 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')
@app.route('/users/search')
def search_users():
return render_template('wip.html')
##############################################
# SQL
##############################################
def sql_get_distinct(query):
results = []
for result in query:
result = result[0]
if result not in results:
results.append(result)
return results
# Main #######################################
if __name__ == '__main__':
app.run(host='0.0.0.0')