-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.py
More file actions
153 lines (111 loc) · 4.83 KB
/
application.py
File metadata and controls
153 lines (111 loc) · 4.83 KB
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
import os
import datetime
from flask import Flask, flash, redirect, render_template, request, session, jsonify
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions
from werkzeug.security import check_password_hash, generate_password_hash
from dotenv import load_dotenv
import sqlite3
def create_app():
load_dotenv()
conn = sqlite3.connect('alerts.db',check_same_thread=False)
db = conn.cursor()
# Configure application
app = Flask(__name__)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/")
def menu():
return render_template("menu.html")
@app.route("/commonalerts", methods=["GET", "POST"])
def commonalerts():
if request.method == "POST":
if not request.form.get("calamity"):
return "Must enter the calamity."
elif not request.form.get("location"):
return "Must enter the location"
c = request.form.get("calamity")
l = request.form.get("location")
d = request.form.get("description")
s = {"success":"yes"}
db.execute("INSERT INTO commonalerts (calamity,location,description) VALUES (?,?,?)",
(c,l,d))
conn.commit()
return "Alert Issued Successfully.",200
else:
return render_template("alerts.html")
@app.route("/getcommonalerts")
def getcommonalerts():
alerts=[]
w = db.execute("SELECT * FROM commonalerts ORDER BY id DESC ")
for w1 in w:
s = {"datetime":w1[1],"location":w1[3],"calamity":w1[2],"description":w1[4]}
alerts.append(s)
return jsonify(alerts)
@app.route("/govtalerts", methods=["GET", "POST"])
def govtalerts():
if request.method == "POST":
if not request.form.get("username"):
return "Must enter a username"
elif not request.form.get("password"):
return "Must enter the password"
elif not request.form.get("calamity"):
return "Must enter the calamity"
elif not request.form.get("location"):
return "Must enter the location"
elif not request.form.get("description"):
return "Must enter the description"
username=request.form.get("username")
password=request.form.get("password")
rows = db.execute("SELECT * FROM govtids WHERE username = ?",(username,))
row = db.fetchone()
if row is None or not check_password_hash(row[2], request.form.get("password")):
return "Invalid username and/or password"
c = request.form.get("calamity")
l = request.form.get("location")
d = request.form.get("description")
s = {"success":"yes"}
db.execute("INSERT INTO govtalerts (calamity,location,description) VALUES (?,?,?)",
(c,l,d))
conn.commit()
return "Alert Issued Successfully.",200
else:
return render_template("govtalerts.html")
@app.route("/getgovtalerts")
def getgovtalerts():
alerts=[]
w = db.execute("SELECT * FROM govtalerts ORDER BY id DESC ")
for w1 in w:
s = {"datetime":w1[1],"location":w1[3],"calamity":w1[2],"description":w1[4]}
alerts.append(s)
return jsonify(alerts)
#@app.route("/generateids")
# def generateids():
# usernames=["earthquakeagencyofindia","hurricaneagencyofindia","floodagencyofindia","disastermanagementagencyofindia"]
# passwords=["eaoi","haoi","faoi","dmaoi"]
# for i in range(4):
# db.execute("INSERT INTO govtids (username,password) VALUES (?,?)",(usernames[i],generate_password_hash(passwords[i])))
@app.route("/viewgovtalerts")
def viewgovtalerts():
alerts=[]
w = db.execute("SELECT * FROM govtalerts ORDER BY id DESC ")
for w1 in w:
s = {"datetime":w1[1],"location":w1[3],"calamity":w1[2],"description":w1[4]}
alerts.append(s)
return render_template("view.html",rows=alerts,alert="Govt Alerts")
@app.route("/viewcommonalerts")
def viewcommonalerts():
alerts=[]
w = db.execute("SELECT * FROM commonalerts ORDER BY id DESC ")
for w1 in w:
s = {"datetime":w1[1],"location":w1[3],"calamity":w1[2],"description":w1[4]}
alerts.append(s)
return render_template("view.html",rows=alerts,alert="Common Alerts")
return app
app = create_app()