-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
317 lines (256 loc) · 9.33 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
from flask import (
Flask,
render_template,
request,
redirect,
flash,
jsonify,
url_for,
Response,
)
from flask_mail import Mail, Message
from pymongo.mongo_client import MongoClient
import os
from bson import ObjectId
from datetime import datetime
import cloudinary
from cloudinary.uploader import upload
from werkzeug.security import generate_password_hash, check_password_hash
from pprint import pprint
import markdown, re
application = Flask(__name__)
application.secret_key = str(os.urandom(24))
ADMIN_USERNAME = "ayushmaanFCB"
ADMIN_PASSWORD_HASH = generate_password_hash(os.environ.get("admin_portal_password"))
try:
application.config["MAIL_SERVER"] = "smtp.gmail.com"
application.config["MAIL_PORT"] = 587
application.config["MAIL_USE_TLS"] = True
application.config["MAIL_USERNAME"] = "dasayush5maan@gmail.com"
application.config["MAIL_PASSWORD"] = os.environ.get("mail_app_key")
application.config["MAIL_DEFAULT_SENDER"] = "dasayush5maan@gmail.com"
application.secret_key = str(os.urandom(24))
mail = Mail(application)
except Exception as e:
print("Failed to connect Mail Service : ", e)
try:
cluster = MongoClient(os.environ.get("mongodb_uri"))
db = cluster["Personal-Portfolio"]
collection = db["Koyeb-Flask-Application"]
blogs_collection = db["Blogs"]
subscribers_collection = db["Subscribers"]
except Exception as e:
print("Failed to connect to Mongo DB Database : ", e)
try:
cloudinary_api_key = os.environ.get("cloudinary_api_key")
cloudinary_api_secret = os.environ.get("cloudinary_api_secret")
cloudinary.config(
api_key=cloudinary_api_key,
api_secret=cloudinary_api_secret,
cloud_name="dpviprc3b",
)
except Exception as e:
print("Failed to configure Cloudinary services : ", e)
@application.route("/")
def home_page_alt():
return render_template("home.html")
@application.route("/home")
def home_page():
return render_template("home.html")
@application.route("/projects")
def project_page():
return render_template("projects2.html")
@application.route("/resume")
def resume_page():
return render_template("resume.html")
@application.route("/skills")
def skills_page():
return render_template("skills.html")
@application.route("/contact")
def contact_form():
return render_template("contact.html")
@application.route("/submit", methods=["POST"])
def submit_form():
try:
data = {
"name": request.form["name"],
"email": request.form["email"],
"phone": request.form["phone"],
"organization": request.form["organization"],
"position": request.form["position"],
"website": request.form["website"],
"linkedin": request.form["linkedin"],
"message": request.form["message"],
}
msg = Message(
subject="Knock Knock, someone wants to contact",
recipients=[application.config["MAIL_DEFAULT_SENDER"]],
body=f"""
You have a new visitor on your portfolio website:
Full Name: {data["name"]}
Email Address: {data["email"]}
Phone Number: {data["phone"]}
Organization: {data["organization"]}
Position: {data["position"]}
Website: {data["website"]}
LinkedIn: {data["linkedin"]}
Message: {data["message"]}
""",
)
try:
mail.send(msg)
except Exception as e:
print("Error sending mail : ", e)
try:
collection.insert_one(data)
flash("Your form has been submitted successfully!", "success")
except Exception as e:
print("Error inserting data : ", e)
return redirect("/")
except Exception as e:
print(e)
return redirect("/")
@application.template_filter("nl2br")
def nl2br(value):
# Replace newlines with <br> tags
return re.sub(r"\n", "<br>", value)
@application.route("/blog", methods=["GET"])
def blog():
# Get the filter parameters from the request
sort_by = request.args.get("sort", "date_desc") # Default to sorting by newest
blog_type = request.args.get("type", "") # Default to no filter
date_range = request.args.get("date_range", "")
# Build MongoDB filter query
filter_query = {}
if blog_type:
filter_query["type"] = blog_type
if date_range:
filter_query["created_at"] = {"$gte": datetime.strptime(date_range, "%Y-%m-%d")}
# Sort the blogs based on the sort_by parameter
if sort_by == "date_desc":
sort_order = [("created_at", -1)] # Descending order (newest first)
elif sort_by == "date_asc":
sort_order = [("created_at", 1)] # Ascending order (oldest first)
elif sort_by == "popularity":
sort_order = [("upvotes", -1)] # Sort by popularity (upvotes)
# Query MongoDB for blogs with filters applied
blogs = list(blogs_collection.find(filter_query).sort(sort_order))
# Format the date
for blog in blogs:
if "created_at" in blog:
blog["created_at"] = blog["created_at"].strftime("%Y-%m-%d %H:%M:%S")
# if "content" in blog:
# blog["content"] = markdown.markdown(blog['content'])
# Get the unique date ranges for the dropdown
date_ranges = sorted(set(blog["created_at"][:10] for blog in blogs))
# Render the template with the selected filter values
return render_template(
"blogs.html",
blogs=blogs,
date_ranges=date_ranges,
sort_by=sort_by,
blog_type=blog_type,
date_range=date_range,
)
@application.route("/upvote/<blog_id>", methods=["POST"])
def toggle_upvote(blog_id):
blog = blogs_collection.find_one({"_id": ObjectId(blog_id)})
upvote = blog["upvotes"]
if blog:
# Adjust the upvote count
new_count = blog["upvotes"] + 1
new_count = max(0, new_count) # Prevent negative count
# Update the database
blogs_collection.update_one(
{"_id": ObjectId(blog_id)}, {"$set": {"upvotes": new_count}}
)
# Respond with the new count and state
return jsonify({"newCount": new_count, "upvoted": upvote})
else:
return jsonify({"error": "Blog not found"}), 404
@application.route("/subscribed", methods=["POST"])
def subscribed():
name = request.form.get("userName")
email = request.form.get("userEmail")
subscribers_collection.insert_one({"name": name, "email": email})
print(f"New subscription added to Database: {email}")
return redirect(url_for("blog"))
def send_notifications(subject, template, context):
subscribers = subscribers_collection.find()
for subscriber in subscribers:
name = subscriber["name"]
email = subscriber["email"]
html_content = render_template(template, name=name, **context)
msg = Message(
subject=subject,
recipients=[email],
html=html_content,
)
mail.send(msg)
@application.route("/admin")
def admin():
auth = request.authorization
if (
not auth
or auth.username != ADMIN_USERNAME
or not check_password_hash(ADMIN_PASSWORD_HASH, auth.password)
):
return Response(
"Access Denied: Please provide valid credentials.",
401,
{"WWW-Authenticate": 'Basic realm="Admin Login"'},
)
print("Logged in as Administrator : ", ADMIN_USERNAME, " at", str(datetime.now()))
return render_template("admin.html")
@application.route("/upload/project", methods=["POST"])
def upload_project():
title = request.form["title"]
content = request.form["content"]
tags = request.form["tags"].split(",")
images = request.files.getlist("images")
uploaded_urls = []
for image in images:
response = upload(image, folder="Portfolio")
uploaded_urls.append(response["url"])
project_data = {
"title": title,
"content": content,
"tags": tags,
"images": uploaded_urls,
"created_at": datetime.utcnow(),
"type": "project",
"upvotes": 0,
}
blogs_collection.insert_one(project_data)
try:
send_notifications(
subject=f"New Project: {title}",
template="project_notification.html",
context={"title": title, "content": content, "tags": tags},
)
print("Notification sent successfully!!!")
except Exception as e:
print("Error sending notification : ", e)
return jsonify({"message": "Project uploaded successfully!"}), 201
@application.route("/upload/update", methods=["POST"])
def upload_update():
content = request.form["content"]
update_data = {
"content": content,
"created_at": datetime.utcnow(),
"type": "updates",
"upvotes": 0,
}
blogs_collection.insert_one(update_data)
try:
send_notifications(
subject="New Update Posted!",
template="updates_notification.html",
context={"content": content},
)
print("Notification sent successfully!!!")
except Exception as e:
print("Error sending notification : ", e)
return jsonify({"message": "Update uploaded successfully!"}), 201
if __name__ == "__main__":
application.run(host="0.0.0.0", port=8000, debug=True)