-
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
Remove views from existing projects on view update #431
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0e6ccd1
Remove views from existing projects on view update
m6121 b5d411e
Fix project tests with additional test data
m6121 545effb
Refactored implementation to use django signals
m6121 8934670
Removed unused imports
m6121 5424b90
Added signal for group filtering
m6121 37a8631
Added feature flag for removal
m6121 2d07cd4
Added configuration flag to default settings
m6121 554579f
FIxed bug, extended tests
m6121 9eacdd7
Moved signal tests into separate file
m6121 4096af9
Merge branch 'rdmorganiser:master' into view_update
m6121 dff207e
Merge branch 'dev' into view_update
m6121 15ecaa0
Moved implementation to projects, code formatting, minor improvements
m6121 1046772
Fixed accidentally deleted import, flake8 adjustments
m6121 3c8b920
flake8 adjustment
m6121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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.dispatch import receiver | ||
|
||
from rdmo.questions.models import Catalog | ||
from rdmo.projects.models import Project, Membership | ||
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(m2m_changed, sender=View.groups.through) | ||
def m2m_changed_view_groups_signal(sender, instance, **kwargs): | ||
groups = instance.groups.all() | ||
catalogs = instance.catalogs.all() | ||
|
||
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() | ||
|
||
# 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) |
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,60 @@ | ||
import itertools | ||
import pytest | ||
|
||
from django.contrib.auth.models import Group | ||
from django.contrib.sites.models import Site | ||
|
||
from rdmo.projects.models import Project | ||
from rdmo.questions.models import Catalog | ||
from rdmo.views.models import View | ||
|
||
view_update_tests = [ | ||
# tuples of: view_id, sites, catalogs, groups, project_id, project_exists | ||
('3', [], [], [], '10', True), | ||
('3', [2], [], [], '10', False), | ||
('3', [1, 2, 3], [], [], '10', True), | ||
('3', [], [2], [], '10', False), | ||
('3', [2], [2], [], '10', False), | ||
('3', [1, 2, 3], [2], [], '10', False), | ||
('3', [], [1, 2], [], '10', True), | ||
('3', [2], [1, 2], [], '10', False), | ||
('3', [1, 2, 3], [1, 2], [], '10', True), | ||
|
||
('3', [], [], [1], '10', False), | ||
('3', [2], [], [1], '10', False), | ||
('3', [1, 2, 3], [], [1], '10', False), | ||
('3', [], [2], [1], '10', False), | ||
('3', [2], [2], [1], '10', False), | ||
('3', [1, 2, 3], [2], [1], '10', False), | ||
('3', [], [1, 2], [1], '10', False), | ||
('3', [2], [1, 2], [1], '10', False), | ||
('3', [1, 2, 3], [1, 2], [1], '10', False), | ||
|
||
('3', [], [], [1, 2, 3, 4], '10', True), | ||
('3', [2], [], [1, 2, 3, 4], '10', False), | ||
('3', [1, 2, 3], [], [1, 2, 3, 4], '10', True), | ||
('3', [], [2], [1, 2, 3, 4], '10', False), | ||
('3', [2], [2], [1, 2, 3, 4], '10', False), | ||
('3', [1, 2, 3], [2], [1, 2, 3, 4], '10', False), | ||
('3', [], [1, 2], [1, 2, 3, 4], '10', True), | ||
('3', [2], [1, 2], [1, 2, 3, 4], '10', False), | ||
('3', [1, 2, 3], [1, 2], [1, 2, 3, 4], '10', True) | ||
] | ||
|
||
@pytest.mark.parametrize('view_id,sites,catalogs,groups,project_id,project_exists', view_update_tests) | ||
def test_update_projects(db, view_id, sites, catalogs, groups, project_id, project_exists): | ||
view = View.objects.get(pk=view_id) | ||
|
||
view.sites.set(Site.objects.filter(pk__in=sites)) | ||
view.catalogs.set(Catalog.objects.filter(pk__in=catalogs)) | ||
view.groups.set(Group.objects.filter(pk__in=groups)) | ||
|
||
assert sorted(list(itertools.chain.from_iterable(view.sites.all().values_list('pk')))) == sites | ||
assert sorted(list(itertools.chain.from_iterable(view.catalogs.all().values_list('pk')))) == catalogs | ||
assert sorted(list(itertools.chain.from_iterable(view.groups.all().values_list('pk')))) == groups | ||
|
||
if not project_exists: | ||
with pytest.raises(Project.DoesNotExist): | ||
Project.objects.filter(views=view).get(pk=project_id) | ||
else: | ||
assert Project.objects.filter(views=view).get(pk=project_id) |
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
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 |
---|---|---|
|
@@ -31,6 +31,8 @@ | |
|
||
PROJECT_SEND_INVITE = True | ||
|
||
PROJECT_REMOVE_VIEWS = True | ||
|
||
EMAIL_RECIPIENTS_CHOICES = [ | ||
('[email protected]', 'Emmi Email <[email protected]>'), | ||
] | ||
|
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
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.
I moved the implementation into the
projects
app. I think this makes sense as you suggested. Furthermore, I also moved it fromsignals.py
tohandlers.py
. However, I am not happy with the linking into theprojects/apps.py
due to the configuration flag.