Skip to content

Commit

Permalink
Invitation admin features (#423)
Browse files Browse the repository at this point in the history
* Invitation admin should have invite url and a way to reset invitation

display invite url in admin page of invitation

Resend invitation to multiple invitations (action)

This change so when a user did not receive a invitation for somereason, we can initate a resend so they can use it

* use get_invite_url from invitation models in sharing views

* remove reverse which is unused import
  • Loading branch information
AnishReddyRavula authored Sep 29, 2023
1 parent 5b25d37 commit 88f0b02
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
52 changes: 49 additions & 3 deletions projects/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from django.contrib import admin
from django.utils.html import format_html

from projects.models import (
ChameleonPublication,
Funding,
Invitation,
Publication,
Funding,
ChameleonPublication,
PublicationSource,
)
from projects.views import resend_invitation


class ProjectFields:
Expand Down Expand Up @@ -116,8 +118,52 @@ class PublicationSourceAdmin(PublicationFields, admin.ModelAdmin):
)


class InvitationAdmin(admin.ModelAdmin):
list_display = ["email_address", "status", "date_issued", "expiry_list_display"]
fields = [
"project",
"email_address",
"status",
"invite_link",
"user_issued",
"date_issued",
"user_accepted",
"date_accepted",
"date_expires",
"duration",
]
readonly_fields = [
"invite_link",
"user_issued",
"date_issued",
"email_address",
"user_accepted",
"date_accepted",
"status",
]
actions = ["resend_invitation"]

def invite_link(self, obj):
if obj.status == Invitation.STATUS_ISSUED:
url = obj.get_invite_url()
return format_html(f'<a href="{url}" target="_blank">Invite Link</a>')
return ""

@admin.display(description="Expiry")
def expiry_list_display(self, obj):
return f"{'EXPIRED' if obj._is_expired() else obj.date_expires}"

def resend_invitation(self, request, invitations):
user_issued = request.user
for invitation in invitations:
resend_invitation(invitation.id, user_issued, request)
self.message_user(request, f"Invitations resent by {user_issued}.")

resend_invitation.short_description = "Resend Invitations"


admin.site.register(Publication, PublicationAdmin)
admin.site.register(Invitation)
admin.site.register(Invitation, InvitationAdmin)
admin.site.register(Funding, FundingAdmin)
admin.site.register(ChameleonPublication, ChameleonPublicationAdmin)
admin.site.register(PublicationSource, PublicationSourceAdmin)
17 changes: 17 additions & 0 deletions projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ def _is_accepted(self):
def _is_expired(self):
return self.date_expires < timezone.now()

def get_invite_url(self, request=None):
"""Returns invitation URL
Args:
request (HttpRequest, optional): Returns absolute URL if the invitation.
Defaults to None.
Returns:
str
"""
relative_url = reverse(
"projects:accept_invite", kwargs={"invite_code": self.email_code}
)
if request:
return request.build_absolute_uri(relative_url)
return relative_url


class JoinLink(models.Model):
"""
Expand Down
8 changes: 1 addition & 7 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,6 @@ def format_timedelta(timedelta_instance):
return str(timedelta_instance).split(".")[0]


def get_invite_url(request, code):
return request.build_absolute_uri(
reverse("projects:accept_invite", kwargs={"invite_code": code})
)


def notify_join_request_user(django_request, join_request):
project = join_request.join_link.project
link = django_request.build_absolute_uri(
Expand Down Expand Up @@ -698,7 +692,7 @@ def add_project_invitation(
def send_invitation_email(invitation, request):
project_title = invitation.project.title
project_charge_code = invitation.project.charge_code
url = get_invite_url(request, invitation.email_code)
url = invitation.get_invite_url(request)
help_url = request.build_absolute_uri(reverse("djangoRT:mytickets"))
subject = f'Invitation for project "{project_title}" ({project_charge_code})'
body = f"""
Expand Down
3 changes: 1 addition & 2 deletions sharing_portal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from projects.util import get_project_members
from projects.views import (
add_project_invitation,
get_invite_url,
manage_membership_in_scope,
get_project_membership_managers,
is_membership_manager,
Expand Down Expand Up @@ -1023,7 +1022,7 @@ def send_request_decision_mail(request, daypass_request, daypass_project):
)
daypass_request.invitation = invite
daypass_request.save()
url = get_invite_url(request, invite.email_code)
url = invite.get_invite_url(request)
artifact_url = request.build_absolute_uri(
reverse("sharing_portal:detail", args=[artifact["uuid"]])
)
Expand Down

0 comments on commit 88f0b02

Please sign in to comment.