Skip to content

Commit 32ed7f4

Browse files
36 - Publish Date Field
1 parent 82bc3f2 commit 32ed7f4

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

full_stack_python/blog/detail.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def blog_post_detail_page() -> rx.Component:
1919
edit_link_el,
2020
align='end'
2121
),
22+
rx.text(state.BlogPostState.post.publish_date),
2223
rx.text(
2324
state.BlogPostState.post.content,
2425
white_space='pre-wrap'

full_stack_python/blog/forms.py

+2
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ def blog_post_edit_form() -> rx.Component:
8585
rx.box(
8686
rx.hstack(
8787
rx.input(
88+
default_value=BlogEditFormState.publish_display_date,
8889
type='date',
8990
name='publish_date',
9091
width='100%'
9192
),
9293
rx.input(
94+
default_value=BlogEditFormState.publish_display_time,
9395
type='time',
9496
name='publish_time',
9597
width='100%'

full_stack_python/blog/model.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@ class BlogPostModel(rx.Model, table=True):
2929
nullable=False
3030
)
3131
publish_active: bool = False
32-
# publish_date
33-
# publish_time
32+
publish_date: datetime = Field(
33+
default=None,
34+
sa_type=sqlalchemy.DateTime(timezone=True),
35+
sa_column_kwargs={},
36+
nullable=True
37+
)

full_stack_python/blog/state.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import datetime
12
from typing import Optional, List
23
import reflex as rx
34

@@ -107,6 +108,23 @@ class BlogEditFormState(BlogPostState):
107108
form_data: dict = {}
108109
# post_content: str = ""
109110

111+
@rx.var
112+
def publish_display_date(self) -> str:
113+
# return "2023-12-01" # YYYY-MM-DD
114+
if not self.post:
115+
return datetime.now().strftime("%Y-%m-%d")
116+
if not self.post.publish_date:
117+
return datetime.now().strftime("%Y-%m-%d")
118+
return self.post.publish_date.strftime("%Y-%m-%d")
119+
120+
@rx.var
121+
def publish_display_time(self) -> str:
122+
if not self.post:
123+
return datetime.now().strftime("%H:%M:%S")
124+
if not self.post.publish_date:
125+
return datetime.now().strftime("%H:%M:%S")
126+
return self.post.publish_date.strftime("%H:%M:%S")
127+
110128
def handle_submit(self, form_data):
111129
self.form_data = form_data
112130
post_id = form_data.pop('post_id')
@@ -116,11 +134,16 @@ def handle_submit(self, form_data):
116134
publish_time = None
117135
if 'publish_time' in form_data:
118136
publish_time = form_data.pop('publish_time')
119-
print(publish_date, publish_time)
137+
publish_input_string = f"{publish_date} {publish_time}"
138+
try:
139+
final_publish_date = datetime.strptime(publish_input_string, '%Y-%m-%d %H:%M:%S')
140+
except:
141+
final_publish_date = None
120142
publish_active = False
121143
if 'publish_active' in form_data:
122144
publish_active = form_data.pop('publish_active') == "on"
123145
updated_data = {**form_data}
124146
updated_data['publish_active'] = publish_active
147+
updated_data['publish_date'] = final_publish_date
125148
self.save_post_edits(post_id, updated_data)
126149
return self.to_blog_post()

0 commit comments

Comments
 (0)