Adding all the status filter and custom filter set up for dropdown

This commit is contained in:
Rebecca Hsieh 2025-02-04 14:34:00 -08:00
parent d8d9475938
commit 77ddc6700a
No known key found for this signature in database
2 changed files with 80 additions and 11 deletions

View file

@ -1478,9 +1478,31 @@ class BaseInvitationAdmin(ListHeaderAdmin):
return response return response
# class DomainInvitationAdminForm(forms.ModelForm):
# """Custom form for DomainInvitation in admin to only allow cancellations."""
# STATUS_CHOICES = [
# ("", "------"), # no action
# ("canceled", "Canceled"),
# ]
# status = forms.ChoiceField(choices=STATUS_CHOICES, required=False, label="Status")
# class Meta:
# model = models.DomainInvitation
# fields = "__all__"
# def clean_status(self):
# # Clean status - we purposely dont edit anything so we dont mess with the state
# status = self.cleaned_data.get("status")
# return status
class DomainInvitationAdmin(BaseInvitationAdmin): class DomainInvitationAdmin(BaseInvitationAdmin):
"""Custom domain invitation admin class.""" """Custom domain invitation admin class."""
# form = DomainInvitationAdminForm
class Meta: class Meta:
model = models.DomainInvitation model = models.DomainInvitation
fields = "__all__" fields = "__all__"
@ -1505,23 +1527,49 @@ class DomainInvitationAdmin(BaseInvitationAdmin):
search_help_text = "Search by email or domain." search_help_text = "Search by email or domain."
# Mark the FSM field 'status' as readonly # # Mark the FSM field 'status' as readonly
# to allow admin users to create Domain Invitations # # to allow admin users to create Domain Invitations
# without triggering the FSM Transition Not Allowed # # without triggering the FSM Transition Not Allowed
# error. # # error.
# readonly_fields = ["status"]
# Now it can be edited
readonly_fields = ["status"] readonly_fields = ["status"]
autocomplete_fields = ["domain"] autocomplete_fields = ["domain"]
change_form_template = "django/admin/domain_invitation_change_form.html" change_form_template = "django/admin/domain_invitation_change_form.html"
# Select domain invitations to change -> Domain invitations # # Custom status filter within DomainInvitationAdmin
def changelist_view(self, request, extra_context=None): # class StatusListFilter(admin.SimpleListFilter):
if extra_context is None: # # custom filter for status field
extra_context = {}
extra_context["tabtitle"] = "Domain invitations" # title = _("status")
# Get the filtered values # parameter_name = "status"
return super().changelist_view(request, extra_context=extra_context)
# def lookups(self, request, model_admin):
# # only return cancel as option
# return [
# ('canceled', _('Canceled')),
# ('invited', _('Invited')),
# ('retrieved', _('Retrieved')),
# ]
# def queryset(self, request, queryset):
# """Filter the queryset based on the selected status."""
# if self.value():
# return queryset.filter(status=self.value()) # Apply the filter based on the selected status
# return queryset
# list_filter = (StatusListFilter,) # Apply the custom filter to the list view
# # Select domain invitations to change -> Domain invitations
# def changelist_view(self, request, extra_context=None):
# if extra_context is None:
# extra_context = {}
# extra_context["tabtitle"] = "Domain invitations"
# # Get the filtered values
# return super().changelist_view(request, extra_context=extra_context)
def save_model(self, request, obj, form, change): def save_model(self, request, obj, form, change):
""" """
@ -1531,6 +1579,23 @@ class DomainInvitationAdmin(BaseInvitationAdmin):
which will be successful if a single User exists for that email; otherwise, will which will be successful if a single User exists for that email; otherwise, will
just continue to create the invitation. just continue to create the invitation.
""" """
# print("***** IN SAVE_MODEL, OUTSIDE OF CHANGE")
# # If there is a change and it's related to status, look for canceled
# if change and "status" in form.changed_data:
# print("********* DO WE COME INTO THE CHANGE SECTION")
# if obj.status == DomainInvitation.DomainInvitationStatus.CANCELED:
# # Call the transition method to change the status
# obj.cancel_invitation()
# messages.success(request, f"Invitation for {obj.email} has been canceled.")
# return super().save_model(request, obj, form, change)
# # if invited/retrieved dont alow manual changes
# if obj.status not in [DomainInvitation.DomainInvitationStatus.INVITED, DomainInvitation.DomainInvitationStatus.RETRIEVED]:
# messages.error(request, "You cannot manually set the status to anything other than 'invited' or 'retrieved'.")
# return
if not change: if not change:
domain = obj.domain domain = obj.domain
domain_org = getattr(domain.domain_info, "portfolio", None) domain_org = getattr(domain.domain_info, "portfolio", None)

View file

@ -78,6 +78,10 @@ class DomainInvitation(TimeStampedModel):
@transition(field="status", source=DomainInvitationStatus.INVITED, target=DomainInvitationStatus.CANCELED) @transition(field="status", source=DomainInvitationStatus.INVITED, target=DomainInvitationStatus.CANCELED)
def cancel_invitation(self): def cancel_invitation(self):
"""When an invitation is canceled, change the status to canceled""" """When an invitation is canceled, change the status to canceled"""
# print("***** IN CANCEL_INVITATION SECTION")
# logger.info(f"Invitation for {self.email} to {self.domain} has been canceled.")
# print("WHEN INVITATION IS CANCELED > CHANGE STATUS TO CANCELED")
# Send email here maybe?
pass pass
@transition(field="status", source=DomainInvitationStatus.CANCELED, target=DomainInvitationStatus.INVITED) @transition(field="status", source=DomainInvitationStatus.CANCELED, target=DomainInvitationStatus.INVITED)