Skip to content

Commit

Permalink
Merge pull request #51 from PetrDlouhy/overridability
Browse files Browse the repository at this point in the history
Increase overridability
  • Loading branch information
KatherineMichel authored Jul 21, 2020
2 parents 8955dbb + c5c4e71 commit a9aa3bb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
57 changes: 39 additions & 18 deletions pinax/referrals/admin.py
Original file line number Diff line number Diff line change
@@ -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('<a href="{}">{}</a>', admin_link, obj.target.__str__())
20 changes: 13 additions & 7 deletions pinax/referrals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a9aa3bb

Please # to comment.