diff --git a/physionet-django/events/models.py b/physionet-django/events/models.py index 8975ec5622..8dc8865a6a 100644 --- a/physionet-django/events/models.py +++ b/physionet-django/events/models.py @@ -1,4 +1,7 @@ +from datetime import datetime + from django.db import models, transaction +from django.db.models import Manager, Q from django.utils.crypto import get_random_string from django.utils import timezone from django.utils.translation import gettext_lazy as _ @@ -188,10 +191,25 @@ class Meta: default_permissions = () +class EventDatasetManager(Manager): + def accessible_by(self, user): + """ + Returns all the EventDatasets that are accessible to the user. + """ + accessible_events = Event.objects.filter(Q(host=user) | Q(participants__user=user)) + active_events = accessible_events.filter(end_date__gte=datetime.now()) + accessible_datasets = EventDataset.objects.filter(event__in=active_events, is_active=True) + return accessible_datasets + + def get_queryset(self): + return super().get_queryset().filter(is_active=True) + + class EventDataset(models.Model): """ Captures information about datasets for events. """ + objects = EventDatasetManager() class EventDatasetAccessType(models.TextChoices): GOOGLE_BIG_QUERY = 'GBQ', _('Google BigQuery') diff --git a/physionet-django/project/managers/publishedproject.py b/physionet-django/project/managers/publishedproject.py index 4df6f08d95..1db08cb037 100644 --- a/physionet-django/project/managers/publishedproject.py +++ b/physionet-django/project/managers/publishedproject.py @@ -52,13 +52,8 @@ def accessible_by(self, user): ) # add projects that are accessible through events - events_all = Event.objects.filter(Q(host=user) | Q(participants__user=user)) - active_events = set(events_all.filter(end_date__gte=datetime.now())) - accessible_datasets = EventDataset.objects.filter(event__in=active_events, is_active=True) - accessible_projects_ids = [] - for event_dataset in accessible_datasets: - if event_dataset.has_access(user): - accessible_projects_ids.append(event_dataset.dataset.id) - query |= Q(id__in=accessible_projects_ids) + accessible_event_dataset = EventDataset.objects.accessible_by(user) + accessible_projects_ids = [dataset.dataset.pk for dataset in accessible_event_dataset] + query |= Q(pk__in=accessible_projects_ids) return self.filter(query)