Skip to content

Commit

Permalink
add ProjectInvite.reset_date_expire() helper (#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Sep 5, 2024
1 parent af05c73 commit 174907b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Added
- **Projectroles**
- ``get_user_by_uuid()`` common template tag (#1478)
- ``ProjectInvite.get_url()`` helper (#1485)
- ``ProjectInvite.refresh_date_expire()`` helper (#1486)

Changed
-------
Expand Down Expand Up @@ -41,6 +42,7 @@ Removed

- **Projectroles**
- ``build_invite_url()`` utility method (#1485)
- ``get_expiry_date()`` utility method (#1486)


v1.0.1 (2024-08-08)
Expand Down
3 changes: 1 addition & 2 deletions projectroles/management/commands/batchupdateroles.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SODAR_CONSTANTS,
)
from projectroles.views import RoleAssignmentModifyMixin, ProjectInviteMixin
from projectroles.utils import get_expiry_date, build_secret
from projectroles.utils import build_secret


logger = ManagementCommandLogger(__name__)
Expand Down Expand Up @@ -116,7 +116,6 @@ def _invite_user(self, email, project, role):
project=project,
role=role,
issuer=self.issuer,
date_expire=get_expiry_date(),
secret=build_secret(),
)
self.handle_invite(invite, self.request, add_message=False)
Expand Down
25 changes: 25 additions & 0 deletions projectroles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.db import models
from django.db.models import Q
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from djangoplugins.models import Plugin
Expand Down Expand Up @@ -1101,6 +1102,22 @@ def __repr__(self):
values = (self.project.title, self.email, self.role.name, self.active)
return 'ProjectInvite({})'.format(', '.join(repr(v) for v in values))

@classmethod
def _get_date_expire(cls):
"""
Return expiry date based on current date + INVITE_EXPIRY_DAYS
:return: DateTime object
"""
return timezone.now() + timezone.timedelta(
days=settings.PROJECTROLES_INVITE_EXPIRY_DAYS
)

def save(self, *args, **kwargs):
if not self.pk and not self.date_expire: # Set date_expire on create
self.date_expire = self._get_date_expire()
super().save(*args, **kwargs)

# Custom row-level functions

def is_ldap(self):
Expand Down Expand Up @@ -1139,6 +1156,14 @@ def get_url(self, request):
)
)

def reset_date_expire(self):
"""
Reset date_expire to current date plus defined expiry days. Saves the
object.
"""
self.date_expire = self._get_date_expire()
self.save()


# RemoteSite -------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions projectroles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
CAT_DELIMITER_ERROR_MSG,
ROLE_PROJECT_TYPE_ERROR_MSG,
)
from projectroles.utils import build_secret, get_expiry_date
from projectroles.utils import build_secret
from projectroles.views import (
ProjectModifyMixin,
RoleAssignmentModifyMixin,
Expand Down Expand Up @@ -349,7 +349,7 @@ def validate(self, attrs):

def create(self, validated_data):
validated_data['issuer'] = self.context['request'].user
validated_data['date_expire'] = get_expiry_date()
# validated_data['date_expire'] = get_expiry_date()
validated_data['secret'] = build_secret()
return super().create(validated_data)

Expand Down
20 changes: 9 additions & 11 deletions projectroles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import uuid

from django.conf import settings
from django.contrib.auth.models import Group
from django.core.exceptions import ValidationError
from django.forms.models import model_to_dict
Expand Down Expand Up @@ -149,16 +148,7 @@ def make_invite(
'role': role,
'issuer': issuer,
'message': message,
'date_expire': (
date_expire
if date_expire
else (
timezone.now()
+ timezone.timedelta(
days=settings.PROJECTROLES_INVITE_EXPIRY_DAYS
)
)
),
'date_expire': date_expire,
'secret': secret or SECRET,
'active': True,
}
Expand Down Expand Up @@ -1016,6 +1006,14 @@ def test_get_url(self):
self.invite.get_url(request), request.build_absolute_uri()
)

def test_reset_date_expire(self):
"""Test reset_date_expire()"""
old_date = self.invite.date_expire
self.assertIsNotNone(old_date)
self.invite.reset_date_expire()
self.assertNotEqual(self.invite.date_expire, old_date)
self.assertTrue(self.invite.date_expire > old_date)

@override_settings(
ENABLE_LDAP=True,
AUTH_LDAP_USERNAME_DOMAIN='xyz',
Expand Down
11 changes: 0 additions & 11 deletions projectroles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

from django.conf import settings
from django.urls import reverse
from django.utils import timezone

from projectroles.plugins import get_active_plugins
from projectroles.models import SODAR_CONSTANTS

# Settings
SECRET_LENGTH = getattr(settings, 'PROJECTROLES_SECRET_LENGTH', 32)
INVITE_EXPIRY_DAYS = settings.PROJECTROLES_INVITE_EXPIRY_DAYS

# SODAR constants
PROJECT_TYPE_PROJECT = SODAR_CONSTANTS['PROJECT_TYPE_PROJECT']
Expand Down Expand Up @@ -77,15 +75,6 @@ def build_secret(length=SECRET_LENGTH):
)


def get_expiry_date():
"""
Return expiry date based on current date + INVITE_EXPIRY_DAYS
:return: DateTime object
"""
return timezone.now() + timezone.timedelta(days=INVITE_EXPIRY_DAYS)


def get_app_names():
"""Return list of names for locally installed non-django apps"""
ret = []
Expand Down
5 changes: 2 additions & 3 deletions projectroles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
get_backend_api,
)
from projectroles.remote_projects import RemoteProjectAPI
from projectroles.utils import get_expiry_date, get_display_name
from projectroles.utils import get_display_name


app_settings = AppSettingAPI()
Expand Down Expand Up @@ -2935,8 +2935,7 @@ def get(self, *args, **kwargs):
)
)
# Reset invite expiration date
invite.date_expire = get_expiry_date()
invite.save()
invite.reset_date_expire()
# Resend mail and add to timeline
self.handle_invite(invite=invite, request=self.request, resend=True)
return redirect(
Expand Down

0 comments on commit 174907b

Please sign in to comment.