Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expiring Referrals #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions anafero/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@


admin.site.register(Referral,
list_display = ["user", "code", "label", "redirect_to", "target_content_type", "target_object_id"],
list_display = ["user", "code", "label", "redirect_to", "target_content_type", "target_object_id", "expired_at"],
readonly_fields = ["code", "created_at"],
list_filter = ["target_content_type", "created_at"],
list_filter = ["target_content_type", "created_at", "expired_at"],
search_fields = ["user", "code"]
)

Expand Down
3 changes: 2 additions & 1 deletion anafero/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def process_request(self, request):

try:
referral = Referral.objects.get(code=code)
referral.link_responses_to_user(request.user, session_key)
if referral.is_active():
referral.link_responses_to_user(request.user, session_key)
except Referral.DoesNotExist:
pass

Expand Down
7 changes: 5 additions & 2 deletions anafero/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ class Referral(models.Model):

def __unicode__(self):
if self.user:
return u"%s (%s)" % (self.user, self.code)
return u"{0} ({1})".format(self.user, self.code)
else:
return self.code

def is_active(self):
return self.expired_at is None or self.expired_at > timezone.now()

@classmethod
def for_request(cls, request):
cookie = request.COOKIES.get("anafero-referral")
Expand All @@ -54,7 +57,7 @@ def url(self):
path = reverse("anafero_process_referral", kwargs={"code": self.code})
domain = Site.objects.get_current().domain
protocol = "https" if settings.ANAFERO_SECURE_URLS else "http"
return "%s://%s%s" % (protocol, domain, path)
return "{0}://{1}{2}".format(protocol, domain, path)

@property
def response_count(self):
Expand Down
7 changes: 5 additions & 2 deletions anafero/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json

from django.http import HttpResponse
from django.http import HttpResponse, Http404
from django.shortcuts import redirect, get_object_or_404
from django.utils import timezone
from django.template import RequestContext
from django.template.loader import render_to_string
from django.views.decorators.http import require_POST
Expand Down Expand Up @@ -48,13 +49,15 @@ def create_referral(request):

def process_referral(request, code):
referral = get_object_or_404(Referral, code=code)
if not referral.is_active():
raise Http404()
session_key = ensure_session_key(request)
referral.respond(request, "RESPONDED")
response = redirect(referral.redirect_to)
if request.user.is_anonymous():
response.set_cookie(
"anafero-referral",
"%s:%s" % (code, session_key)
"{0}:{1}".format(code, session_key)
)
else:
response.delete_cookie("anafero-referral")
Expand Down