1313import uuid
1414from typing import Callable , Dict , List , Optional , Set , Tuple , Union
1515
16+ import pytz
1617import wrapt
1718from PIL import Image , ImageStat
1819from requests import codes
@@ -79,7 +80,7 @@ def parse_target_id(
7980 try :
8081 [matching_target ] = [
8182 target for target in instance .targets
82- if target .target_id == target_id
83+ if target .target_id == target_id and not target . delete_date
8384 ]
8485 except ValueError :
8586 body : Dict [str , str ] = {
@@ -224,18 +225,23 @@ def __init__( # pylint: disable=too-many-arguments
224225 Vuforia's documentation).
225226 application_metadata (str): The base64 encoded application metadata
226227 associated with the target.
228+ delete_date (Optional[datetime.datetime]): The time that the target
229+ was deleted.
227230 """
228231 self .name = name
229232 self .target_id = uuid .uuid4 ().hex
230233 self .active_flag = active_flag
231234 self .width = width
232- self .upload_date : datetime .datetime = datetime .datetime .now ()
235+ gmt = pytz .timezone ('GMT' )
236+ now = datetime .datetime .now (tz = gmt )
237+ self .upload_date : datetime .datetime = now
233238 self .last_modified_date = self .upload_date
234239 self .processed_tracking_rating = random .randint (0 , 5 )
235240 self .image = image
236241 self .reco_rating = ''
237242 self ._processing_time_seconds = processing_time_seconds
238243 self .application_metadata = application_metadata
244+ self .delete_date : Optional [datetime .datetime ] = None
239245
240246 @property
241247 def _post_processing_status (self ) -> TargetStatuses :
@@ -273,7 +279,9 @@ def status(self) -> str:
273279 seconds = self ._processing_time_seconds ,
274280 )
275281
276- time_since_change = datetime .datetime .now () - self .last_modified_date
282+ gmt = pytz .timezone ('GMT' )
283+ now = datetime .datetime .now (tz = gmt )
284+ time_since_change = now - self .last_modified_date
277285
278286 if time_since_change <= processing_time :
279287 return str (TargetStatuses .PROCESSING .value )
@@ -298,7 +306,9 @@ def tracking_rating(self) -> int:
298306 seconds = self ._processing_time_seconds / 2 ,
299307 )
300308
301- time_since_upload = datetime .datetime .now () - self .upload_date
309+ gmt = pytz .timezone ('GMT' )
310+ now = datetime .datetime .now (tz = gmt )
311+ time_since_upload = now - self .upload_date
302312
303313 if time_since_upload <= pre_rating_time :
304314 return - 1
@@ -372,7 +382,8 @@ def add_target(
372382 """
373383 name = request .json ()['name' ]
374384
375- if any (target .name == name for target in self .targets ):
385+ targets = (target for target in self .targets if not target .delete_date )
386+ if any (target .name == name for target in targets ):
376387 context .status_code = codes .FORBIDDEN
377388 body = {
378389 'transaction_id' : uuid .uuid4 ().hex ,
@@ -429,9 +440,9 @@ def delete_target(
429440 }
430441 return json_dump (body )
431442
432- self . targets = [
433- item for item in self . targets if item . target_id != target . target_id
434- ]
443+ gmt = pytz . timezone ( 'GMT' )
444+ now = datetime . datetime . now ( tz = gmt )
445+ target . delete_date = now
435446
436447 body = {
437448 'transaction_id' : uuid .uuid4 ().hex ,
@@ -457,29 +468,31 @@ def database_summary(
457468 [
458469 target for target in self .targets
459470 if target .status == TargetStatuses .SUCCESS .value
460- and target .active_flag
471+ and target .active_flag and not target . delete_date
461472 ],
462473 )
463474
464475 failed_images = len (
465476 [
466477 target for target in self .targets
467478 if target .status == TargetStatuses .FAILED .value
479+ and not target .delete_date
468480 ],
469481 )
470482
471483 inactive_images = len (
472484 [
473485 target for target in self .targets
474486 if target .status == TargetStatuses .SUCCESS .value
475- and not target .active_flag
487+ and not target .active_flag and not target . delete_date
476488 ],
477489 )
478490
479491 processing_images = len (
480492 [
481493 target for target in self .targets
482494 if target .status == TargetStatuses .PROCESSING .value
495+ and not target .delete_date
483496 ],
484497 )
485498
@@ -513,7 +526,10 @@ def target_list(
513526 Fake implementation of
514527 https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Get-a-Target-List-for-a-Cloud-Database
515528 """
516- results = [target .target_id for target in self .targets ]
529+ results = [
530+ target .target_id for target in self .targets
531+ if not target .delete_date
532+ ]
517533
518534 body : Dict [str , Union [str , List [str ]]] = {
519535 'transaction_id' : uuid .uuid4 ().hex ,
@@ -644,7 +660,10 @@ def update_target(
644660 if 'name' in request .json ():
645661 name = request .json ()['name' ]
646662 other_targets = set (self .targets ) - set ([target ])
647- if any (other .name == name for other in other_targets ):
663+ if any (
664+ other .name == name for other in other_targets
665+ if not other .delete_date
666+ ):
648667 context .status_code = codes .FORBIDDEN
649668 body = {
650669 'transaction_id' : uuid .uuid4 ().hex ,
@@ -665,7 +684,9 @@ def update_target(
665684 available_values = list (set (range (6 )) - set ([target .tracking_rating ]))
666685 target .processed_tracking_rating = random .choice (available_values )
667686
668- target .last_modified_date = datetime .datetime .now ()
687+ gmt = pytz .timezone ('GMT' )
688+ now = datetime .datetime .now (tz = gmt )
689+ target .last_modified_date = now
669690
670691 body = {
671692 'result_code' : ResultCodes .SUCCESS .value ,
0 commit comments