1+ from datetime import datetime
12from typing import Optional , List
23import 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