-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds orgnaisation relations, filtering and permissions (#1658)
* Adds links and filters to organizations * Adds activity overview to an organization * Adds filtering tests * Assigns view permissions to the organisations groups to related objects
- Loading branch information
Showing
21 changed files
with
482 additions
and
12 deletions.
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
24 changes: 24 additions & 0 deletions
24
app/grandchallenge/algorithms/migrations/0003_algorithm_organizations.py
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,24 @@ | ||
# Generated by Django 3.1.1 on 2020-12-17 06:53 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("organizations", "0001_initial"), | ||
("algorithms", "0002_auto_20201214_0939"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="algorithm", | ||
name="organizations", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
help_text="The organizations associated with this algorithm", | ||
related_name="algorithms", | ||
to="organizations.Organization", | ||
), | ||
), | ||
] |
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
24 changes: 24 additions & 0 deletions
24
app/grandchallenge/archives/migrations/0004_archive_organizations.py
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,24 @@ | ||
# Generated by Django 3.1.1 on 2020-12-17 06:53 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("organizations", "0001_initial"), | ||
("archives", "0003_auto_20201215_0931"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="archive", | ||
name="organizations", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
help_text="The organizations associated with this archive", | ||
related_name="archives", | ||
to="organizations.Organization", | ||
), | ||
), | ||
] |
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
34 changes: 34 additions & 0 deletions
34
app/grandchallenge/challenges/migrations/0002_auto_20201217_0653.py
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,34 @@ | ||
# Generated by Django 3.1.1 on 2020-12-17 06:53 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("organizations", "0001_initial"), | ||
("challenges", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="challenge", | ||
name="organizations", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
help_text="The organizations associated with this challenge", | ||
related_name="challenges", | ||
to="organizations.Organization", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="externalchallenge", | ||
name="organizations", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
help_text="The organizations associated with this challenge", | ||
related_name="externalchallenges", | ||
to="organizations.Organization", | ||
), | ||
), | ||
] |
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 @@ | ||
default_app_config = "grandchallenge.organizations.apps.OrganizationsConfig" |
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,9 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class OrganizationsConfig(AppConfig): | ||
name = "grandchallenge.organizations" | ||
|
||
def ready(self): | ||
# noinspection PyUnresolvedReferences | ||
import grandchallenge.organizations.signals # noqa: F401 |
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,81 @@ | ||
from django.db.models.signals import m2m_changed | ||
from django.dispatch import receiver | ||
from guardian.shortcuts import assign_perm, remove_perm | ||
|
||
from grandchallenge.algorithms.models import Algorithm | ||
from grandchallenge.archives.models import Archive | ||
from grandchallenge.challenges.models import Challenge, ExternalChallenge | ||
from grandchallenge.reader_studies.models import ReaderStudy | ||
|
||
|
||
@receiver(m2m_changed, sender=Algorithm.organizations.through) | ||
@receiver(m2m_changed, sender=Archive.organizations.through) | ||
@receiver(m2m_changed, sender=Challenge.organizations.through) | ||
@receiver(m2m_changed, sender=ExternalChallenge.organizations.through) | ||
@receiver(m2m_changed, sender=ReaderStudy.organizations.through) | ||
def update_related_permissions( | ||
sender, instance, action, reverse, model, pk_set, **_ | ||
): | ||
if action not in ["post_add", "post_remove", "pre_clear"]: | ||
# nothing to do for the other actions | ||
return | ||
|
||
if sender == Algorithm.organizations.through: | ||
related_model = Algorithm | ||
related_name = "algorithms" | ||
elif sender == Archive.organizations.through: | ||
related_model = Archive | ||
related_name = "archives" | ||
elif sender == Challenge.organizations.through: | ||
related_model = Challenge | ||
related_name = "challenges" | ||
elif sender == ExternalChallenge.organizations.through: | ||
related_model = ExternalChallenge | ||
related_name = "externalchallenges" | ||
elif sender == ReaderStudy.organizations.through: | ||
related_model = ReaderStudy | ||
related_name = "readerstudies" | ||
else: | ||
raise RuntimeError(f"Unrecognised sender: {sender}") | ||
|
||
_update_related_view_permissions( | ||
action=action, | ||
instance=instance, | ||
model=model, | ||
pk_set=pk_set, | ||
reverse=reverse, | ||
related_model=related_model, | ||
related_name=related_name, | ||
) | ||
|
||
|
||
def _update_related_view_permissions( | ||
*, action, instance, model, pk_set, reverse, related_model, related_name, | ||
): | ||
if reverse: | ||
organizations = [instance] | ||
if pk_set is None: | ||
# When using a _clear action, pk_set is None | ||
# https://docs.djangoproject.com/en/2.2/ref/signals/#m2m-changed | ||
related_objects = getattr(instance, related_name).all() | ||
else: | ||
related_objects = model.objects.filter(pk__in=pk_set) | ||
else: | ||
related_objects = related_model.objects.filter(pk=instance.pk) | ||
if pk_set is None: | ||
# When using a _clear action, pk_set is None | ||
# https://docs.djangoproject.com/en/2.2/ref/signals/#m2m-changed | ||
organizations = instance.organizations.all() | ||
else: | ||
organizations = model.objects.filter(pk__in=pk_set) | ||
|
||
op = assign_perm if "add" in action else remove_perm | ||
perm = f"view_{related_model._meta.model_name}" | ||
|
||
for org in organizations: | ||
op( | ||
perm, org.editors_group, related_objects, | ||
) | ||
op( | ||
perm, org.members_group, related_objects, | ||
) |
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.