def login(): error = '' try: c, conn = cursor_conn() role_dict = { "Login as Reporter": "report", "Login as Relief Worker": "relief" } if request.method == "POST": c.execute("USE FLASKAPP;") data = c.execute( "SELECT password FROM users WHERE username = (%s) and role = (%s) ", (thwart(request.form['username']), thwart(role_dict[request.form['action']]))) data = c.fetchone()['password'] if sha256_crypt.verify(request.form['password'], data): session['logged_in'] = role_dict[request.form['action']] session['username'] = request.form['username'] print(session) flash("Logged in successfully") return redirect(url_for('homepage')) else: error = "Invalid credentials, try again." gc.collect() flash(error) return render_template("login.html") except Exception as e: error = "Account does not exist. Click signup to create one." flash(error) return render_template("login.html") return render_template('login.html')
def check(): try: c, conn = cursor_conn() x = c.execute( "SELECT mobile FROM FLASKAPP.users WHERE username = (%s)", (thwart(session['username']))) usermobile = c.fetchone()['mobile'] print(usermobile) c.execute( "SELECT name, mobile, status from FLASKAPP.victims WHERE reporterMobile = (%s)", (thwart(usermobile))) result = c.fetchall() if len(result) == 0: flash("No victims reported") return render_template('report.html') c.close() conn.close() gc.collect() return render_template("check.html", result=result) except Exception as e: print("error") flash("No victims reported") return render_template('report.html')
def update(): try: if request.method == "POST": mobile = request.form["mobile"] if mobile == "": flash("Please fill corect data") return render_template('update.html') c, conn = cursor_conn() x = c.execute("SELECT * FROM FLASKAPP.victims WHERE mobile = (%s)", (thwart(mobile))) if int(x) <= 0: flash("This mobile number has not been reported") return render_template("update.html") x = c.execute( "UPDATE FLASKAPP.victims SET status = %s WHERE mobile= (%s)", (thwart(request.form['action']), thwart(mobile))) conn.commit() flash("Person Status updated to {}".format(request.form['action'])) c.close() conn.close() gc.collect() return render_template("update.html") except Exception as e: raise flash("Please fill corect data") return render_template('report.html')
def relief(): c, conn = cursor_conn() c.execute('''CREATE TABLE IF NOT EXISTS FLASKAPP.relief ( username VARCHAR(55) PRIMARY KEY, latitude VARCHAR(55) NOT NULL, longitude VARCHAR(55) NOT NULL); ''') c.close() conn.close() return
def users(): c, conn = cursor_conn() c.execute('''CREATE TABLE IF NOT EXISTS FLASKAPP.users ( email VARCHAR(55) NOT NULL, password VARCHAR(100) NOT NULL, username VARCHAR(50) UNIQUE NOT NULL, name VARCHAR(20) NOT NULL, mobile VARCHAR(20) PRIMARY KEY, role ENUM('relief', 'report') NOT NULL); ''') c.close() conn.close() return
def victims(): c, conn = cursor_conn() c.execute('''CREATE TABLE IF NOT EXISTS FLASKAPP.victims ( name VARCHAR(20) NOT NULL, reporterMobile VARCHAR(20) NOT NULL, mobile VARCHAR(20) PRIMARY KEY, latitude VARCHAR(55) NOT NULL, longitude VARCHAR(55) NOT NULL, status ENUM('rescued', 'not_rescued', 'dead') NOT NULL, FOREIGN KEY (reporterMobile) REFERENCES users(mobile)); ''') c.close() conn.close() return
def register(): form = register_form(request.form) if request.method == "POST": if form.validate(): mobile = form.mobile.data email = form.email.data name = form.name.data username = form.username.data password = sha256_crypt.encrypt((str(form.password.data))) role = form.role.data c, conn = cursor_conn() x = c.execute("SELECT * FROM FLASKAPP.users WHERE username = (%s)", (thwart(username))) if int(x) > 0: flash("That username is already taken, please choose another") return render_template('register.html', form=form) else: try: c.execute( "INSERT INTO FLASKAPP.users (email, password, name, mobile, role, username) VALUES (%s, %s, %s, %s, %s, %s)", (thwart(email), thwart(password), thwart(name), thwart(mobile), thwart(role), thwart(username))) flash("Thanks for registering!") except: flash( "That Mobile number is already taken, please choose another" ) return render_template('register.html', form=form) c.close() conn.close() gc.collect() session['logged_in'] = role session['username'] = username return render_template('main.html') else: flash("Error in form. Please Fill Again") return render_template('register.html', form=form)
def helpme(): c, conn = cursor_conn() if request.method == "POST": lat = request.form['lat'] lng = request.form['lng'] # print(lat, lng) x = c.execute("SELECT latitude, longitude FROM FLASKAPP.relief;") relief_data = c.fetchall() # print(relief_data) to_display = [] for worker in relief_data: dist = latlongdist(float(lat), float(lng), float(worker['latitude']), float(worker['longitude'])) print(dist) if dist < 50: to_display.append(worker) print(to_display) # print("gg") return jsonify(to_display) return render_template('helpme.html')
def locate(): c, conn = cursor_conn() if request.method == "POST": # e = request.json["e"] # lat = e.latlng.lat # lng = request.form['lng'] lat = request.form['lat'] lng = request.form['lng'] print(lat, lng) x = c.execute("DELETE FROM FLASKAPP.relief WHERE username = (%s);", (session['username'])) conn.commit() x = c.execute( "INSERT INTO FLASKAPP.relief(username, latitude, longitude) VALUES (%s, %s, %s);", (session['username'], lat, lng)) conn.commit() print(x) x = c.execute( "SELECT name, latitude, longitude, mobile FROM FLASKAPP.victims WHERE status = 'not_rescued'" ) variable = c.fetchall() return render_template('locate.html', variable=variable)
def report(): try: if request.method == "POST": name = request.form["name"] latitude = request.form["latitude"] longitude = request.form["longitude"] mobile = request.form["mobile"] if name == "" or latitude == "" or longitude == "" or mobile == "": flash("Please fill corect data") return render_template('report.html') c, conn = cursor_conn() x = c.execute( "SELECT mobile FROM FLASKAPP.users WHERE username = (%s)", (thwart(session['username']))) usermobile = c.fetchone()['mobile'] c.execute( "INSERT INTO FLASKAPP.victims (name, reporterMobile, mobile, latitude, longitude, status) VALUES (%s, %s, %s, %s, %s, %s)", (thwart(name), thwart(usermobile), thwart(mobile), thwart(latitude), thwart(longitude), thwart("not_rescued"))) conn.commit() flash("Person Added. Relief workers will find for your loved one!") c.close() conn.close() gc.collect() return redirect(url_for('report')) return render_template("report.html") except pymysql.IntegrityError as e: flash("Person has already been reported") return render_template('report.html') except Exception as e: flash("Please fill corect data") return render_template('report.html')
def setup(): c, conn = cursor_conn() c.execute("CREATE DATABASE IF NOT EXISTS FLASKAPP")