79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
import functools
|
|
import os
|
|
|
|
from flask import(
|
|
Blueprint, current_app, flash, g, redirect, render_template, request, session, url_for
|
|
)
|
|
from werkzeug.exceptions import abort
|
|
|
|
from flaskfdx.auth import login_required
|
|
from flaskfdx.db import get_db
|
|
from flaskfdx.upload import process_file
|
|
|
|
bp = Blueprint('dashboard', __name__)
|
|
|
|
@bp.route('/')
|
|
@login_required
|
|
def index():
|
|
db = get_db()
|
|
fourweeks = db.execute(
|
|
'SELECT * FROM totals ORDER BY date DESC LIMIT 56'
|
|
).fetchall()
|
|
lastseven = {}
|
|
lastseven['Totals'] = {}
|
|
stops = 0
|
|
totstops = 0
|
|
totpkgs = 0
|
|
tottermpkgs = 0
|
|
i = 0
|
|
while len(lastseven) < 8:
|
|
if fourweeks[i]['date'] not in lastseven:
|
|
lastseven[fourweeks[i]['date']] = {}
|
|
if fourweeks[i]['entity'] == 'contract':
|
|
stops = fourweeks[i]['delstops'] + fourweeks[i]['pustops']
|
|
pkgs = fourweeks[i]['delpkgs'] + fourweeks[i]['pupkgs']
|
|
totstops += stops
|
|
totpkgs += pkgs
|
|
lastseven[fourweeks[i]['date']]['stops'] = stops
|
|
lastseven[fourweeks[i]['date']]['pkgs'] = pkgs
|
|
lastseven[fourweeks[i]['date']]['pkgsperstop'] = round(pkgs / stops, 2)
|
|
i += 1
|
|
if fourweeks[i]['entity'] == 'terminal' :
|
|
tottermpkgs += fourweeks[i]['prepkgs']
|
|
lastseven[fourweeks[i]['date']]['termpkgs'] = fourweeks[i]['prepkgs']
|
|
lastseven[fourweeks[i]['date']]['percenttermvol'] = round(100 * stops / fourweeks[i]['prepkgs'], 2)
|
|
lastseven['Totals']['pkgs'] = totpkgs
|
|
lastseven['Totals']['stops'] = totstops
|
|
lastseven['Totals']['pkgsperstop'] = round(totpkgs / totstops, 2)
|
|
lastseven['Totals']['termpkgs'] = tottermpkgs
|
|
lastseven['Totals']['percenttermvol'] = round( 100 * totstops / tottermpkgs, 2)
|
|
|
|
return render_template('dashboard/index.html', lastseven=lastseven)
|
|
|
|
def allowed_file(filename):
|
|
ALLOWED_EXTENSIONS = {'xls', 'xlsx'}
|
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
@bp.route('/upload', methods=('GET', 'POST'))
|
|
@login_required
|
|
def upload():
|
|
''' need to add upload folder and allowed extensions'''
|
|
if request.method == 'POST':
|
|
if 'file' not in request.files:
|
|
flash('No file part')
|
|
return redirect(request.url)
|
|
file = request.files['file']
|
|
if file.filename == '':
|
|
flash('No selected file')
|
|
return redirect(request.url)
|
|
if file and allowed_file(file.filename):
|
|
# todo validity check before processing
|
|
filename, error = process_file(filetype, file)
|
|
if error:
|
|
print(error)
|
|
else:
|
|
file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
|
|
|
|
return redirect(url_for('index'))
|
|
return render_template('dashboard/upload.html')
|