-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.py
181 lines (133 loc) · 6.04 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
from flask import Flask, session
from flask_session import Session
from flask import Flask, render_template, redirect, url_for, request, flash
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from werkzeug.security import check_password_hash, generate_password_hash
from helpers import login_required
import requests
app = Flask(__name__)
#HEROKU_URL FOR SETTING UP postgres://exbggyfzvhnnoa:a641263e7716306e5115314f58bd70c7f4ad6790d405a550e762c2e4ed1f8eb9@ec2-34-197-212-240.compute-1.amazonaws.com:5432/dd974uhsrc5dfg
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
@app.route("/")
@login_required
def index():
return render_template("uploads.html")
# Route for handling the login page logic
@app.route('/login', methods=['GET', 'POST'])
def login():
""" Log user in """
# forget any user_id
session.clear()
username = request.form.get("username")
#password = request.form.get("password") #iski jroort nhi, aage include kr diya hai
#user reached route via submitting a form through POST
if request.method == 'POST':
#ensure that username was submitted
if not request.form.get("username"):
return render_template("error.html", message="you must provide username")
#ensure that password was submitted #iski jroort nhi, aage include kr diya hai
#if not request.form.get("password"):
# return render_template("error.html", message="you must provide password")
rows = db.execute("SELECT * FROM users where username = :username", {"username": username})
result = rows.fetchone()
#ensuring username exists and password is correct
if result == None or not check_password_hash(result[2], request.form.get("password")):
return render_template("error.html", message="invalid username and/or password")
#Remember which user has logged in
session["user_id"] = result[0]
session["user_name"] = result[1]
#redirect user to home page
return redirect("/")
#if user reached the route via GET
else:
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log user out"""
#forget any user ID
session.clear()
#redirect user to login form
return redirect("/")
@app.route("/register", methods=["GET", "POST"])
def register():
""" Register user """
#forget any user_id
session.clear()
#if user reached route via POST
if request.method == "POST":
#ensure username was submitted
if not request.form.get("username"):
return render_template("error.html", message="you must provide a username")
#quering database for username
userCheck = db.execute("SELECT * FROM users WHERE username= :username",
{"username":request.form.get("username")}).fetchone()
#check if username already exist
if userCheck:
return render_template("error.html", message="this username already exists")
#ensure password was submitted
elif not request.form.get("password"):
return render_template("error.html", message="must provide password")
#ensure confirmation was submitted
elif not request.form.get("confirmation"):
return render_template("error.html", message="must provide confirmation")
#checking whether the passwords are equal
elif not request.form.get("confirmation") == request.form.get("password"):
return render_template("error.html", message="passwords didn't match")
#Hash user's password to store in DB
hashedPassword = generate_password_hash(request.form.get("password"), method='pbkdf2:sha256', salt_length=8)
#insert register into DB
db.execute("INSERT INTO users (username, hash) VALUES (:username, :password)",
{"username":request.form.get("username"),
"password":hashedPassword})
#commiting changes to database
db.commit()
flash('Account created', 'info') #ye chla nhi pta nhi kyu, bad me dekhta
#redirect user to login page
return redirect("/login")
#if user reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.html")
#KAFI MACHAYA, 150 LINES
@app.route("/handleFileUpload", methods=['POST'])
def handleFileUpload():
if 'agentsfile' in request.files:
agentsfile = request.files['agentsfile']
if agentsfile.filename != '':
agentsfile.save(os.path.join('uploadsfolder/', 'agents.txt'))
if 'commodityfile' in request.files:
commodityfile = request.files['commodityfile']
if commodityfile.filename != '':
commodityfile.save(os.path.join('uploadsfolder/', commodityfile.filename))
if 'consumersfile' in request.files:
consumersfile = request.files['consumersfile']
if consumersfile.filename != '':
consumersfile.save(os.path.join('uploadsfolder/', 'locations.txt'))
return redirect(url_for('script'))
@app.route("/script")
def script():
os.system('python3 master_file.py uploadsfolder/locations.txt uploadsfolder/agents.txt templates/finalroutes.html > outputlog.txt')
os.system('rm -rf agentdata.json finalroutes.json distances.json')
return render_template("finalroutes.html")
@app.route("/specificroute/<id>")
def spicificroute(id):
return render_template('{}.html'.format(id))
@app.route('/outputlog')
def outputlog():
file = open('outputlog.txt', 'r')
lines = file.readlines()
returnstring = ' '
for line in lines:
returnstring += (line)
returnstring += '<br>'
return returnstring