-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
144 lines (122 loc) · 5.14 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
from flask import Flask, request, redirect, url_for, render_template
from flask_mail import Mail, Message
from werkzeug.utils import secure_filename
import sqlite3, os, json
with open("config.json", "r") as f:
params = json.load(f)["params"]
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__, template_folder="templates")
app.config['UPLOAD_FOLDER'] = params['upload_folder']
app.static_folder = "./static"
# configuration of mail
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = params['email_id']
app.config['MAIL_PASSWORD'] = params['email_passwd']
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def send_email(subject, sender, recipient, msg):
print("-->"*20,subject, sender, recipient, msg)
mail.send_message(
subject,
sender=sender,
recipients=[recipient],
body=msg
)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/facts")
def facts():
return render_template("facts.html")
@app.route("/tourist_place")
def tourist_place():
with sqlite3.connect("Travel.db") as con:
cur = con.cursor()
info = cur.execute("SELECT * FROM Jaipur;")
con.commit()
information = [dict(name=row[1], image=row[2], desc=row[4]) for row in info]
return render_template("tourist_place.html", information=information, heading="Tourist Places")
@app.route("/category/<category>")
def categorize(category):
with sqlite3.connect("Travel.db") as con:
cur = con.cursor()
info = cur.execute(f"SELECT * FROM Jaipur WHERE tag = '{category}';")
con.commit()
information = [dict(name=row[1], image=row[2], desc=row[4]) for row in info]
more_btn = 1
if category == "gardens":
heading = "Parks / Gardens"
elif category == "wildlife":
heading = "Nature / Wildlife"
elif category == "food":
heading = "Food Delicacies"
else:
heading = category
if category in ["shopping", "food", "festivals"]:
more_btn = 0
return render_template("tourist_place.html", information=information, heading=heading, more_btn=more_btn)
@app.route("/place/<name>")
def detail(name):
## ---------------------- Working -----------------
print("--"*30,name,"--"*30)
# with sqlite3.connect("Travel.db") as con:
# cur = con.cursor()
# info = cur.execute(f"SELECT * FROM Jaipur WHERE tag = '{name}';")
# con.commit()
return render_template("more_info.html", name=name)
@app.route("/add", methods=['GET', 'POST'])
def add():
if request.method == 'POST':
username = request.form.get("username")
email = request.form.get("email")
place_name = request.form.get("place_name")
address = request.form.get("address")
tag = request.form.get("category")
desc = request.form.get("description")
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filename = place_name + ".jpg"
if email == params["email_id"]:
file.save(os.path.join(params['image_folder'], filename))
with sqlite3.connect("Travel.db") as con:
image = "img/" + filename
cur = con.cursor()
cur.execute(f'INSERT INTO Jaipur ("name", "image", "tag", "desc") VALUES(?,?,?,?);',(place_name, image, tag, desc))
con.commit()
print("--"*20, "Data is saved by Admin!!")
else:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print("--"*20, "Data is saved!!")
else:
return render_template("add.html")
return redirect("/")
else:
return render_template("add.html")
@app.route("/contact", methods=['GET','POST'])
def contact():
if request.method == "POST":
user_name = request.form.get("username")
email = request.form.get("email")
phone_no = request.form.get("phone_number")
suggestion = request.form.get("suggestion")
print("-"*10, user_name, email, phone_no, suggestion)
with sqlite3.connect("Travel.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO Suggestion (username, email, contact, suggestion) VALUES (?,?,?,?);",(user_name,email,phone_no,suggestion))
con.commit()
subject = 'Thank you for reponse'
user_msg = f"Dear {user_name},\n\nWe are glad to have your response. Hope you loved our website and gained information.\n\n\n\nRegards,\nJaipur Guide Team"
admin_sbj = f"Suggestion from {user_name}"
admin_msg = f"{suggestion} \n\n{user_name}\n{phone_no}"
send_email(subject, params['email_id'], email, user_msg)
send_email(admin_sbj, email, params['email_id'], admin_msg)
return render_template("contact.html")
else:
return render_template("contact.html")
if __name__ == '__main__':
app.run(debug=True)