-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
signal handlers for syncing of project views and tasks #1218
Open
MyPyDavid
wants to merge
8
commits into
2.3.0
Choose a base branch
from
feat-sync-project-views-and-tasks
base: 2.3.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9065876
feat(project,handlers): add sync for views and tasks, #966 #1198
MyPyDavid 009d592
fix: remove views if catalog is changed
m6121 88e4583
Merge pull request #1200 from rdmorganiser/fix/1198-views-catalogs-ch…
MyPyDavid edb842e
feat(project, handlers): refactor and add handler for project save, c…
MyPyDavid ca40992
fix(project,handlers): ignore signals at project create
MyPyDavid d93f15f
refactor(project,handlers): simplify pre and post save
MyPyDavid 4dc9f04
fix(managers,tasks,views): use project.owners for filter
MyPyDavid e19be64
feat(project, update): prevent views or tasks update when sync is ena…
MyPyDavid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,16 @@ class Meta: | |
'catalog': forms.RadioSelect() | ||
} | ||
|
||
def save(self, commit=True, *args, **kwargs): | ||
if 'cancel' in self.data: | ||
return self.instance | ||
|
||
# if the catalog is the same, do nothing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to prevent calling the |
||
if self.instance.catalog.id == self.cleaned_data.get('catalog'): | ||
return self.instance | ||
|
||
return super().save(commit=commit) | ||
|
||
|
||
class ProjectUpdateTasksForm(forms.ModelForm): | ||
|
||
|
@@ -180,6 +190,22 @@ class Meta: | |
'tasks': forms.CheckboxSelectMultiple() | ||
} | ||
|
||
def save(self, commit=True, *args, **kwargs): | ||
if 'cancel' in self.data: | ||
return self.instance | ||
|
||
# If the tasks are the same, do nothing | ||
current_tasks = set(self.instance.tasks.values_list('id', flat=True)) | ||
new_tasks = set(self.cleaned_data.get('tasks').values_list('id', flat=True)) if self.cleaned_data.get( | ||
'tasks') else set() | ||
|
||
if current_tasks == new_tasks: | ||
return self.instance | ||
|
||
# Save the updated tasks | ||
self.instance.tasks.set(self.cleaned_data.get('tasks')) | ||
return super().save(commit=commit) | ||
|
||
|
||
class ProjectUpdateViewsForm(forms.ModelForm): | ||
|
||
|
@@ -200,6 +226,22 @@ class Meta: | |
'views': forms.CheckboxSelectMultiple() | ||
} | ||
|
||
def save(self, commit=True, *args, **kwargs): | ||
if 'cancel' in self.data: | ||
return self.instance | ||
|
||
# If the views are the same, do nothing | ||
current_views = set(self.instance.views.values_list('id', flat=True)) | ||
new_views = ( set(self.cleaned_data.get('views').values_list('id', flat=True)) | ||
if self.cleaned_data.get('views') else set() | ||
) | ||
|
||
if current_views == new_views: | ||
return self.instance | ||
|
||
# Save the updated views | ||
self.instance.views.set(self.cleaned_data.get('views')) | ||
return super().save(commit=commit) | ||
|
||
class ProjectUpdateParentForm(forms.ModelForm): | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,25 @@ | ||
import logging | ||
|
||
from django.contrib.auth.models import User | ||
from django.contrib.sites.models import Site | ||
from django.db.models.signals import m2m_changed | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
|
||
from rdmo.projects.models import Membership, Project | ||
from rdmo.questions.models import Catalog | ||
from rdmo.projects.models import Project | ||
from rdmo.views.models import View | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@receiver(m2m_changed, sender=View.catalogs.through) | ||
def m2m_changed_view_catalog_signal(sender, instance, **kwargs): | ||
catalogs = instance.catalogs.all() | ||
|
||
if catalogs: | ||
catalog_candidates = Catalog.objects.exclude(id__in=[catalog.id for catalog in catalogs]) | ||
|
||
# Remove catalog candidates for all sites | ||
projects = Project.objects.filter(catalog__in=catalog_candidates, views=instance) | ||
for proj in projects: | ||
proj.views.remove(instance) | ||
|
||
|
||
@receiver(m2m_changed, sender=View.sites.through) | ||
def m2m_changed_view_sites_signal(sender, instance, **kwargs): | ||
sites = instance.sites.all() | ||
catalogs = instance.catalogs.all() | ||
|
||
if sites: | ||
site_candidates = Site.objects.exclude(id__in=[site.id for site in sites]) | ||
if not catalogs: | ||
# if no catalogs are selected, update all | ||
catalogs = Catalog.objects.all() | ||
|
||
# Restrict chosen catalogs for chosen sites | ||
projects = Project.objects.filter(site__in=site_candidates, catalog__in=catalogs, views=instance) | ||
for project in projects: | ||
project.views.remove(instance) | ||
|
||
@receiver(post_save, sender=Project) | ||
def update_views_on_catalog_change(sender, instance, **kwargs): | ||
# remove views that are no longer available | ||
view_candidates = instance.views.exclude(catalogs__in=[instance.catalog]) \ | ||
.exclude(catalogs=None) | ||
|
||
@receiver(m2m_changed, sender=View.groups.through) | ||
def m2m_changed_view_groups_signal(sender, instance, **kwargs): | ||
groups = instance.groups.all() | ||
catalogs = instance.catalogs.all() | ||
for view in view_candidates: | ||
instance.views.remove(view) | ||
|
||
if groups: | ||
users = User.objects.exclude(groups__in=groups) | ||
memberships = [membership.id for membership in Membership.objects.filter(role='owner', user__in=users)] | ||
if not catalogs: | ||
# if no catalogs are selected, update all | ||
catalogs = Catalog.objects.all() | ||
# add views that are now available | ||
view_candidates = View.objects.exclude(id__in=[v.id for v in instance.views.all()]) \ | ||
.filter_current_site() \ | ||
.filter_catalog(instance.catalog) | ||
# .filter_group(self.request.user) \ | ||
# .filter_availability(self.request.user).exists() | ||
|
||
# Restrict chosen catalogs for chosen groups | ||
projects = Project.objects.filter(memberships__in=list(memberships), catalog__in=catalogs, views=instance) | ||
for project in projects: | ||
project.views.remove(instance) | ||
for view in view_candidates: | ||
instance.views.add(view) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from django.contrib.auth.models import Group, User | ||
from django.contrib.sites.models import Site | ||
|
||
from rdmo.projects.models import Membership, Project | ||
|
||
from ...questions.models import Catalog | ||
from .utils import add_instance_to_projects, remove_instance_from_projects | ||
|
||
|
||
def m2m_catalogs_changed_projects_sync_signal_handler(action, pk_set, instance, project_field): | ||
""" | ||
Update project relationships for m2m_changed signals. | ||
|
||
Args: | ||
action (str): The m2m_changed action (post_add, post_remove, post_clear). | ||
pk_set (set): The set of primary keys for the related model instances. | ||
instance (Model): The instance being updated (e.g., View or Task). | ||
project_field (str): The field on Project to update (e.g., 'views', 'tasks'). | ||
""" | ||
if action == 'post_remove' and pk_set: | ||
related_instances = Catalog.objects.filter(pk__in=pk_set) | ||
projects_to_change = Project.objects.filter_catalogs(catalogs=related_instances).filter( | ||
**{project_field: instance} | ||
) | ||
remove_instance_from_projects(projects_to_change, project_field, instance) | ||
|
||
elif action == 'post_clear': | ||
projects_to_change = Project.objects.filter(**{project_field: instance}) | ||
remove_instance_from_projects(projects_to_change, project_field, instance) | ||
|
||
elif action == 'post_add' and pk_set: | ||
related_instances = Catalog.objects.filter(pk__in=pk_set) | ||
projects_to_change = Project.objects.filter_catalogs(catalogs=related_instances).exclude( | ||
**{project_field: instance} | ||
) | ||
add_instance_to_projects(projects_to_change, project_field, instance) | ||
|
||
|
||
def m2m_sites_changed_projects_sync_signal_handler(action, pk_set, instance, project_field): | ||
""" | ||
Synchronize Project relationships for m2m_changed signals triggered by site updates. | ||
|
||
Args: | ||
action (str): The m2m_changed action (post_add, post_remove, post_clear). | ||
pk_set (set): The set of primary keys for the related model instances. | ||
instance (Model): The instance being updated (e.g., View or Task). | ||
project_field (str): The field on Project to update (e.g., 'views', 'tasks'). | ||
""" | ||
if action == 'post_remove' and pk_set: | ||
related_sites = Site.objects.filter(pk__in=pk_set) | ||
catalogs = instance.catalogs.all() | ||
|
||
projects_to_change = Project.objects.filter_catalogs(catalogs=catalogs).filter( | ||
site__in=related_sites, | ||
**{project_field: instance} | ||
) | ||
remove_instance_from_projects(projects_to_change, project_field, instance) | ||
|
||
elif action == 'post_clear': | ||
projects_to_change = Project.objects.filter_catalogs().filter(**{project_field: instance}) | ||
remove_instance_from_projects(projects_to_change, project_field, instance) | ||
|
||
elif action == 'post_add' and pk_set: | ||
related_sites = Site.objects.filter(pk__in=pk_set) | ||
catalogs = instance.catalogs.all() | ||
|
||
projects_to_change = Project.objects.filter_catalogs(catalogs=catalogs).filter( | ||
site__in=related_sites | ||
).exclude(**{project_field: instance}) | ||
add_instance_to_projects(projects_to_change, project_field, instance) | ||
|
||
|
||
def m2m_groups_changed_projects_sync_signal_handler(action, pk_set, instance, project_field): | ||
""" | ||
Synchronize Project relationships for m2m_changed signals triggered by group updates. | ||
|
||
Args: | ||
action (str): The m2m_changed action (post_add, post_remove, post_clear). | ||
pk_set (set): The set of primary keys for the related model instances. | ||
instance (Model): The instance being updated (e.g., View or Task). | ||
project_field (str): The field on Project to update (e.g., 'views', 'tasks'). | ||
""" | ||
if action == 'post_remove' and pk_set: | ||
related_groups = Group.objects.filter(pk__in=pk_set) | ||
users = User.objects.filter(groups__in=related_groups) | ||
memberships = Membership.objects.filter(role='owner', user__in=users).values_list('id', flat=True) | ||
catalogs = instance.catalogs.all() | ||
|
||
projects_to_change = Project.objects.filter_catalogs(catalogs=catalogs).filter( | ||
memberships__in=memberships, | ||
**{project_field: instance} | ||
) | ||
remove_instance_from_projects(projects_to_change, project_field, instance) | ||
|
||
elif action == 'post_clear': | ||
# Remove all linked projects regardless of catalogs | ||
projects_to_change = Project.objects.filter_catalogs().filter(**{project_field: instance}) | ||
remove_instance_from_projects(projects_to_change, project_field, instance) | ||
|
||
elif action == 'post_add' and pk_set: | ||
related_groups = Group.objects.filter(pk__in=pk_set) | ||
users = User.objects.filter(groups__in=related_groups) | ||
memberships = Membership.objects.filter(role='owner', user__in=users).values_list('id', flat=True) | ||
catalogs = instance.catalogs.all() | ||
|
||
projects_to_change = Project.objects.filter_catalogs(catalogs=catalogs).filter( | ||
memberships__in=memberships | ||
).exclude(**{project_field: instance}) | ||
add_instance_to_projects(projects_to_change, project_field, instance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
from django.db.models.signals import m2m_changed | ||
from django.dispatch import receiver | ||
|
||
from rdmo.tasks.models import Task | ||
|
||
from .generic_handlers import ( | ||
m2m_catalogs_changed_projects_sync_signal_handler, | ||
m2m_groups_changed_projects_sync_signal_handler, | ||
m2m_sites_changed_projects_sync_signal_handler, | ||
) | ||
|
||
|
||
@receiver(m2m_changed, sender=Task.catalogs.through) | ||
def m2m_changed_task_catalog_signal(sender, instance, action, pk_set, **kwargs): | ||
m2m_catalogs_changed_projects_sync_signal_handler( | ||
action=action, | ||
pk_set=pk_set, | ||
instance=instance, | ||
project_field='tasks', | ||
) | ||
|
||
|
||
@receiver(m2m_changed, sender=Task.sites.through) | ||
def m2m_changed_task_sites_signal(sender, instance, action, pk_set, **kwargs): | ||
m2m_sites_changed_projects_sync_signal_handler( | ||
action=action, | ||
pk_set=pk_set, | ||
instance=instance, | ||
project_field='tasks' | ||
) | ||
|
||
|
||
@receiver(m2m_changed, sender=Task.groups.through) | ||
def m2m_changed_task_groups_signal(sender, instance, action, pk_set, **kwargs): | ||
m2m_groups_changed_projects_sync_signal_handler( | ||
action=action, | ||
pk_set=pk_set, | ||
instance=instance, | ||
project_field='tasks' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
from django.db.models.signals import m2m_changed | ||
from django.dispatch import receiver | ||
|
||
from rdmo.views.models import View | ||
|
||
from .generic_handlers import ( | ||
m2m_catalogs_changed_projects_sync_signal_handler, | ||
m2m_groups_changed_projects_sync_signal_handler, | ||
m2m_sites_changed_projects_sync_signal_handler, | ||
) | ||
|
||
|
||
@receiver(m2m_changed, sender=View.catalogs.through) | ||
def m2m_changed_view_catalog_signal(sender, instance, action, pk_set, **kwargs): | ||
m2m_catalogs_changed_projects_sync_signal_handler( | ||
action=action, | ||
pk_set=pk_set, | ||
instance=instance, | ||
project_field='views', | ||
) | ||
|
||
|
||
|
||
@receiver(m2m_changed, sender=View.sites.through) | ||
def m2m_changed_view_sites_signal(sender, instance, action, pk_set, **kwargs): | ||
m2m_sites_changed_projects_sync_signal_handler( | ||
action=action, | ||
pk_set=pk_set, | ||
instance=instance, | ||
project_field='views' | ||
) | ||
|
||
|
||
@receiver(m2m_changed, sender=View.groups.through) | ||
def m2m_changed_view_groups_signal(sender, instance, action, pk_set, **kwargs): | ||
m2m_groups_changed_projects_sync_signal_handler( | ||
action=action, | ||
pk_set=pk_set, | ||
instance=instance, | ||
project_field='views' | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was just still missing right? ;)