Skip to content
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

[WIP] refactor access to dataset through events #1918

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions physionet-django/events/models.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand Down Expand Up @@ -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')
Expand Down
11 changes: 3 additions & 8 deletions physionet-django/project/managers/publishedproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)