From d0ac7153cd9a88c5a9c6edef4f6d415b4d88143b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Mon, 16 Sep 2019 17:51:07 +0200 Subject: [PATCH 1/9] make admin classes overridable --- pinax/referrals/admin.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pinax/referrals/admin.py b/pinax/referrals/admin.py index d5d9a74..49646dd 100644 --- a/pinax/referrals/admin.py +++ b/pinax/referrals/admin.py @@ -2,31 +2,31 @@ 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"] -) + ] + 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"] + -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"] -) + ] + readonly_fields = ["referral", "session_key", "user", "ip_address", "action"] + list_filter = ["action", "created_at"] + search_fields = ["referral__code", "referral__user__username", "ip_address"] From a6348e9fa055acc6f6bef7b1ac241deb43c9e49a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Mon, 16 Sep 2019 17:51:33 +0200 Subject: [PATCH 2/9] add referral_responses_for_request() helper function --- pinax/referrals/models.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pinax/referrals/models.py b/pinax/referrals/models.py index c6a3589..5f19356 100644 --- a/pinax/referrals/models.py +++ b/pinax/referrals/models.py @@ -95,16 +95,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): From 4461dc567191c8844ab8d0a16beedcdde2e78d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Tue, 17 Sep 2019 08:52:18 +0200 Subject: [PATCH 3/9] few upgrades to the admin --- pinax/referrals/admin.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/pinax/referrals/admin.py b/pinax/referrals/admin.py index 49646dd..e9ae260 100644 --- a/pinax/referrals/admin.py +++ b/pinax/referrals/admin.py @@ -1,4 +1,6 @@ from django.contrib import admin +from django.urls import reverse +from django.utils.html import format_html from .models import Referral, ReferralResponse @@ -11,11 +13,13 @@ class ReferralAdmin(admin.ModelAdmin): "label", "redirect_to", "target_content_type", - "target_object_id" + "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.register(ReferralResponse) @@ -25,8 +29,25 @@ class ReferralResponseAdmin(admin.ModelAdmin): "session_key", "user", "ip_address", - "action" + "action", + "target_object_link", ] - readonly_fields = ["referral", "session_key", "user", "ip_address", "action"] + readonly_fields = ["referral", "session_key", "user", "ip_address", "action", "target_object_link"] list_filter = ["action", "created_at"] - search_fields = ["referral__code", "referral__user__username", "ip_address"] + 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__()) From d3411eed41fce6599f1de79f43cfb5f8f9266455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Tue, 17 Sep 2019 08:53:01 +0200 Subject: [PATCH 4/9] don't require context type fields (which are often blank) --- pinax/referrals/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pinax/referrals/models.py b/pinax/referrals/models.py index 5f19356..3252b1c 100644 --- a/pinax/referrals/models.py +++ b/pinax/referrals/models.py @@ -156,8 +156,8 @@ class ReferralResponse(models.Model): ip_address = models.CharField(max_length=45) 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" From 8cf86445b19aa384f4b1937837a5a71ccdde9ad3 Mon Sep 17 00:00:00 2001 From: KatherineMichel Date: Sat, 27 Jun 2020 19:03:53 -0500 Subject: [PATCH 5/9] Removing Python 2.7 support --- pinax/referrals/models.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pinax/referrals/models.py b/pinax/referrals/models.py index 3252b1c..08ea25e 100644 --- a/pinax/referrals/models.py +++ b/pinax/referrals/models.py @@ -1,12 +1,9 @@ -from __future__ import unicode_literals - from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from django.db import models from django.urls import reverse from django.utils import timezone -from django.utils.encoding import python_2_unicode_compatible from .conf import settings from .signals import user_linked_to_response @@ -14,7 +11,6 @@ AUTH_USER_MODEL = getattr(settings, "AUTH_USER_MODEL", "auth.User") -@python_2_unicode_compatible class Referral(models.Model): user = models.ForeignKey( @@ -39,8 +35,7 @@ class Referral(models.Model): def __str__(self): if self.user: - return "{} ({})".format(self.user, self.code) - else: + return f"{self.user} ({self.code})" else: return self.code @classmethod @@ -58,7 +53,7 @@ def url(self): path = reverse("pinax_referrals:process_referral", kwargs={"code": self.code}) domain = Site.objects.get_current().domain protocol = "https" if settings.PINAX_REFERRALS_SECURE_URLS else "http" - return "{}://{}{}".format(protocol, domain, path) + return f"{protocol}://{domain}{path}" @property def response_count(self): @@ -67,8 +62,7 @@ def response_count(self): def save(self, *args, **kwargs): if not self.code: self.code = settings.PINAX_REFERRALS_CODE_GENERATOR_CALLBACK(Referral) - return super(Referral, self).save(*args, **kwargs) - + return super().save(*args, **kwargs) @classmethod def create(cls, redirect_to, user=None, label="", target=None): if target: From 253561ce8081f70c971266a40012ab0917147376 Mon Sep 17 00:00:00 2001 From: KatherineMichel Date: Sat, 27 Jun 2020 19:11:00 -0500 Subject: [PATCH 6/9] Fix else statement --- pinax/referrals/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pinax/referrals/models.py b/pinax/referrals/models.py index 08ea25e..0f13aca 100644 --- a/pinax/referrals/models.py +++ b/pinax/referrals/models.py @@ -35,7 +35,8 @@ class Referral(models.Model): def __str__(self): if self.user: - return f"{self.user} ({self.code})" else: + return f"{self.user} ({self.code})" + else: return self.code @classmethod From 61c2c2b83809cfe73a49c05f05a8c5e02c8fe6ce Mon Sep 17 00:00:00 2001 From: KatherineMichel Date: Sat, 27 Jun 2020 19:28:22 -0500 Subject: [PATCH 7/9] Fixing linting --- pinax/referrals/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pinax/referrals/models.py b/pinax/referrals/models.py index 0f13aca..ce28116 100644 --- a/pinax/referrals/models.py +++ b/pinax/referrals/models.py @@ -35,7 +35,7 @@ class Referral(models.Model): def __str__(self): if self.user: - return f"{self.user} ({self.code})" + return f"{self.user} ({self.code})" else: return self.code From 20e84d7cd4dffe8e057ac9c4565c21656a06f012 Mon Sep 17 00:00:00 2001 From: KatherineMichel Date: Sat, 27 Jun 2020 19:36:11 -0500 Subject: [PATCH 8/9] Fix quotes --- pinax/referrals/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pinax/referrals/admin.py b/pinax/referrals/admin.py index e9ae260..c690545 100644 --- a/pinax/referrals/admin.py +++ b/pinax/referrals/admin.py @@ -49,5 +49,5 @@ class ReferralResponseAdmin(admin.ModelAdmin): 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,)) + 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__()) From c5c4e71d412425c6b0e8aa14f1c69966de526c57 Mon Sep 17 00:00:00 2001 From: KatherineMichel Date: Tue, 21 Jul 2020 17:37:53 -0500 Subject: [PATCH 9/9] Removing whitespace --- pinax/referrals/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pinax/referrals/models.py b/pinax/referrals/models.py index 8b54bf7..6ea0332 100644 --- a/pinax/referrals/models.py +++ b/pinax/referrals/models.py @@ -64,7 +64,7 @@ def save(self, *args, **kwargs): if not self.code: self.code = settings.PINAX_REFERRALS_CODE_GENERATOR_CALLBACK(Referral) return super().save(*args, **kwargs) - + @classmethod def create(cls, redirect_to, user=None, label="", target=None): if target: