-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
368 lines (299 loc) · 11.5 KB
/
app.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from flask import *
from pyrebase import *
from dotenv import load_dotenv
import os
from authlib.integrations.flask_client import OAuth
import database as db
import carbon
import campaignAI
import os
import google.generativeai as genai
#-----------------setting up the app------------------
app = Flask(__name__)
app.config['SECRET_KEY'] = 'SECRET_KEY'
app.secret_key = 'SECRET_KEY' #generate a secret key and use it here in this virtual env
load_dotenv()
#------------------------Database setup-----------------------------
@app.before_request
def before_request():
# Open database connection before each request
db.connect_to_database()
@app.after_request
def after_request(response):
# Close database connection after each request
db.close_connection()
return response
#--------------Authlib---------------
oauth = OAuth(app)
oauth.register(
"club",
client_id=os.getenv("OAUTH2_CLIENT_ID"),
client_secret=os.getenv("OAUTH2_CLIENT_SECRET"),
client_kwargs={"scope": "openid profile email",},
server_metadata_url=f'{os.getenv("OAUTH2_META_URL")}',
)
#------------------------firebase settingc---------------------------
firebase_config = {
'apiKey': os.getenv('APIKEY'),
'authDomain': os.getenv('AUTHDOMAIN'),
'projectId': os.getenv('PROJECTID'),
'storageBucket': os.getenv('STORAGEBUCKET'),
'messagingSenderId': os.getenv('MESSAGINGSENDERID'),
'appId': os.getenv('APPID'),
'measurementId': os.getenv('MEASUREMENTID'),
'databaseURL': ""
}
firebase = initialize_app(firebase_config)
auth = firebase.auth() #auth for the user_token
#------------------- Routing ------------------------
@app.route('/sign-up', methods=['GET','POST'])
def signup():
if request.method == 'POST':
email = request.form.get('newemail')
password = request.form.get('newpassword')
fname = request.form.get('firstname')
lname = request.form.get('lastname')
region = request.form.get('region')
phone = request.form.get('phone')
try:
name = fname + " " + lname
db.create_user(email,name,phone,0,region)
user = auth.create_user_with_email_and_password(email, password)
session['user'] = user
return redirect('/')
except Exception as e:
print("Error During Signup:", e)
flash("Something went wrong!",'error')
return redirect('/sign-up')
# Print error for debugging
else:
return render_template('login.html')
@app.route("/signin-google")
def googleCallback():
try:
# fetch access token and id token using authorization code
token = oauth.clubsync.authorize_access_token()
# print(token,"\n\n",type(token))
token = dict(token)
# print(json.dumps(token, indent = 4))
# Extract necessary user data from the ID token
personal = token.get('userinfo')
user_info = {"name" : personal.get('name'),
"email" : personal.get('email'),
"idToken" : token.get('id_token')}
# Set complete user information in the session
session["user"] = user_info
return redirect('/homepage')
except Exception as e:
print("Error during google callback:", e)
return redirect('/sign-up')
@app.route("/google-login")
def googleLogin():
try:
if "user" in session:
abort(404)
return oauth.clubsync.authorize_redirect(redirect_uri=url_for("googleCallback", _external=True))
except Exception as e:
flash("Error during google login:", "Failure")
print(e)
return redirect('/signup')
@app.route('/login', methods=['GET','POST'])
def login():
if request.method == 'POST':
try:
email = request.form.get('email')
password = request.form.get('password')
user = auth.sign_in_with_email_and_password(email, password)
name = db.fetch_user_name(email)
user['name'] = name
session['user'] = user
return redirect('/')
except Exception as e:
print("Error during login:", e) # Print error for debugging
login_error = "Invalid email or password. Please try again."
flash(login_error)
return render_template('login.html')
else:
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('user', None)
session.clear()
flash("Logged out successfully!","Success")
return redirect('/')
@app.route('/forgot-password', methods=['GET','POST'])
def forgotPassword():
if request.method == 'POST':
try:
email = request.form.get('user_email')
try:
auth.send_password_reset_email(email)
flash("Password reset link sent to your email!","Success")
return redirect('/login')
except Exception as e:
print("Error during password reset:", e)
flash("Something went wrong!","Error")
return redirect('/login')
except Exception as e:
print("Error during password reset:", e)
flash("Something went wrong!","Error")
return redirect('/login')
return render_template('forgotPassword.html')
@app.route('/')
def homepage():
"""
"""
if 'user' in session:
#signed user
user_id_token = session['user']
try:
email = session["user"].get('email')
name = db.fetch_user_name(email)
return render_template('index.html',name = name)
except KeyError as e:
flash(e,"Something went wrong!")
else:
return render_template('withoutindex.html')
@app.route('/ongoing-campaigns')
def campaigns():
if "user" in session:
return render_template('ongoingcmp.html')
else:
return redirect('/login')
@app.route('/campaign1')
def campaign1():
if "user" in session:
try:
campaignAI.waste_management_campaign()
return redirect('/ongoing-campaigns')
except Exception as e:
print("Error during campaign creation:", e)
flash("Something went wrong!", "Error")
return redirect('/ongoing-campaigns')
else:
return redirect('/login')
@app.route('/campaign2')
def campaign2():
if "user" in session:
try:
campaignAI.water_conservation_campaign()
return redirect('/ongoing-campaigns')
except Exception as e:
print("Error during campaign creation:", e)
flash("Something went wrong!", "Error")
return redirect('/ongoing-campaigns')
else:
return redirect('/login')
@app.route('/campaign3')
def campaign3():
if "user" in session:
try:
campaignAI.sustainable_development_campaign()
return redirect('/ongoing-campaigns')
except Exception as e:
print("Error during campaign creation:", e)
flash("Something went wrong!", "Error")
return redirect('/ongoing-campaigns')
else:
return redirect('/login')
@app.route('/campaign4')
def campaign4():
if "user" in session:
try:
campaignAI.public_healthcare_campaign()
return redirect('/ongoing-campaigns')
except Exception as e:
print("Error during campaign creation:", e)
flash("Something went wrong!", "Error")
return redirect('/ongoing-campaigns')
else:
return redirect('/login')
@app.route('/Green-O-Gram')
def space():
if "user" in session:
try:
posts = db.fetch_social_posts()
print(posts)
return render_template('communitypost.html', posts=posts)
except Exception as e:
print("Error during fetching posts:", e)
flash("Something went wrong!", "Error")
return redirect('/')
else:
return redirect('/login')
@app.route('/post', methods=['GET','POST'])
def post():
if "user" in session:
if request.method == 'POST':
try:
post = request.form.get('caption')
email = session["user"].get('email')
title = request.form.get('title')
db.create_social_post(email, post, "none", title)
return redirect('/Green-O-Gram')
except Exception as e:
print("Error during post creation:", e)
flash("Something went wrong!", "Error")
return redirect('/Green-O-Gram')
else:
return render_template('photouplode.html')
else:
return redirect('/login')
@app.route('/carbon-footprint', methods=['GET','POST'])
def carbon_footprint():
if "user" in session:
user = session["user"]
email = user["email"]
if request.method == 'POST':
try:
electricity_kWh = float(request.form.get('electricity'))
fuel_liters = float(request.form.get('fuel'))
waste_kg = float(request.form.get('waste'))
flight_hours = float(request.form.get('flight'))
total_carbon_footprint = carbon.calculate_carbon_footprint(electricity_kWh, fuel_liters, waste_kg, flight_hours, 'india')
print(total_carbon_footprint,type(total_carbon_footprint))
db.update_user_cfp(email, total_carbon_footprint)
try:
# Fetch table names and check if user's table exists
table_names = [table[0] for table in db.fetch_table_names()]
print(f"Fetched table names: {table_names}")
if f"{email}_cfp" in table_names:
db.track_user_cfp(email, total_carbon_footprint)
else:
db.create_user_cfp_table(email)
db.track_user_cfp(email, total_carbon_footprint)
except Exception as e:
print(f"Error during carbon footprint tracking: {e}")
flash("Something went wrong!", "Error")
return redirect('/carbon-footprint')
return render_template('footprint.html', total_carbon_footprint=total_carbon_footprint)
return render_template('footprint.html', total_carbon_footprint=total_carbon_footprint)
except Exception as e:
print("Error during carbon footprint calculation:", e)
flash("Something went wrong!","Error")
return redirect('/carbon-footprint')
return render_template('footprint.html')
else:
return render_template('footprint.html')
else:
return redirect('/login')
genai.configure(api_key=os.environ.get("API_KEY_GENAI"))
@app.route('/chat', methods=['POST'])
def chat():
data = request.json
user_message = data.get("message")
print(user_message)
if not user_message:
return jsonify({"error": "No message provided"}), 400
try:
# Define the Gemini AI model
model = genai.GenerativeModel('gemini-1.5-flash')
# Generate the response
response = model.generate_content(user_message)
bot_message = response.text.strip()
print(bot_message)
except Exception as e:
return jsonify({"error": str(e)}), 500
return jsonify({"message": bot_message})
if __name__ == '__main__':
app.run(debug=True)