"Beta release"
This commit is contained in:
75
tetastock.py
75
tetastock.py
@@ -77,6 +77,28 @@ class Stock_orders(db.Model):
|
||||
########################################################################
|
||||
# Here comes the fun
|
||||
########################################################################
|
||||
def get_max_kit(kit_id):
|
||||
""" Get max available kit from kit.id """
|
||||
default_max_kit = float("inf")
|
||||
max_kit = default_max_kit
|
||||
composition = Stock_kit_compositions.query.filter_by(kit_id=kit_id).with_entities(
|
||||
Stock_kit_compositions.componant_id,
|
||||
Stock_kit_compositions.quantity).all()
|
||||
for c_componant in composition:
|
||||
componant = Stock_componants.query.filter_by(id=c_componant.componant_id).first()
|
||||
if componant:
|
||||
try:
|
||||
mkit = int(componant.quantity) / int(c_componant.quantity)
|
||||
except ZeroDivisionError as e:
|
||||
print "[+] Error at get_max_kit:"
|
||||
print "------------------------------"
|
||||
print "%s" % e.message
|
||||
print "------------------------------"
|
||||
if mkit < max_kit:
|
||||
max_kit = mkit
|
||||
if max_kit == default_max_kit:
|
||||
max_kit = 0
|
||||
return max_kit
|
||||
|
||||
def sync_cookies(response, session):
|
||||
""" Sync cookies with session """
|
||||
@@ -85,7 +107,7 @@ def sync_cookies(response, session):
|
||||
if key != u'session':
|
||||
print '[c]', key, session[key]
|
||||
|
||||
def sync_session(request, session, offset_reset):
|
||||
def sync_session(request, session):
|
||||
""" Sync session with cookies"""
|
||||
for key in request.cookies:
|
||||
try:
|
||||
@@ -101,10 +123,6 @@ def sync_session(request, session, offset_reset):
|
||||
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]
|
||||
|
||||
@@ -132,7 +150,6 @@ def check_user(request, session):
|
||||
return True
|
||||
# Password mismatch
|
||||
return False
|
||||
|
||||
|
||||
def resume_session(func):
|
||||
""" Resume pending session """
|
||||
@@ -156,9 +173,6 @@ def resume_session(func):
|
||||
k_count = 0
|
||||
kc_quantity = 0
|
||||
kc_limit = 3
|
||||
offset_reset = [u'c_reference', u'c_designation', u'c_place',
|
||||
u'c_provider', u'p_name', u'p_address', u'p_mail', u'p_url',
|
||||
u'k_name', u'k_designation']
|
||||
if not u'token' in session:
|
||||
session[u'token'] = empty
|
||||
if not u'password' in session:
|
||||
@@ -260,8 +274,7 @@ def resume_session(func):
|
||||
if not u'kc_reference' in session:
|
||||
session[u'kc_reference'] = empty
|
||||
# Cookies/session sync
|
||||
sync_session(request, session, offset_reset)
|
||||
|
||||
sync_session(request, session)
|
||||
# Switch sort order
|
||||
refresh = {u'desc': u'asc', u'asc': u'desc'}
|
||||
if session[u'c_order_refresh'] == 1:
|
||||
@@ -273,7 +286,6 @@ def resume_session(func):
|
||||
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
|
||||
@@ -315,7 +327,7 @@ def componants():
|
||||
place=session[u'c_place'].decode('utf8'),
|
||||
provider_id=session[u'c_provider'])
|
||||
|
||||
@app.route('/componants/<componant_id>')
|
||||
@app.route('/componants/<componant_id>', methods=['GET', 'POST'])
|
||||
@resume_session
|
||||
def get_componant(componant_id):
|
||||
""" Edit componant """
|
||||
@@ -348,7 +360,7 @@ def update_componant(componant_id):
|
||||
pass
|
||||
return 'KO'
|
||||
|
||||
@app.route('/componants/delete/<componant_id>')
|
||||
@app.route('/componants/delete/<componant_id>', methods=['GET', 'POST'])
|
||||
@resume_session
|
||||
def delete_componant(componant_id):
|
||||
""" Delete componant """
|
||||
@@ -392,20 +404,20 @@ def new_componant():
|
||||
return 'KO'
|
||||
return 'OK'
|
||||
|
||||
@app.route('/componants/in/<componant_id>')
|
||||
@app.route('/componants/in')
|
||||
@resume_session
|
||||
def in_componants():
|
||||
""" Incoming order """
|
||||
return render_template('wip.html')
|
||||
|
||||
@app.route('/componants/out/<componant_id>')
|
||||
@app.route('/componants/out')
|
||||
@resume_session
|
||||
def out_componants():
|
||||
""" Outgoing order """
|
||||
return render_template('wip.html')
|
||||
|
||||
## Componants update result set
|
||||
@app.route('/componants/update', methods=['POST'])
|
||||
@app.route('/componants/update', methods=['GET', 'POST'])
|
||||
@resume_session
|
||||
def update_componants():
|
||||
""" Display componants list """
|
||||
@@ -719,6 +731,9 @@ def search_kits():
|
||||
kits = kits.limit(session[u'k_limit'])
|
||||
# Get result
|
||||
kits = kits.all()
|
||||
for kit in kits:
|
||||
max_kit = get_max_kit(kit.id)
|
||||
setattr(kit, 'max_kit', max_kit)
|
||||
response = app.make_response(render_template('result_kits.html',
|
||||
kits=kits,
|
||||
offset=session[u'k_offset'] ,
|
||||
@@ -738,9 +753,7 @@ def search_kits():
|
||||
@app.route('/kits/composition/<kit_id>', methods=['POST'])
|
||||
@resume_session
|
||||
def get_kit_composition(kit_id):
|
||||
default_max_kit = 999999999999999
|
||||
kit_composition = []
|
||||
max_kit = default_max_kit
|
||||
composition = Stock_kit_compositions.query.filter_by(kit_id=kit_id).with_entities(
|
||||
Stock_kit_compositions.componant_id,
|
||||
Stock_kit_compositions.quantity).all()
|
||||
@@ -756,20 +769,8 @@ def get_kit_composition(kit_id):
|
||||
u'needed': c_componant.quantity
|
||||
}
|
||||
kit_composition.append(c)
|
||||
try:
|
||||
mkit = int(componant.quantity) / int(c_componant.quantity)
|
||||
except ZeroDivisionError as e:
|
||||
print "[+] Error at get_kit_composition:"
|
||||
print "------------------------------"
|
||||
print "%s" % e.message
|
||||
print "------------------------------"
|
||||
if mkit < max_kit:
|
||||
max_kit = mkit
|
||||
if max_kit == default_max_kit:
|
||||
max_kit = 0
|
||||
if len(kit_composition) > 0:
|
||||
return render_template('kit_composition.html', kit_composition=kit_composition, max_kit=max_kit, kit_id=kit_id)
|
||||
return "Aucun composant"
|
||||
max_kit = get_max_kit(kit_id)
|
||||
return render_template('kit_composition.html', kit_composition=kit_composition, max_kit=max_kit, kit_id=kit_id)
|
||||
|
||||
@app.route('/kits/remove/<kit_id>/<componant_id>', methods=['GET'])
|
||||
@resume_session
|
||||
@@ -789,20 +790,20 @@ def remove_componant_from_kit(kit_id, componant_id):
|
||||
print "------------------------------"
|
||||
return get_kit(kit_id)
|
||||
|
||||
@app.route('/kits/add/<kit_id>/<componant_id>', methods=['POST', 'GET'])
|
||||
@app.route('/kits/composition/add/<kit_id>', methods=['POST', 'GET'])
|
||||
@resume_session
|
||||
def add_componant_to_kit(kit_id, componant_id):
|
||||
def add_componant_to_kit(kit_id):
|
||||
""" Add componant to kit """
|
||||
try:
|
||||
kit_id = int(kit_id)
|
||||
count = Stock_kit_compositions.query.filter_by(kit_id=kit_id, componant_id=componant_id).count()
|
||||
count = Stock_kit_compositions.query.filter_by(kit_id=kit_id, componant_id=session[u'kc_componant_id']).count()
|
||||
if count > 0:
|
||||
# Componant already in kit composition
|
||||
# => Updating with new quantity
|
||||
return update_kit_composition(kit_id)
|
||||
|
||||
composition = Stock_kit_compositions(kit_id=kit_id,
|
||||
componant_id=componant_id,
|
||||
componant_id=session[u'kc_componant_id'],
|
||||
quantity=session[u'kc_quantity'])
|
||||
db.session.add(composition)
|
||||
commit = db.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user