Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 132 lines (106 sloc) 4.229 kB
5988965 @paltman initial commit
paltman authored
1 import datetime
2
3 from django.db import models
86bc8ce @paltman Move url definition to model
paltman authored
4 from django.core.urlresolvers import reverse
5988965 @paltman initial commit
paltman authored
5
6 from django.contrib.auth.models import User
7 from django.contrib.contenttypes.models import ContentType
8 from django.contrib.contenttypes import generic
86bc8ce @paltman Move url definition to model
paltman authored
9 from django.contrib.sites.models import Site
5988965 @paltman initial commit
paltman authored
10
d0e53f6 @paltman Add ability to filter responses
paltman authored
11 from anafero.conf import settings
5988965 @paltman initial commit
paltman authored
12
13
14 class Referral(models.Model):
15
69c5d02 @paltman Make user nullable on Referral
paltman authored
16 user = models.ForeignKey(User, related_name="referral_codes", null=True)
d510677 @paltman Add optional label to referral
paltman authored
17 label = models.CharField(max_length=100, blank=True)
5988965 @paltman initial commit
paltman authored
18 code = models.CharField(max_length=40, unique=True)
19 redirect_to = models.CharField(max_length=512)
20 target_content_type = models.ForeignKey(ContentType, null=True)
21 target_object_id = models.PositiveIntegerField(null=True)
22 target = generic.GenericForeignKey(
23 ct_field="target_content_type",
24 fk_field="target_object_id"
25 )
26
27 created_at = models.DateTimeField(default=datetime.datetime.now)
28
4234783 @paltman Make admin a bit more usable
paltman authored
29 def __unicode__(self):
30 if self.user:
31 return u"%s (%s)" % (self.user, self.code)
32 else:
33 return self.code
34
819e796 @paltman Add ability to get a referral object from a request
paltman authored
35 @classmethod
36 def for_request(cls, request):
37 cookie = request.COOKIES.get("anafero-referral")
9de5012 @paltman Handle case when cookie isn't present
paltman authored
38 if cookie:
39 code, session_key = cookie.split(":")
40 try:
41 return Referral.objects.get(code=code)
42 except Referral.DoesNotExist:
43 pass
819e796 @paltman Add ability to get a referral object from a request
paltman authored
44
71a468e @paltman Add a response count property
paltman authored
45 @property
86bc8ce @paltman Move url definition to model
paltman authored
46 def url(self):
47 path = reverse("anafero_process_referral", kwargs={"code": self.code})
48 domain = Site.objects.get_current().domain
d0e53f6 @paltman Add ability to filter responses
paltman authored
49 protocol = "https" if settings.ANAFERO_SECURE_URLS else "http"
86bc8ce @paltman Move url definition to model
paltman authored
50 return "%s://%s%s" % (protocol, domain, path)
51
52 @property
71a468e @paltman Add a response count property
paltman authored
53 def response_count(self):
54 return self.responses.filter(action="RESPONDED").count()
55
5988965 @paltman initial commit
paltman authored
56 @classmethod
d510677 @paltman Add optional label to referral
paltman authored
57 def create(cls, redirect_to, user=None, label="", target=None):
d0e53f6 @paltman Add ability to filter responses
paltman authored
58 code = settings.ANAFERO_CODE_GENERATOR_CALLBACK(cls)
5988965 @paltman initial commit
paltman authored
59
60 if target:
c95b717 @paltman Correct a bunch of silly mistakes
paltman authored
61 obj, _ = cls.objects.get_or_create(
5988965 @paltman initial commit
paltman authored
62 user=user,
63 code=code,
64 redirect_to=redirect_to,
d510677 @paltman Add optional label to referral
paltman authored
65 label=label,
5988965 @paltman initial commit
paltman authored
66 target_content_type=ContentType.objects.get_for_model(target),
67 target_object_id=target.pk
68 )
6eea46f @paltman Whitespace nits
paltman authored
69 else:
c95b717 @paltman Correct a bunch of silly mistakes
paltman authored
70 obj, _ = cls.objects.get_or_create(
5988965 @paltman initial commit
paltman authored
71 user=user,
72 code=code,
d510677 @paltman Add optional label to referral
paltman authored
73 label=label,
5988965 @paltman initial commit
paltman authored
74 redirect_to=redirect_to,
75 )
76
77 return obj
78
8388348 @paltman Add utility class method for recording other actions
paltman authored
79 @classmethod
80 def record_response(cls, request, action_string):
059f3da @paltman Add a new class method for getting a referral object
paltman authored
81 referral = cls.referral_for_request(request)
82 if referral:
83 return referral.respond(request, action_string)
84
85 @classmethod
86 def referral_for_request(cls, request):
c95b717 @paltman Correct a bunch of silly mistakes
paltman authored
87 if request.user.is_authenticated():
8388348 @paltman Add utility class method for recording other actions
paltman authored
88 qs = ReferralResponse.objects.filter(user=request.user)
89 else:
90 qs = ReferralResponse.objects.filter(session_key=request.session.session_key)
91
92 try:
059f3da @paltman Add a new class method for getting a referral object
paltman authored
93 return qs.order_by("-created_at")[0].referral
8388348 @paltman Add utility class method for recording other actions
paltman authored
94 except IndexError:
95 pass
96
92d9c4f @paltman Add ability to pass in a user
paltman authored
97 def respond(self, request, action_string, user=None):
98 if user is None:
99 if request.user.is_authenticated():
100 user = request.user
101 else:
102 user = None
5988965 @paltman initial commit
paltman authored
103
d0e53f6 @paltman Add ability to filter responses
paltman authored
104 ip_address = request.META.get(
105 settings.ANAFERO_IP_ADDRESS_META_FIELD,
106 ""
107 )
5988965 @paltman initial commit
paltman authored
108
109 return ReferralResponse.objects.create(
110 referral=self,
111 session_key=request.session.session_key,
112 ip_address=ip_address,
113 action=action_string,
114 user=user
115 )
d0e53f6 @paltman Add ability to filter responses
paltman authored
116
117 def filtered_responses(self):
118 return settings.ANAFERO_RESPONSES_FILTER_CALLBACK(
119 referral=self
120 )
5988965 @paltman initial commit
paltman authored
121
122
123 class ReferralResponse(models.Model):
124
8388348 @paltman Add utility class method for recording other actions
paltman authored
125 referral = models.ForeignKey(Referral, related_name="responses")
5988965 @paltman initial commit
paltman authored
126 session_key = models.CharField(max_length=40)
127 user = models.ForeignKey(User, null=True)
128 ip_address = models.CharField(max_length=45)
129 action = models.CharField(max_length=128)
130
131 created_at = models.DateTimeField(default=datetime.datetime.now)
Something went wrong with that request. Please try again.