Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, request
- from flask import redirect, url_for, flash, session, abort
- from flask_caching import Cache
- from secrets import *
- from lists import *
- from search import *
- import os, requests, re
- import pymssql, datetime, atexit
- app = Flask(__name__)
- app.config.from_object(__name__)
- app.config['SECRET_KEY'] = 'VERYSECRET!!'
- cache = Cache(app,config={'CACHE_TYPE': 'simple'})
- currentUser = ""
- cfUser = 0
- nrUser = 0
- editID = 0
- @app.route("/")
- def main():
- if not session.get('logged_in'):
- return render_template('login.html')
- else:
- if check_user() == "CF":
- return render_template('index.html', currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup)
- elif check_user() =="NR":
- return render_template('indexN.html', currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup)
- def check_user():
- if cfUser>0: return "CF"
- if nrUser>0: return "NR"
- return
- @app.route('/login', methods=['POST'])
- def login():
- global cfUser
- global nrUser
- global currentUser
- user = request.form['user']
- password = request.form['password']
- if (user in loginDict and user == "neuroth" and password == loginDict[user]):
- currentUser = user
- nrUser += 1
- session['logged_in'] = True
- elif (user in loginDict and password == loginDict[user]):
- currentUser = user
- cfUser += 1
- session['logged_in'] = True
- return main()
- @app.route("/logout", methods=['POST'])
- def logout():
- global cfUser
- global nrUser
- cfUser = 0
- nrUser = 0
- session['logged_in'] = False
- return main()
- @app.route("/clear", methods=['POST'])
- def clear():
- return render_template('add.html', currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- @app.route("/delete", methods=['POST'])
- def delete():
- return render_template('index.html', currentUser = currentUser, listLocation = listLocation,
- listStatus = listStatus, listFiliale = listFiliale, listGroup = listGroup)
- @app.route("/activity", methods=['POST'])
- def activity():
- queryActivity = """Select * from %s""" % 'Activity'
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(queryActivity)
- activityData = cursor.fetchall()
- conn.close()
- return render_template('activity.html', activityData = activityData, currentUser = currentUser)
- @app.route('/history', methods=['GET', 'POST'])
- def history():
- id_dict = (request.form.to_dict())
- fk_id = (list(id_dict.keys())[1])
- search_history = """Select * from History where Fk_ID_LagerstandIT=%s""" % fk_id
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(search_history)
- history_data = cursor.fetchall()
- count = str(len(history_data))
- conn.close()
- flash("Number of results: "+count)
- parcel_status()
- return render_template('history.html', currentUser = currentUser, history_data=history_data)
- @app.route('/new', methods=['POST'])
- def redirectAdd():
- if check_user() == "NR": return render_template('no.html')
- return render_template('add.html', listLocation = listLocation, currentUser = currentUser, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- @app.route('/add', methods=['GET', 'POST'])
- def add():
- now = str(datetime.datetime.now())
- if check_user() == "NR": return render_template('no.html')
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- isInput = 0
- data = ['0']
- if request.method == 'POST':
- serialNr = request.form["serialNr"]
- model = request.form["deviceModel"]
- comments = request.form["besonderes"]
- status = request.form.get('list_status')
- location = request.form.get('list_location')
- filiale = request.form.get('list_filiale')
- group = request.form.get('list_group')
- tag = request.form.get('list_tag')
- monat = request.form.get('list_monat')
- jahr = request.form.get('list_jahr')
- if status == "Select Status": isInput += 1
- if location == "Select Neuroth": isInput += 1
- if filiale == "Select Filiale": isInput += 1
- if group == "Select Group": isInput += 1
- if tag == "Tag Wählen": isInput += 1
- if monat == "Monat Wählen": isInput += 1
- if jahr == "Jahr Wählen": isInput += 1
- if (serialNr or model) == "":
- isInput += 1
- if isInput>0:
- flash("Please fill in all information.")
- return render_template('add.html', listLocation = listLocation, currentUser = currentUser, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- query = """INSERT INTO LagerstandIT (Lager_Bewegung, Jahr, Monat, Tag, Neuroth, Filiale, ArtikelGruppe,
- ArtikelKorrekt, SerienNr, Besonderes) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s',
- '%s')""" % (status, jahr, monat, tag, location, filiale, group, model, serialNr, comments)
- queryReplaced = query.replace("'", '"')
- queryActivity = """INSERT INTO Activity (Who, Date, Action) VALUES ('{0}', '{1}', '{2}')""".format(currentUser, now, queryReplaced)
- flash(queryActivity)
- cursor.execute(query)
- cursor.execute(queryActivity)
- conn.commit()
- cursor.close()
- data = sql_serial(serialNr)
- return render_template('add.html', data = data, currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- else:
- cursor.close()
- return render_template('add.html', currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- @app.route('/editor', methods=['POST'])
- def redirectEdit():
- global editID
- id_dict = (request.form.to_dict())
- id = (list(id_dict.keys())[1])
- editID = id
- query = """ Select * from LagerstandIT where ID=%s""" % id
- queryInfoHistory = """Select * from History where Fk_ID_LagerstandIT = %s""" % editID
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(query)
- data = cursor.fetchall()
- listData = data[0]
- cursor.execute(queryInfoHistory)
- listHistory = cursor.fetchall()
- if (len(listHistory) == 0):
- listDataHistory = ""
- else:
- listDataHistory = listHistory[(len(listHistory)-1)]
- conn.close()
- return render_template('edit.html', listDataHistory = listDataHistory, currentUser = currentUser, data = data, listData = listData, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- @app.route('/edit', methods=['POST'])
- def edit():
- now = str(datetime.datetime.now())
- queryInfo = """Select * from LagerstandIT where ID = %s""" % editID
- queryInfoHistory = """Select * from History where Fk_ID_LagerstandIT = %s""" % editID
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(queryInfo)
- list = cursor.fetchall()
- listData = list[0]
- cursor.execute(queryInfoHistory)
- listHistory = cursor.fetchall()
- if (len(listHistory) == 0):
- listDataHistory = ""
- else:
- listDataHistory = listHistory[(len(listHistory)-1)]
- if request.method == 'POST':
- serialNr = request.form['serialNr']
- neurothNr = request.form['neurothNr']
- ticketCF = request.form['ticketCF']
- ticketNR = request.form['ticketNR']
- tracking = request.form['trackingNr']
- model = request.form['deviceModel']
- comments = request.form['besonderes']
- status = request.form.get("list_status")
- statusHistory = request.form.get("list_status")
- location = request.form.get('list_location')
- filiale = request.form.get('list_filiale')
- group = request.form.get('list_group')
- tag = request.form.get('list_tag')
- monat = request.form.get('list_monat')
- jahr = request.form.get('list_jahr')
- if serialNr: serialNr = "SerienNr = '%s', " % serialNr
- if neurothNr: neurothNr = "NeurothNr = '%s', " % neurothNr
- if model: model = "ArtikelKorrekt = '%s', " % model
- if comments: comments = "Besonderes = '%s', " % comments
- if statusHistory == "Select Status": statusHistory = ""
- if status == "Select Status":
- status = ""
- else:
- status = "Lager_Bewegung = '%s', " % status
- if location == "Select Neuroth":
- location = ""
- else:
- location = "Neuroth = '%s', " % location
- if filiale == "Select Filiale":
- filiale = ""
- else:
- filiale = "Filiale = '%s', " % filiale
- if group == "Select Group":
- group = ""
- else:
- group = "ArtikelGruppe = '%s', " % group
- if tag == "Tag Wählen":
- tag = ""
- else:
- tag = "Tag = '%s', " % tag
- if monat == "Monat Wählen":
- monat = ""
- else:
- monat = "Monat = '%s', " % monat
- if jahr == "Jahr Wählen":
- jahr = ""
- else:
- jahr = "Jahr = '%s', " % jahr
- query = """UPDATE LagerstandIT SET {0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10} WHERE ID = {11}""".format(serialNr,
- neurothNr, model, comments, status, location, filiale, group, tag, monat, jahr, editID)
- """
- if (tracking or ticketCF or ticketNR) and not (serialNr or neurothNr or model or comments or status or location
- or filiale or group or tag or monat or jahr):
- pkCount = getPkCount()
- queryHistory = editHistory(statusHistory, tracking, ticketCF, ticketNR, pkCount)
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(queryHistory)
- conn.commit()
- cursor.execute(queryInfo)
- data = cursor.fetchall()
- listdata = data[0]
- conn.close()
- flash("Entered: "+queryHistory)
- return render_template('edit.html', currentUser = currentUser, listDataHistory = listDataHistory, listData = listData, data = data, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- """
- if (tracking or ticketCF or ticketNR):
- pkCount = getPkCount()
- queryHistory = editHistory(statusHistory, tracking, ticketCF, ticketNR, pkCount)
- flash("Entered: "+queryHistory)
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(queryHistory)
- queryReplaced = queryHistory.replace("'", '"')
- queryActivity = """INSERT INTO Activity (Who, Date, Action) VALUES ('{0}', '{1}', '{2}')""".format(currentUser,
- now, queryReplaced)
- cursor.execute(queryActivity)
- conn.commit()
- conn.close()
- split = re.split(' (?=WHERE)', query)
- str1 = str(split[0])
- str1 = str1[:-2]
- queryChange = str1+" "+str(split[1])
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(queryChange)
- queryReplaced = queryChange.replace("'", '"')
- queryActivity = """INSERT INTO Activity (Who, Date, Action) VALUES ('{0}', '{1}', '{2}')""".format(currentUser,
- now, queryReplaced)
- print(queryActivity)
- cursor.execute(queryActivity)
- conn.commit()
- cursor.execute(queryInfo)
- data = cursor.fetchall()
- listData = data[0]
- flash("Entered: "+queryChange)
- cursor.execute(queryInfoHistory)
- listHistory = cursor.fetchall()
- if (len(listHistory) == 0):
- listDataHistory = ""
- else:
- listDataHistory = listHistory[(len(listHistory)-1)]
- conn.close()
- return render_template('edit.html', data = data, listData = listData, listDataHistory = listDataHistory, currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- return render_template('edit.html', listDataHistory = listDataHistory, listLocation = listLocation, listData = listData, currentUser = currentUser, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- def editHistory(statusHistory, tracking, ticketCF, ticketNR, pkCount):
- count = 0
- subStat = ""
- subTrack = ""
- subTickCF = ""
- subTickNR = ""
- historyDate = "History_Date, "
- subID = "Fk_ID_LagerstandIT, "
- subIDPK = "Pk_ID_History, "
- now = "'"+str(datetime.datetime.now())+"', "
- subUser = "Activity, "
- user = "'"+currentUser+"', "
- PK = pkCount
- if statusHistory:
- count += 1
- statusHistory = "'"+statusHistory+"'"+", "
- subStat = "Movement, "
- if tracking:
- count += 1
- tracking = "'"+tracking+"'"+", "
- subTrack = "Parcel_Code, "
- if ticketCF:
- count += 1
- ticketCF = "'"+ticketCF+"'"+", "
- subTickCF = "Ticket_Number_CF, "
- if ticketNR:
- count += 1
- ticketNR = "'"+ticketNR+"'"+", "
- subTickNR = "Ticket_Number_NR, "
- query = """INSERT into History ({0}{1}{2}{3}{4}{5}{6}{7}) VALUES ({8}, {9}{10}{11}{12}{13}{14}{15})""".format(subIDPK, subStat, subTrack, subTickCF,
- subTickNR, historyDate, subUser, subID, PK, statusHistory, tracking, ticketCF, ticketNR, now, user, editID)
- split = re.split(' (?=VALUES)', query)
- str1 = str(split[0])
- str1 = str1[:-3]
- queryHistory = str1+") "+str(split[1])
- print(queryHistory)
- return queryHistory
- def getPkCount():
- query = """SELECT COUNT(*) FROM History"""
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(query)
- pkCount = cursor.fetchone()
- conn.close()
- pkCount = pkCount[0]
- pkCount += 2
- return pkCount
- @app.route('/home', methods=['POST'])
- def redirectHome():
- if check_user() == "NR":
- return render_template('indexN.html', currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup)
- return render_template('index.html', currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup, listTag = listTag, listMonat = listMonat,
- listJahr = listJahr)
- @app.route('/search', methods=['GET', 'POST'])
- def input():
- inputCount = 0
- if request.method == 'POST':
- serialNr = request.form["serialNr"]
- neurothNr = request.form["neurothNr"]
- status = request.form.get('list_status')
- location = request.form.get('list_location')
- filiale = request.form.get('list_filiale')
- group = request.form.get('list_group')
- entered = "You've entered: "
- if status != 'Select Status': inputCount += 1
- if location != 'Select Neuroth': inputCount += 1
- if filiale != 'Select Filiale': inputCount += 1
- if group != 'Select Group': inputCount += 1
- if inputCount>0:
- if status != 'Select Status':
- entered += status
- if location != 'Select Neuroth':
- entered += " ---> "+location
- if filiale != 'Select Filiale':
- entered += " ---> "+filiale
- if group != 'Select Group':
- entered += " ---> "+group
- flash(entered)
- if (inputCount == 0 and serialNr == '' and neurothNr == ""):
- data = sql_all()
- elif (serialNr == '' and neurothNr == "" and inputCount>0):
- data = sql_list(inputCount)
- elif (serialNr == "" and neurothNr != ''):
- data = sql_neuroth(neurothNr)
- elif (serialNr != "" and neurothNr == ''):
- data = sql_serial(serialNr)
- elif (serialNr != "" and neurothNr != ''):
- flash("Please enter either serial number OR neuroth number.")
- if check_user() == "NR":
- return render_template('indexN.html', currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup)
- return render_template('index.html', currentUser = currentUser, listLocation = listLocation,
- listStatus = listStatus, listFiliale = listFiliale, listGroup = listGroup)
- if check_user() == "NR":
- return render_template('indexN.html', data = data, currentUser = currentUser, listLocation = listLocation, listStatus = listStatus,
- listFiliale = listFiliale, listGroup = listGroup)
- return render_template('index.html', data=data, currentUser = currentUser, listLocation = listLocation,
- listStatus = listStatus, listFiliale = listFiliale, listGroup = listGroup)
- else:
- return render_template('index.html')
- def parcel_status():
- search_status = """select Parcel_Code,Pk_ID_History from dbo.History WHERE NOT Status='Zugestellt' OR Status IS NULL"""
- conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port)
- cursor = conn.cursor()
- cursor.execute(search_status)
- for parcel in cursor:
- try:
- post_link = 'https://www.post.ch/swisspost-tracking?formattedParcelCodes='
- post_link = post_link + str(parcel[0]) + str('&&p_language=de')
- post_session = requests.session()
- post_parcel_data = post_session.get(post_link)
- post_parcel_data_enc = post_parcel_data.content
- status_code_data = re.findall('<span class="fvEventCode">[0-9]{1,2} <\/span>', str(post_parcel_data_enc))
- status_code = re.sub('[^0-9]', '', str(status_code_data))
- if status_code == '2':
- statement_status2 = """UPDATE dbo.History SET Movement='Ausgang', Status='Packet aufgegeben' WHERE Pk_ID_History=%s""" % parcel[1]
- cursor = conn.cursor()
- cursor.execute(statement_status2)
- conn.commit()
- elif status_code == '288':
- statement_status88 = """UPDATE dbo.History Movement='Ausgang', SET Status='sortiert und weiterge.' WHERE Pk_ID_History=%s""" % parcel[1]
- cursor = conn.cursor()
- cursor.execute(statement_status88)
- conn.commit()
- elif status_code == '28812':
- statement_status12 = """UPDATE dbo.History SET Movement='Ausgang', Status='sortiert für Zustellung' WHERE Pk_ID_History=%s""" % parcel[1]
- cursor = conn.cursor()
- cursor.execute(statement_status12)
- conn.commit()
- elif status_code == '2881210':
- statement_status10 = """UPDATE dbo.History SET Movement='Ausgang', Status='ankunft abhol und Zust.' WHERE Pk_ID_History=%s""" % parcel[1]
- cursor = conn.cursor()
- cursor.execute(statement_status10)
- conn.commit()
- elif status_code == '288121040':
- statement_status40 = """UPDATE dbo.History SET Movement='Ausgang', Status='Zugestellt' WHERE Pk_ID_History=%s""" % parcel[1]
- cursor = conn.cursor()
- cursor.execute(statement_status40)
- conn.commit()
- else:
- statement_status_errors = """UPDATE dbo.History SET Movement='Ausgang', Status='%s' WHERE Pk_ID_History=%s""" % (post_link, parcel[1])
- cursor = conn.cursor()
- cursor.execute(statement_status_errors)
- conn.commit()
- except requests.exceptions.RequestException as e:
- flash(e)
- conn.close()
- @atexit.register
- def goodbye():
- print("GOODBYE")
- if __name__ == "__main__":
- app.secret_key = os.urandom(12)
- app.run(host='0.0.0.0', port=80, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement