Skip to content

Commit 22ae438

Browse files
committed
Refactor post rendering logic to include recommended articles and update author details
1 parent 59dc0c0 commit 22ae438

5 files changed

Lines changed: 41 additions & 27 deletions

File tree

__pycache__/app.cpython-311.pyc

434 Bytes
Binary file not shown.

app.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from flask_migrate import Migrate
55
from models import Post, Comment, Event
66
from datetime import datetime
7+
from sqlalchemy import func
78
import os
89

910

@@ -30,6 +31,10 @@ def create_app():
3031
def inject_year():
3132
return {'year': datetime.utcnow().year}
3233

34+
@app.route('/')
35+
def home():
36+
return render_template('index.html')
37+
3338
@app.route('/about')
3439
def about():
3540
return render_template('about.html')
@@ -54,12 +59,6 @@ def get_events():
5459
except Exception as e:
5560
return jsonify({"error": str(e)}), 500
5661

57-
@app.route('/')
58-
def home():
59-
teaser_events = Event.query.filter_by(is_featured=True).order_by(
60-
Event.event_date.asc()).limit(2).all()
61-
return render_template('index.html', teaser_events=teaser_events)
62-
6362
@app.route('/events')
6463
def events():
6564
featured_events = Event.query.filter_by(
@@ -78,23 +77,34 @@ def post(post_id):
7877
post = Post.query.get_or_404(post_id)
7978
comments = Comment.query.filter_by(post_id=post.id).all()
8079

81-
# Get previous and next posts for navigation
8280
prev_post = Post.query.filter(
8381
Post.id < post_id).order_by(Post.id.desc()).first()
8482
next_post = Post.query.filter(
8583
Post.id > post_id).order_by(Post.id.asc()).first()
8684

87-
# Get related posts (same category, excluding current post)
8885
related_posts = []
8986
if post.category:
9087
related_posts = Post.query.filter(
9188
Post.category == post.category,
9289
Post.id != post.id
9390
).order_by(Post.date_posted.desc()).limit(3).all()
9491

92+
recommended_posts = []
93+
if post.category:
94+
recommended_posts = Post.query.filter(
95+
Post.category != post.category,
96+
Post.id != post.id
97+
).order_by(func.random()).limit(3).all()
98+
99+
if not recommended_posts:
100+
recommended_posts = Post.query.filter(
101+
Post.id != post.id
102+
).order_by(func.random()).limit(3).all()
103+
95104
return render_template("post.html", post=post, comments=comments,
96105
prev_post=prev_post, next_post=next_post,
97-
related_posts=related_posts)
106+
related_posts=related_posts,
107+
recommended_posts=recommended_posts)
98108

99109
@app.route('/add_comment/<int:post_id>', methods=['POST'])
100110
def add_comment(post_id):
@@ -111,7 +121,6 @@ def add_comment(post_id):
111121
db.session.add(comment)
112122
db.session.commit()
113123

114-
# Redirect to the post page
115124
return redirect(url_for('post', post_id=post_id))
116125

117126
return app

populate_db.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def populate_database():
5757
""",
5858
category="Artificial Intelligence",
5959
date_posted=datetime.utcnow(),
60-
image_url="./static/img/ai-ml.jpg"
60+
image_url="img/ai-ml.jpg"
6161
)
6262

6363
post2 = Post(
@@ -100,7 +100,7 @@ def populate_database():
100100
""",
101101
category="Web Development",
102102
date_posted=datetime.utcnow(),
103-
image_url="./static/img/web-dev.jpg"
103+
image_url="img/web-dev.jpg"
104104
)
105105

106106
post3 = Post(
@@ -130,7 +130,7 @@ def populate_database():
130130
""",
131131
category="Tech and Gadgets",
132132
date_posted=datetime.utcnow(),
133-
image_url="./static/img/tech-gadget.jpg"
133+
image_url="img/tech-gadget.jpg"
134134
)
135135

136136
post4 = Post(
@@ -161,7 +161,7 @@ def populate_database():
161161
""",
162162
category="Technology",
163163
date_posted=datetime.utcnow(),
164-
image_url="./static/img/5g.jpg"
164+
image_url="img/5g.jpg"
165165
)
166166

167167
post5 = Post(
@@ -202,7 +202,7 @@ def populate_database():
202202
""",
203203
category="Cybersecurity",
204204
date_posted=datetime.utcnow(),
205-
image_url="./static/img/cybersecurity.jpg"
205+
image_url="img/cybersecurity.jpg"
206206
)
207207

208208
post6 = Post(
@@ -242,7 +242,7 @@ def populate_database():
242242
""",
243243
category="Technology",
244244
date_posted=datetime.utcnow(),
245-
image_url="./static/img/quantum-computing.jpg"
245+
image_url="img/quantum-computing.jpg"
246246
)
247247

248248
db.session.add_all([post1, post2, post3, post4, post5, post6])
@@ -275,7 +275,7 @@ def populate_database():
275275
description='A deep dive into the latest cybersecurity trends and best practices.',
276276
location='London, UK',
277277
event_date=datetime(2025, 6, 20),
278-
image_url='events/cybersecurity_forum.jpg',
278+
image_url='img/cybersecurity_forum.jpg',
279279
link='https://cybersecurityforum.com',
280280
is_featured=False,
281281
category='Workshop'
@@ -297,7 +297,7 @@ def populate_database():
297297
description='Discover how cloud technology is transforming industries worldwide.',
298298
location='Singapore',
299299
event_date=datetime(2025, 9, 12),
300-
image_url='events/cloud_expo.jpg',
300+
image_url='img/cloud_expo.jpg',
301301
link='https://cloudexpo.com',
302302
is_featured=False,
303303
category='Webinar'
@@ -319,7 +319,7 @@ def populate_database():
319319
description='Showcasing cutting-edge robotics and automation advancements.',
320320
location='Tokyo, Japan',
321321
event_date=datetime(2025, 11, 8),
322-
image_url='events/robotics_expo.jpg',
322+
image_url='img/robotics_expo.jpg',
323323
link='https://roboticsexpo.com',
324324
is_featured=False,
325325
category='Conference'

static/css/index.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,7 @@ footer {
20992099
overflow: hidden;
21002100
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
21012101
transition: transform 0.3s ease;
2102+
height: 450px;
21022103
}
21032104

21042105
.featured-post-card:hover {
@@ -2320,7 +2321,7 @@ footer {
23202321
margin-bottom: var(--space-md);
23212322
flex-grow: 1;
23222323
line-height: 1.5;
2323-
2324+
23242325
}
23252326

23262327
.blog-card-meta {
@@ -3864,6 +3865,10 @@ footer {
38643865
flex-direction: column;
38653866
}
38663867

3868+
.featured-post-card {
3869+
height: 100%;
3870+
}
3871+
38673872
.form-visual {
38683873
display: none;
38693874
}

templates/post.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ <h1>{{ post.title }}</h1>
3131
<img src="{{ url_for('static', filename='img/author-avatar.jpg') }}" alt="Author">
3232
</div>
3333
<div class="author-info">
34-
<h3>John Doe</h3>
34+
<h3>Mayen Akech</h3>
3535
<p>Tech Writer & Developer</p>
3636
</div>
3737
<div class="post-share">
@@ -93,10 +93,10 @@ <h4>No Next Post</h4>
9393
<div class="sidebar-widget author-card">
9494
<div class="author-card-header">
9595
<img src="{{ url_for('static', filename='img/author-avatar.jpg') }}" alt="Author">
96-
<h3>John Doe</h3>
96+
<h3>Mayen Akech</h3>
9797
<p>Tech Writer & Developer</p>
9898
</div>
99-
<p class="author-bio">John is a seasoned tech enthusiast with over 10 years of experience in software development
99+
<p class="author-bio">Mayen is a seasoned tech enthusiast with over 10 years of experience in software development
100100
and emerging technologies.</p>
101101
<div class="author-social">
102102
<a href="#" aria-label="Twitter"><i class="fab fa-twitter"></i></a>
@@ -199,12 +199,12 @@ <h4>{{ comment.username }}</h4>
199199
<section class="related-articles-section">
200200
<div class="section-header">
201201
<h2>You Might Also <span>Enjoy</span></h2>
202-
<p class="section-subtitle">More articles from this category</p>
202+
<p class="section-subtitle">Discover more tech insights</p>
203203
</div>
204204

205205
<div class="related-articles-container">
206-
{% if related_posts %}
207-
{% for related in related_posts %}
206+
{% if recommended_posts %}
207+
{% for related in recommended_posts %}
208208
<article class="related-article-card">
209209
<div class="article-image">
210210
<img
@@ -227,7 +227,7 @@ <h3>{{ related.title }}</h3>
227227
{% endfor %}
228228
{% else %}
229229
<div class="no-related">
230-
<p>No related articles available at this time.</p>
230+
<p>No recommended articles available at this time.</p>
231231
</div>
232232
{% endif %}
233233
</div>

0 commit comments

Comments
 (0)