forked from Sanjeev-Kumar78/Travel-Itinerary-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
227 lines (188 loc) · 7.73 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
from flask import Flask, render_template, request, redirect, url_for, session, flash
from flask_sqlalchemy import SQLAlchemy
from flask_sitemapper import Sitemapper
import bcrypt
import requests
import datetime
import bard,os
from dotenv import load_dotenv
# Load the environment variables
load_dotenv()
api_key = os.environ.get("WEATHER_API_KEY")
secret_key = os.environ.get("SECRET_KEY")
# Initialize the app
app = Flask(__name__)
sitemapper = Sitemapper(app=app) # Create and initialize the sitemapper
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
app.secret_key = secret_key
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(120), nullable=False)
def __init__(self, name, email, password):
self.name = name
self.email = email
self.password = bcrypt.hashpw(password.encode(
'utf8'), bcrypt.gensalt()).decode('utf8')
def check_password(self, password):
return bcrypt.checkpw(password.encode('utf8'), self.password.encode('utf8'))
with app.app_context():
db.create_all()
# Weather Data
def get_weather_data(api_key: str, location: str, start_date: str, end_date: str) -> dict:
"""
Retrieves weather data from Visual Crossing Weather API for a given location and date range.
Args:
api_key (str): API key for Visual Crossing Weather API.
location (str): Location for which weather data is to be retrieved.
start_date (str): Start date of the date range in "MM/DD/YYYY" format.
end_date (str): End date of the date range in "MM/DD/YYYY" format.
Returns:
dict: Weather data in JSON format.
Raises:
requests.exceptions.RequestException: If there is an error in making the API request.
"""
# Date Formatting as per API "YYYY-MM-DD"
base_url = f"https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/{location}/{start_date}/{end_date}?unitGroup=metric&include=days&key={api_key}&contentType=json"
try:
response = requests.get(base_url)
response.raise_for_status()
data = response.json()
# print(json.dumps(data, indent=4, sort_keys=True))
return data
except requests.exceptions.RequestException as e:
print("Error:", e.__str__)
@sitemapper.include() # Include the route in the sitemap
@app.route('/', methods=["GET", "POST"])
def index():
"""
Renders the index.html template.
Returns:
The rendered index.html template.
"""
if request.method == "POST":
global source, destination, start_date, end_date
source = request.form.get("source")
destination = request.form.get("destination")
start_date = request.form.get("date")
end_date = request.form.get("return")
# Calculating the number of days
no_of_day = (datetime.datetime.strptime(end_date, "%Y-%m-%d") - datetime.datetime.strptime(start_date, "%Y-%m-%d")).days
# Process the route input here
if no_of_day < 0:
flash("Return date should be greater than the Travel date (Start date).", "danger")
return redirect(url_for("index"))
else:
try:
weather_data = get_weather_data(api_key, destination, start_date, end_date)
except requests.exceptions.RequestException as e:
flash("Error in retrieving weather data.{e.Error}", "danger")
return redirect(url_for("index"))
"""Debugging"""
# Json data format printing
# print(json.dumps(weather_data, indent=4, sort_keys=True))
plan = bard.generate_itinerary(source, destination, start_date, end_date, no_of_day)
if weather_data:
# Render the weather information in the template
return render_template("dashboard.html", weather_data=weather_data, plan=plan)
return render_template('index.html')
@sitemapper.include() # Include the route in the sitemap
@app.route('/dashboard', methods=["GET", "POST"])
def dashboard():
"""
Renders the dashboard.html template.
Returns:
The rendered dashboard.html template.
"""
return render_template('dashboard.html')
@sitemapper.include() # Include the route in the sitemap
@app.route("/login", methods=["GET", "POST"])
def login():
"""
Renders the login.html template.
Returns:
The rendered login.html template.
"""
if request.method == "POST":
email = request.form.get("email")
password = request.form.get("password")
user = User.query.filter_by(email=email).first()
if user and user.check_password(password):
session["user_id"] = user.id
session["user_name"] = user.name
session["user_email"] = user.email
flash("Login successful.", "success")
return redirect(url_for("index"))
else:
flash("Wrong email or password. Please try again or register now.", "danger")
return redirect(url_for("login"))
else:
return render_template("login.html")
@sitemapper.include() # Include the route in the sitemap
@app.route("/logout")
def logout():
"""
Logs the user out.
Returns:
Redirects to the login page.
"""
session.clear()
flash("Logged out.", "info")
return redirect(url_for("login"))
@sitemapper.include() # Include the route in the sitemap
@app.route("/register", methods=["GET", "POST"])
def register():
"""
Renders the register.html template and handles user registration.
If the request method is GET, the function renders the register.html template.
If the request method is POST, the function handles user registration by checking if the passwords match,
checking if the user already exists, and adding the user to the database if they don't exist.
Returns:
If the request method is GET, the rendered register.html template.
If the request method is POST and the user is successfully added to the database, a redirect to the login page.
If the request method is POST and the passwords don't match or the user already exists, a redirect to the login page with an error message.
"""
if request.method == "POST":
name = request.form.get("name")
email = request.form.get("email")
password = request.form.get("password")
password2 = request.form.get("password2")
if password == password2:
# Check if the user already exists
existing_user = User.query.filter_by(email=email).first()
if existing_user:
flash("User already exists. Please log in.", "danger")
return redirect("/login")
else:
user = User(name=name, email=email, password=password)
db.session.add(user)
db.session.commit()
return redirect("/login")
else:
flash("Passwords do not match.", "danger")
return redirect("/register")
else:
return render_template("register.html")
# Robots.txt
@app.route('/robots.txt')
def robots():
return render_template('robots.txt')
# Sitemap
@app.route("/sitemap.xml")
def r_sitemap():
return sitemapper.generate()
# Error handlers
@app.errorhandler(404)
def page_not_found(e):
"""
Renders the 404.html template.
Returns:
The rendered 404.html template.
"""
return render_template('404.html'), 404
# Injecting current time into all templates for copyright year automatically updation
@app.context_processor
def inject_now():
return {'now': datetime.datetime.now()}