Skip to content

Commit

Permalink
refactor(project,handlers): simplify pre and post save
Browse files Browse the repository at this point in the history
Signed-off-by: David Wallace <[email protected]>
  • Loading branch information
MyPyDavid committed Jan 23, 2025
1 parent ca40992 commit d93f15f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
26 changes: 12 additions & 14 deletions rdmo/projects/handlers/project_save_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@

@receiver(pre_save, sender=Project)
def pre_save_project_sync_tasks_from_catalog(sender, instance, raw, update_fields, **kwargs):
if (raw or
instance.id is None or
(update_fields and 'catalog' not in update_fields)
):
if raw or (update_fields and 'catalog' not in update_fields):
return

# Fetch the original catalog from the database
original_instance = sender.objects.get(id=instance.id)
if original_instance.catalog == instance.catalog:
# Do nothing if the catalog has not changed
return
if instance.id is not None:
# Fetch the original catalog from the database
if sender.objects.get(id=instance.id).catalog == instance.catalog:
# Do nothing if the catalog has not changed
return

# Defer synchronization of views
setattr(instance, DEFERRED_SYNC_TASKS_KEY, True)


@receiver(post_save, sender=Project)
def post_save_project_sync_tasks_from_catalog(sender, instance, created, raw, **kwargs):
if not hasattr(instance, DEFERRED_SYNC_TASKS_KEY):
def post_save_project_sync_tasks_from_catalog(sender, instance, created, raw, update_fields, **kwargs):
if raw or (update_fields and 'catalog' not in update_fields):
return
if getattr(instance, DEFERRED_SYNC_TASKS_KEY, None) or (created and not raw):
# For existing projects with catalog changes, use deferred views
instance.views.set(Task.objects.filter_available_tasks_for_project(instance))

if hasattr(instance, DEFERRED_SYNC_TASKS_KEY):
instance.tasks.set(Task.objects.filter_available_tasks_for_project(instance).values_list('id', flat=True))
delattr(instance, DEFERRED_SYNC_TASKS_KEY)
23 changes: 10 additions & 13 deletions rdmo/projects/handlers/project_save_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,23 @@

@receiver(pre_save, sender=Project)
def pre_save_project_sync_views_from_catalog(sender, instance, raw, update_fields, **kwargs):
if (raw or
instance.id is None or
(update_fields and 'catalog' not in update_fields)
):
if raw or (update_fields and 'catalog' not in update_fields):
return

# Fetch the original catalog from the database
original_instance = sender.objects.get(id=instance.id)
if original_instance.catalog == instance.catalog:
# Do nothing if the catalog has not changed
return
if instance.id is not None:
# Fetch the original catalog from the database
if sender.objects.get(id=instance.id).catalog == instance.catalog:
# Do nothing if the catalog has not changed
return

# Defer synchronization of views
setattr(instance, DEFERRED_SYNC_VIEWS_KEY, True)

@receiver(post_save, sender=Project)
def post_save_project_sync_views_from_catalog(sender, instance, created, raw, update_fields, **kwargs):
if not hasattr(instance, DEFERRED_SYNC_VIEWS_KEY):
if raw or (update_fields and 'catalog' not in update_fields):
return
if getattr(instance, DEFERRED_SYNC_VIEWS_KEY, None) or (created and not raw):
# For existing projects with catalog changes, use deferred views
instance.views.set(View.objects.filter_available_views_for_project(instance))

if hasattr(instance, DEFERRED_SYNC_VIEWS_KEY):
instance.views.set(View.objects.filter_available_views_for_project(instance).values_list('id', flat=True))
delattr(instance, DEFERRED_SYNC_VIEWS_KEY)

0 comments on commit d93f15f

Please sign in to comment.