@@ -21,6 +21,14 @@ def current_user_id():
21
21
def find_running (self ):
22
22
pass
23
23
24
+ @abc .abstractmethod
25
+ def stop (self , id : str ):
26
+ pass
27
+
28
+ @abc .abstractmethod
29
+ def restart (self , id : str ):
30
+ pass
31
+
24
32
25
33
container_definition = {
26
34
'id' : 'time_entry' ,
@@ -169,6 +177,20 @@ def check_whether_current_user_owns_item(cls, data: dict):
169
177
raise CustomError (HTTPStatus .FORBIDDEN ,
170
178
"The current user is not the owner of this time entry" )
171
179
180
+ @classmethod
181
+ def checks_owner_and_is_not_stopped (cls , data : dict ):
182
+ cls .check_whether_current_user_owns_item (data )
183
+
184
+ if data .get ('end_date' ) is not None :
185
+ raise CustomError (HTTPStatus .UNPROCESSABLE_ENTITY , "The specified time entry is already stopped" )
186
+
187
+ @classmethod
188
+ def checks_owner_and_is_not_started (cls , data : dict ):
189
+ cls .check_whether_current_user_owns_item (data )
190
+
191
+ if data .get ('end_date' ) is None :
192
+ raise CustomError (HTTPStatus .UNPROCESSABLE_ENTITY , "The specified time entry is already running" )
193
+
172
194
def get_all (self , conditions : dict = {}) -> list :
173
195
event_ctx = self .create_event_context ("read-many" )
174
196
conditions .update ({"owner_id" : event_ctx .user_id })
@@ -182,11 +204,23 @@ def create(self, data: dict):
182
204
event_ctx = self .create_event_context ("create" )
183
205
return self .repository .create (data , event_ctx )
184
206
185
- def update (self , id , data : dict ):
186
- event_ctx = self .create_event_context ("update" )
207
+ def update (self , id , data : dict , description = None ):
208
+ event_ctx = self .create_event_context ("update" , description )
187
209
return self .repository .partial_update (id , data , event_ctx ,
188
210
peeker = self .check_whether_current_user_owns_item )
189
211
212
+ def stop (self , id ):
213
+ event_ctx = self .create_event_context ("update" , "Stop time entry" )
214
+ return self .repository .partial_update (id , {
215
+ 'end_date' : current_datetime_str ()
216
+ }, event_ctx , peeker = self .checks_owner_and_is_not_stopped )
217
+
218
+ def restart (self , id ):
219
+ event_ctx = self .create_event_context ("update" , "Restart time entry" )
220
+ return self .repository .partial_update (id , {
221
+ 'end_date' : None
222
+ }, event_ctx , peeker = self .checks_owner_and_is_not_started )
223
+
190
224
def delete (self , id ):
191
225
event_ctx = self .create_event_context ("delete" )
192
226
self .repository .delete (id , event_ctx , peeker = self .check_whether_current_user_owns_item )
0 commit comments