Skip to content

Commit

Permalink
fix RemoteProject.get_access_date() null date sorting (#1437)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jun 19, 2024
1 parent 82de9f3 commit aa2b724
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Fixed
- Revoked remote projects displayed in project detail view (#1432)
- Invalid URLs for remote peer projects in project detail view (#1435)
- Redundant ``Project.get_source_site()`` calls in project detail view (#1436)
- ``RemoteSite.get_access_date()`` invalid date sorting (#1437)
- **Sodarcache**
- REST API set view ``app_name`` incorrectly set (#1405)

Expand Down
6 changes: 4 additions & 2 deletions projectroles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,8 +1194,10 @@ def _validate_mode(self):

def get_access_date(self):
"""Return date of latest project access by remote site"""
projects = RemoteProject.objects.filter(site=self).order_by(
'-date_access'
projects = (
RemoteProject.objects.filter(site=self)
.exclude(date_access__isnull=True)
.order_by('-date_access')
)
if projects.count() > 0:
return projects.first().date_access
Expand Down
66 changes: 60 additions & 6 deletions projectroles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
PROJECT_TYPE_PROJECT = SODAR_CONSTANTS['PROJECT_TYPE_PROJECT']
SITE_MODE_TARGET = SODAR_CONSTANTS['SITE_MODE_TARGET']
SITE_MODE_SOURCE = SODAR_CONSTANTS['SITE_MODE_SOURCE']
REMOTE_LEVEL_VIEW_AVAIL = SODAR_CONSTANTS['REMOTE_LEVEL_VIEW_AVAIL']
REMOTE_LEVEL_READ_ROLES = SODAR_CONSTANTS['REMOTE_LEVEL_READ_ROLES']
REMOTE_LEVEL_REVOKED = SODAR_CONSTANTS['REMOTE_LEVEL_REVOKED']

# Local constants
SECRET = 'rsd886hi8276nypuvw066sbvv0rb2a6x'
Expand Down Expand Up @@ -273,7 +276,7 @@ def set_up_as_target(cls, projects):
project_uuid=project.sodar_uuid,
project=project,
site=source_site,
level=SODAR_CONSTANTS['REMOTE_LEVEL_READ_ROLES'],
level=REMOTE_LEVEL_READ_ROLES,
)
)
return source_site, remote_projects
Expand Down Expand Up @@ -1314,7 +1317,11 @@ def test_get_value_json(self):


class TestRemoteSite(
ProjectMixin, RoleAssignmentMixin, RemoteSiteMixin, TestCase
ProjectMixin,
RoleAssignmentMixin,
RemoteSiteMixin,
RemoteProjectMixin,
TestCase,
):
"""Tests for RemoteSite"""

Expand Down Expand Up @@ -1380,6 +1387,53 @@ def test_validate_mode(self):
mode='uGaj9eicQueib1th',
)

def test_get_access_date_no_projects(self):
"""Test get_access_date() with no remote projects"""
self.assertEqual(self.site.get_access_date(), None)

def test_get_access_date_not_accessed(self):
"""Test get_access_date() with non-accessed project"""
self.make_remote_project(
project_uuid=self.project.sodar_uuid,
site=self.site,
level=REMOTE_LEVEL_READ_ROLES,
project=self.project,
date_access=None,
)
self.assertEqual(self.site.get_access_date(), None)

def test_get_access_date_accessed(self):
"""Test get_access_date() with accessed project"""
date_access = timezone.now()
self.make_remote_project(
project_uuid=self.project.sodar_uuid,
site=self.site,
level=REMOTE_LEVEL_READ_ROLES,
project=self.project,
date_access=date_access,
)
self.assertEqual(self.site.get_access_date(), date_access)

def test_get_access_date_both_access_types(self):
"""Test get_access_date() with accessed and non-accessed projects"""
date_access = timezone.now()
self.make_remote_project(
project_uuid=self.project.sodar_uuid,
site=self.site,
level=REMOTE_LEVEL_READ_ROLES,
project=self.project,
date_access=date_access,
)
project2 = self.make_project('Project2', PROJECT_TYPE_PROJECT, None)
self.make_remote_project(
project_uuid=project2.sodar_uuid,
site=self.site,
level=REMOTE_LEVEL_READ_ROLES,
project=project2,
date_access=None,
)
self.assertEqual(self.site.get_access_date(), date_access)


class TestRemoteProject(
ProjectMixin,
Expand Down Expand Up @@ -1418,7 +1472,7 @@ def setUp(self):
self.remote_project = self.make_remote_project(
project_uuid=self.project.sodar_uuid,
site=self.site,
level=SODAR_CONSTANTS['REMOTE_LEVEL_VIEW_AVAIL'],
level=REMOTE_LEVEL_VIEW_AVAIL,
project=self.project,
)

Expand All @@ -1429,7 +1483,7 @@ def test_initialization(self):
'project_uuid': self.project.sodar_uuid,
'project': self.project.pk,
'site': self.site.pk,
'level': SODAR_CONSTANTS['REMOTE_LEVEL_VIEW_AVAIL'],
'level': REMOTE_LEVEL_VIEW_AVAIL,
'date_access': None,
'sodar_uuid': self.remote_project.sodar_uuid,
}
Expand Down Expand Up @@ -1477,7 +1531,7 @@ def test_is_revoked_target(self):
self.site.mode = SITE_MODE_SOURCE
self.site.save()
self.assertEqual(self.project.is_revoked(), False)
self.remote_project.level = SODAR_CONSTANTS['REMOTE_LEVEL_REVOKED']
self.remote_project.level = REMOTE_LEVEL_REVOKED
self.remote_project.save()
self.assertEqual(self.project.is_revoked(), True)

Expand Down Expand Up @@ -1511,7 +1565,7 @@ def test_create_duplicate(self):
self.make_remote_project(
project_uuid=self.project.sodar_uuid,
site=self.site,
level=SODAR_CONSTANTS['REMOTE_LEVEL_READ_ROLES'],
level=REMOTE_LEVEL_READ_ROLES,
project=self.project,
)

Expand Down

0 comments on commit aa2b724

Please sign in to comment.