diff --git a/pinax/referrals/admin.py b/pinax/referrals/admin.py index d5d9a74..c690545 100644 --- a/pinax/referrals/admin.py +++ b/pinax/referrals/admin.py @@ -1,32 +1,53 @@ from django.contrib import admin +from django.urls import reverse +from django.utils.html import format_html from .models import Referral, ReferralResponse -admin.site.register( - Referral, - list_display=[ + +@admin.register(Referral) +class ReferralAdmin(admin.ModelAdmin): + list_display = [ "user", "code", "label", "redirect_to", "target_content_type", - "target_object_id" - ], - readonly_fields=["code", "created_at"], - list_filter=["target_content_type", "created_at"], - search_fields=["user__first_name", "user__last_name", "user__email", "user__username", "code"] -) + "target_object_id", + "url", + ] + readonly_fields = ["code", "created_at"] + list_filter = ["target_content_type", "created_at"] + search_fields = ["user__first_name", "user__last_name", "user__email", "user__username", "code"] + autocomplete_fields = ["user"] + -admin.site.register( - ReferralResponse, - list_display=[ +@admin.register(ReferralResponse) +class ReferralResponseAdmin(admin.ModelAdmin): + list_display = [ "referral", "session_key", "user", "ip_address", - "action" - ], - readonly_fields=["referral", "session_key", "user", "ip_address", "action"], - list_filter=["action", "created_at"], - search_fields=["referral__code", "referral__user__username", "ip_address"] -) + "action", + "target_object_link", + ] + readonly_fields = ["referral", "session_key", "user", "ip_address", "action", "target_object_link"] + list_filter = ["action", "created_at"] + search_fields = [ + "referral__code", + "referral__user__email", + "referral__user__username", + "referral__user__first_name", + "referral__user__last_name", + "user__email", + "user__username", + "user__first_name", + "user__last_name", + "ip_address", + ] + + def target_object_link(self, obj): + if obj.pk and obj.target: + admin_link = reverse("admin:%s_%s_change" % (obj.target_content_type.app_label, obj.target_content_type.model), args=(obj.target.pk,)) + return format_html('{}', admin_link, obj.target.__str__()) diff --git a/pinax/referrals/models.py b/pinax/referrals/models.py index deda4e1..6ea0332 100644 --- a/pinax/referrals/models.py +++ b/pinax/referrals/models.py @@ -91,16 +91,22 @@ def record_response(cls, request, action_string, target=None): return referral.respond(request, action_string, target=target) @classmethod - def referral_for_request(cls, request): + def referral_responses_for_request(cls, request): if request.user.is_authenticated: qs = ReferralResponse.objects.filter(user=request.user) else: qs = ReferralResponse.objects.filter(session_key=request.session.session_key) - try: - return qs.order_by("-created_at")[0].referral - except IndexError: - pass + return qs.order_by("-created_at") + + @classmethod + def referral_for_request(cls, request): + responses = cls.referral_responses_for_request(request) + if responses: + try: + return responses[0].referral + except IndexError: + pass def link_responses_to_user(self, user, session_key): for response in self.responses.filter(session_key=session_key, user__isnull=True): @@ -146,8 +152,8 @@ class ReferralResponse(models.Model): ip_address = models.CharField(max_length=265) action = models.CharField(max_length=128) - target_content_type = models.ForeignKey(ContentType, null=True, on_delete=models.SET_NULL) - target_object_id = models.PositiveIntegerField(null=True) + target_content_type = models.ForeignKey(ContentType, null=True, blank=True, on_delete=models.SET_NULL) + target_object_id = models.PositiveIntegerField(null=True, blank=True) target = GenericForeignKey( ct_field="target_content_type", fk_field="target_object_id"