Skip to content

Commit 5a6eb56

Browse files
53 - Manging My Blog Posts
1 parent a5d94c2 commit 5a6eb56

File tree

4 files changed

+55
-41
lines changed

4 files changed

+55
-41
lines changed

full_stack_python/blog/detail.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ..ui.base import base_page
44

55
from . import state
6-
6+
from .notfound import blog_post_not_found
77
# @rx.page(route='/about')
88
def blog_post_detail_page() -> rx.Component:
99
can_edit = True
@@ -13,7 +13,7 @@ def blog_post_detail_page() -> rx.Component:
1313
edit_link,
1414
rx.fragment("")
1515
)
16-
my_child = rx.vstack(
16+
my_child = rx.cond(state.BlogPostState.post, rx.vstack(
1717
rx.hstack(
1818
rx.heading(state.BlogPostState.post.title, size="9"),
1919
edit_link_el,
@@ -30,5 +30,7 @@ def blog_post_detail_page() -> rx.Component:
3030
spacing="5",
3131
align="center",
3232
min_height="85vh"
33+
),
34+
blog_post_not_found()
3335
)
3436
return base_page(my_child)

full_stack_python/blog/edit.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,36 @@
66
from . import forms
77

88
from .state import BlogEditFormState
9-
10-
11-
9+
from .notfound import blog_post_not_found
1210

1311
def blog_post_edit_page() -> rx.Component:
1412
my_form = forms.blog_post_edit_form()
1513
post = BlogEditFormState.post
16-
my_child = rx.vstack(
17-
rx.heading("Editing ", post.title, size="9"),
18-
rx.desktop_only(
19-
rx.box(
20-
my_form,
21-
width='50vw'
22-
)
23-
),
24-
rx.tablet_only(
25-
rx.box(
26-
my_form,
27-
width='75vw'
28-
)
29-
),
30-
rx.mobile_only(
31-
rx.box(
32-
my_form,
33-
width='95vw'
34-
)
35-
),
36-
spacing="5",
37-
align="center",
38-
min_height="95vh",
14+
my_child = rx.cond(post,
15+
rx.vstack(
16+
rx.heading("Editing ", post.title, size="9"),
17+
rx.desktop_only(
18+
rx.box(
19+
my_form,
20+
width='50vw'
21+
)
22+
),
23+
rx.tablet_only(
24+
rx.box(
25+
my_form,
26+
width='75vw'
27+
)
28+
),
29+
rx.mobile_only(
30+
rx.box(
31+
my_form,
32+
width='95vw'
33+
)
34+
),
35+
spacing="5",
36+
align="center",
37+
min_height="95vh",
38+
),
39+
blog_post_not_found()
3940
)
4041
return base_page(my_child)

full_stack_python/blog/notfound.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import reflex as rx
2+
3+
def blog_post_not_found() -> rx.Component:
4+
return rx.hstack(
5+
rx.heading("Blog Post Not Found"),spacing="5",
6+
align="center",
7+
min_height="85vh")

full_stack_python/blog/state.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,22 @@ def blog_post_edit_url(self):
3636
return f"{BLOG_POSTS_ROUTE}/{self.post.id}/edit"
3737

3838
def get_post_detail(self):
39+
if self.my_userinfo_id is None:
40+
self.post = None
41+
self.post_content = ""
42+
self.post_publish_active = False
43+
return
44+
lookups = (
45+
(BlogPostModel.userinfo_id == self.my_userinfo_id) &
46+
(BlogPostModel.id == self.blog_post_id)
47+
)
3948
with rx.session() as session:
4049
if self.blog_post_id == "":
4150
self.post = None
4251
return
4352
sql_statement = select(BlogPostModel).options(
4453
sqlalchemy.orm.joinedload(BlogPostModel.userinfo).joinedload(UserInfo.user)
45-
).where(
46-
(BlogPostModel.id == self.blog_post_id)
47-
)
54+
).where(lookups)
4855
result = session.exec(sql_statement).one_or_none()
4956
# if result.userinfo: # db lookup
5057
# print('working')
@@ -58,20 +65,17 @@ def get_post_detail(self):
5865
# return
5966

6067

61-
def load_posts(self, published_only=False):
62-
lookup_args = ()
63-
if published_only:
64-
lookup_args = (
65-
(BlogPostModel.publish_active == True) &
66-
(BlogPostModel.publish_date < datetime.now())
67-
)
68+
def load_posts(self, *args, **kwargs):
69+
# if published_only:
70+
# lookup_args = (
71+
# (BlogPostModel.publish_active == True) &
72+
# (BlogPostModel.publish_date < datetime.now())
73+
# )
6874
with rx.session() as session:
6975
result = session.exec(
7076
select(BlogPostModel).options(
7177
sqlalchemy.orm.joinedload(BlogPostModel.userinfo)
72-
).where(
73-
*lookup_args
74-
)
78+
).where(BlogPostModel.userinfo_id == self.my_userinfo_id)
7579
).all()
7680
self.posts = result
7781
# return

0 commit comments

Comments
 (0)