1
+ from datetime import datetime
1
2
from typing import Optional , List
2
3
import reflex as rx
3
4
@@ -107,6 +108,23 @@ class BlogEditFormState(BlogPostState):
107
108
form_data : dict = {}
108
109
# post_content: str = ""
109
110
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
+
110
128
def handle_submit (self , form_data ):
111
129
self .form_data = form_data
112
130
post_id = form_data .pop ('post_id' )
@@ -116,11 +134,16 @@ def handle_submit(self, form_data):
116
134
publish_time = None
117
135
if 'publish_time' in form_data :
118
136
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
120
142
publish_active = False
121
143
if 'publish_active' in form_data :
122
144
publish_active = form_data .pop ('publish_active' ) == "on"
123
145
updated_data = {** form_data }
124
146
updated_data ['publish_active' ] = publish_active
147
+ updated_data ['publish_date' ] = final_publish_date
125
148
self .save_post_edits (post_id , updated_data )
126
149
return self .to_blog_post ()
0 commit comments