Skip to content

Commit d5168f3

Browse files
committed
images now available
1 parent fe970b2 commit d5168f3

17 files changed

+140
-87
lines changed

__pycache__/app.cpython-312.pyc

535 Bytes
Binary file not shown.

__pycache__/config.cpython-312.pyc

4 Bytes
Binary file not shown.

__pycache__/helpers.cpython-312.pyc

1.2 KB
Binary file not shown.

__pycache__/models.cpython-312.pyc

0 Bytes
Binary file not shown.

app.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
from models import Blog, User
55
from werkzeug.exceptions import NotFound, Unauthorized, BadRequest, Conflict, InternalServerError
66
import cloudinary.uploader
7+
from helpers import inject_user_data
78

89

910
@app.route('/')
10-
def home_page(): # put application's code here
11-
return render_template("home_page/index.html")
11+
@inject_user_data
12+
def home_page():
13+
blogs = Blog.query.all()
14+
15+
return render_template("home_page/index.html", blogs=list(blogs)[:3])
1216

1317

1418
@app.route("/about")
19+
@inject_user_data
1520
def about_page():
1621
return render_template("about.html")
1722

1823

1924
@app.route("/blogs/<int:blog_id>")
25+
@inject_user_data
2026
def single_blog_page(blog_id):
2127
try:
2228
found_blog = Blog.query.get(blog_id)
@@ -30,6 +36,7 @@ def single_blog_page(blog_id):
3036

3137

3238
@app.route("/blogs")
39+
@inject_user_data
3340
def blogs_page():
3441
try:
3542
blogs = Blog.query.all()
@@ -40,6 +47,7 @@ def blogs_page():
4047

4148

4249
@app.route("/auth/login", methods=["GET", "POST"])
50+
@inject_user_data
4351
def login():
4452
if request.method == "POST":
4553
email = request.form["email"]
@@ -57,13 +65,15 @@ def login():
5765

5866

5967
@app.route("/auth/logout")
68+
@inject_user_data
6069
def logout():
6170
del session["user_id"]
6271

6372
return redirect(url_for("home_page"))
6473

6574

6675
@app.route("/auth/sign-up", methods=["GET", "POST"])
76+
@inject_user_data
6777
def sign_up():
6878
if request.method == "POST":
6979
username = request.form["username"]
@@ -104,6 +114,7 @@ def sign_up():
104114

105115

106116
@app.route("/create-post", methods=["POST", "GET"])
117+
@inject_user_data
107118
def create_post():
108119
if request.method == "POST":
109120
title = request.form["title"]
@@ -135,6 +146,7 @@ def create_post():
135146

136147

137148
@app.route("/not-found")
149+
@inject_user_data
138150
def not_found_page():
139151
return render_template("not_found_page/index.html")
140152

config.py

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from flask_sqlalchemy import SQLAlchemy
77
from sqlalchemy import MetaData
88
import cloudinary
9+
# from helpers import inject_user
910

1011
load_dotenv()
1112

@@ -24,5 +25,7 @@
2425

2526
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
2627

28+
# app.context_processor(inject_user)
29+
2730
cloudinary.config(cloud_name = os.getenv('CLOUD_NAME'), api_key=os.getenv('API_KEY'),
2831
api_secret=os.getenv('API_SECRET'))

helpers.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from models import User
2+
from flask import session
3+
from flask import current_app, g
4+
from functools import wraps
5+
6+
7+
def get_user():
8+
if session.get("user_id"):
9+
return User.query.get(session.get("user_id"))
10+
return None
11+
12+
13+
def inject_user():
14+
user = None
15+
if 'user_id' in session:
16+
user = get_user()
17+
g.user = user
18+
return {}
19+
20+
21+
def inject_user_data(func):
22+
@wraps(func)
23+
def wrapper(*args, **kwargs):
24+
inject_user() # Call the context processor function
25+
return func(*args, **kwargs)
26+
return wrapper

instance/app.db

0 Bytes
Binary file not shown.

models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class User(db.Model):
1616
email = Column(String(255), nullable=False, unique=True)
1717
password_hash = Column(String(255), nullable=False)
1818
blogs = db.relationship("Blog", backref="author")
19-
created_at = Column(DateTime, default=datetime.utcnow)
19+
created_at = Column(DateTime, default=datetime.now)
2020
role = Column(Enum(EnumRole.ADMIN, EnumRole.USER), default=EnumRole.USER)
2121
profile_img = Column(String(255), default="default.jpg")
2222

static/assets/favicon.png

10.4 KB
Loading

static/css/blog-page.css

+4
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
color: black;
99
text-align: center;
1010
}
11+
12+
.no-blog {
13+
height: 100%;
14+
}

static/css/style.css

+4
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ footer {
9797
.section {
9898
color: black;
9999
}
100+
101+
.custom-navbar {
102+
background-color: transparent;
103+
}

static/js/main.js

Whitespace-only changes.

templates/blogs_page/index.html

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@ <h1 class="title is-1 block blogs-heading">All blog posts</h1>
1313
<div class="card">
1414
<div class="card-image">
1515
<figure class="image is-16by9">
16-
<img src="[Image URL for Post 3]" alt="Placeholder image" />
16+
<img src="{{ blog.blog_img }}" alt="{{ blog.title }}" />
1717
</figure>
1818
</div>
1919
<div class="card-content">
2020
<div class="content">
21-
<a href="[Link to Post 3]" class="title is-4"> {{ blog.title }} </a>
22-
<p>[Short excerpt from Post 3]</p>
21+
<a href="/blogs/{{ blog.id }}" class="title is-4">
22+
{{ blog.title }}
23+
</a>
24+
<p>{{ blog.subtitle }}</p>
2325
<a href="/blogs/{{ blog.id }}" class="button is-small">Read More</a>
2426
</div>
2527
</div>
2628
</div>
2729
</div>
2830
{% endfor %} {% else %}
29-
<h1 class="title">
30-
No blogs were found!
31-
<a class="link" href="/create-post">You can create you own</a>
32-
</h1>
31+
<div class="no-blog">
32+
<h1 class="title">
33+
No blogs were found!
34+
<a class="link" href="/create-post">You can create you own</a>
35+
</h1>
36+
</div>
3337
{% endif %}
3438
</section>
3539
{% endblock body %}

templates/blogs_page/single_blog.html

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22
%} {% block body %}
33
<header class="section">
44
<div class="container">
5-
<h1 class="title">{{ blog.title }}</h1>
6-
<h2 class="subtitle">{{ blog.subtitle }}</h2>
7-
<p class="subtitle">
8-
By {{ blog.author_name }} | Posted on {{ blog.created_at }}
9-
</p>
5+
<div class="columns">
6+
<div class="column is-half">
7+
<figure class="image is-16by9">
8+
<img src="{{ blog.blog_img }}" alt="{{ blog.title }}" />
9+
</figure>
10+
</div>
11+
<div class="column is-half">
12+
<h1 class="title">{{ blog.title }}</h1>
13+
<h2 class="subtitle">{{ blog.subtitle }}</h2>
14+
<p class="subtitle">
15+
By {{ blog.author_name }} | Posted on {{ blog.created_at.strftime('%B
16+
%d %Y, %H:%M') }}
17+
</p>
18+
</div>
19+
</div>
1020
</div>
1121
</header>
1222

templates/home_page/index.html

+9-41
Original file line numberDiff line numberDiff line change
@@ -46,60 +46,28 @@ <h2 class="title">About Us</h2>
4646
<div class="container">
4747
<h2 class="title">Latest Posts</h2>
4848
<div class="columns">
49+
{% if blogs %} {% for blog in blogs %}
4950
<div class="column is-one-third">
5051
<div class="card">
5152
<div class="card-image">
5253
<figure class="image is-16by9">
53-
<img src="[Image URL for Post 1]" alt="Placeholder image" />
54+
<img src="{{ blog.blog_img }}" alt="{{ blog.title }}" />
5455
</figure>
5556
</div>
5657
<div class="card-content">
5758
<div class="content">
58-
<a href="[Link to Post 1]" class="title is-4">
59-
[Title of Post 1]
59+
<a href="/blogs/{{ blog.id }}" class="title is-4">
60+
{{ blog.title }}
6061
</a>
61-
<p>[Short excerpt from Post 1]</p>
62-
<a href="[Link to Post 1]" class="button is-small">Read More</a>
63-
</div>
64-
</div>
65-
</div>
66-
</div>
67-
<div class="column is-one-third">
68-
<div class="card">
69-
<div class="card-image">
70-
<figure class="image is-16by9">
71-
<img src="[Image URL for Post 2]" alt="Placeholder image" />
72-
</figure>
73-
</div>
74-
<div class="card-content">
75-
<div class="content">
76-
<a href="[Link to Post 2]" class="title is-4">
77-
[Title of Post 2]
78-
</a>
79-
<p>[Short excerpt from Post 2]</p>
80-
<a href="[Link to Post 2]" class="button is-small">Read More</a>
81-
</div>
82-
</div>
83-
</div>
84-
</div>
85-
<div class="column is-one-third">
86-
<div class="card">
87-
<div class="card-image">
88-
<figure class="image is-16by9">
89-
<img src="[Image URL for Post 3]" alt="Placeholder image" />
90-
</figure>
91-
</div>
92-
<div class="card-content">
93-
<div class="content">
94-
<a href="[Link to Post 3]" class="title is-4">
95-
[Title of Post 3]
96-
</a>
97-
<p>[Short excerpt from Post 3]</p>
98-
<a href="[Link to Post 3]" class="button is-small">Read More</a>
62+
<p>{{ blog.subtitle }}</p>
63+
<a href="{{ blog.id }}" class="button is-small">Read More</a>
9964
</div>
10065
</div>
10166
</div>
10267
</div>
68+
{% endfor %} {% else %}
69+
<p>No posts found.</p>
70+
{% endif %}
10371
</div>
10472
<div class="latest-blogs">
10573
<a href="/blogs" class="button is-centered">View All Posts</a>

templates/layout/layout.html

+53-31
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,89 @@
77
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
88
/>
99
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
10+
<link rel="shortcut icon" href="{{ url_for("static", filename="assets/favicon.png") }}" type="image/x-icon">
1011
<link
1112
rel="stylesheet"
1213
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
1314
/>
1415
<link
1516
rel="stylesheet"
16-
href="{{ url_for('static', filename='css/style.css') }}"
17+
href="https://cdn.jsdelivr.net/npm/bulma@latest/css/bulma.min.css"
1718
/>
1819
<link
1920
rel="stylesheet"
20-
href="https://cdn.jsdelivr.net/npm/bulma@latest/css/bulma.min.css"
21+
href="{{ url_for('static', filename='css/style.css') }}"
2122
/>
2223
{% block css %} {% endblock %}
2324
<title>{% block title %}{% endblock %}</title>
2425
</head>
2526
<body>
26-
<header class="header">
27-
<nav class="header-navigation">
28-
<ul>
29-
<li>
30-
<a class="button is-light is-link header-navigation__link" href="/"
27+
<header class="header has-background-primary">
28+
<nav class="navbar custom-navbar is-light">
29+
<div class="container">
30+
<div class="navbar-start">
31+
<a class="navbar-item is-text has-text-link-dark button" href="/"
3132
>Home</a
3233
>
33-
</li>
34-
<li>
3534
<a
36-
class="button is-light is-link header-navigation__link"
35+
class="navbar-item is-text has-text-link-dark button"
3736
href="/about"
3837
>About</a
3938
>
40-
</li>
41-
<li>
4239
<a
43-
class="button is-light is-link header-navigation__link"
40+
class="navbar-item is-text has-text-link-dark button"
4441
href="/blogs"
4542
>Blogs</a
4643
>
47-
</li>
48-
{% if "user_id" in session %}
49-
<li>
44+
{% if "user_id" in session and g.user %}
5045
<a
51-
class="button is-light is-link header-navigation__link"
46+
class="navbar-item is-text has-text-link-dark button"
5247
href="/create-post"
53-
>Create new Post</a
48+
>Create Blog</a
5449
>
55-
</li>
56-
{% endif %}
57-
</ul>
50+
{% endif %}
51+
</div>
52+
<div class="navbar-end">
53+
{% if "user_id" in session and g.user %}
54+
<div class="navbar-item">
55+
<figure class="image is-24x24" style="margin-right: 10px">
56+
<img
57+
src="{{ g.user.profile_img }}"
58+
alt="Profile Picture"
59+
class="is-rounded"
60+
/>
61+
</figure>
62+
{{ g.user.username }}
63+
</div>
64+
<a
65+
class="navbar-item button is-text has-text-danger"
66+
href="/auth/logout"
67+
>Logout</a
68+
>
69+
{% else %}
70+
<div class="navbar-item">
71+
<a class="button has-text-link-dark is-text" href="/auth/login"
72+
>Login</a
73+
>
74+
</div>
75+
<div class="navbar-item">
76+
<a class="button is-light" href="/auth/sign-up">Sign up</a>
77+
</div>
78+
{% endif %}
79+
</div>
80+
</div>
5881
</nav>
59-
{% if "user_id" not in session %}
60-
<div class="buttons">
61-
<a class="button is-light is-medium" href="/auth/login">Login</a>
62-
<a class="button is-light is-medium" href="/auth/sign-up">Sign up</a>
63-
</div>
64-
{% else %}
65-
<a class="button is-light is-medium" href="/auth/logout">Log out</a>
66-
{% endif %}
6782
</header>
6883
{% block body %}{% endblock %}
69-
<footer>
70-
<p>&copy; Copyright 2024 by themuku</p>
84+
<footer class="footer has-background-dark">
85+
<div class="container has-text-centered">
86+
<p>&copy; Copyright 2024 by themuku</p>
87+
</div>
7188
</footer>
89+
90+
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
91+
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
92+
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
93+
{% block js %} {% endblock %}
7294
</body>
7395
</html>

0 commit comments

Comments
 (0)