Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ability to filter responses

Typical use case for this would be to filter out
staff from counting as referral responses (though
you maybe still likely want to record them).

This change prompted the refactoring of settings
to use AppConf.
  • Loading branch information...
commit d0e53f685cd64ed1aaf5b639f104946858a6caa9 1 parent f2ad476
@paltman paltman authored
View
21 anafero/callbacks.py
@@ -0,0 +1,21 @@
+import random
+
+
+def generate_code(referral_class):
+ def _generate_code():
+ t = "abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890"
+ return "".join([random.choice(t) for i in range(40)])
+ code = _generate_code()
+ while referral_class.objects.filter(code=code).exists():
+ code = _generate_code()
+ return code
+
+
+def filter_responses(user=None, referral=None):
+ from anafero.models import ReferralResponse
+ responses = ReferralResponse.objects.all()
+ if user:
+ responses = responses.filter(referral__user=user)
+ if referral:
+ responses = responses.filter(referral=referral)
+ return responses.order_by("-created_at")
View
20 anafero/conf.py
@@ -0,0 +1,20 @@
+from django.conf import settings
+
+from appconf import AppConf
+
+from anafero.utils import load_path_attr
+
+
+class AnaferoAppConf(AppConf):
+
+ IP_ADDRESS_META_FIELD = "HTTP_X_FORWARDED_FOR"
+ SECURE_URLS = False
+ ACTION_DISPLAY = {"RESPONDED": "Clicked on referral link"}
+ CODE_GENERATOR_CALLBACK = "anafero.callbacks.generate_code"
+ RESPONSES_FILTER_CALLBACK = "anafero.callbacks.filter_responses"
+
+ def configure_code_generator_callback(self, value):
+ return load_path_attr(value)
+
+ def configure_responses_filter_callback(self, value):
+ return load_path_attr(value)
View
32 anafero/models.py
@@ -1,8 +1,6 @@
import datetime
-import sys
from django.db import models
-from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
@@ -10,21 +8,7 @@
from django.contrib.contenttypes import generic
from django.contrib.sites.models import Site
-
-IP_ADDRESS_FIELD = getattr(settings, "ANAFERO_IP_ADDRESS_META_FIELD", "HTTP_X_FORWARDED_FOR")
-SECURE_URLS = getattr(settings, "ANAFERO_SECURE_URLS", False)
-CODE_GENERATOR = getattr(settings, "ANAFERO_CODE_GENERATOR", "anafero.utils.generate_code")
-ACTION_DISPLAY = getattr(settings, "ANAFERO_ACTION_DISPLAY", {"RESPONDED": "Clicked on referral link"})
-
-
-def import_obj(name):
- dot = name.rindex('.')
- mod_name, obj_name = name[:dot], name[dot + 1:]
- __import__(mod_name)
- return getattr(sys.modules[mod_name], obj_name)
-
-
-CODE_GENERATOR = import_obj(CODE_GENERATOR)
+from anafero.conf import settings
class Referral(models.Model):
@@ -62,7 +46,7 @@ def for_request(cls, request):
def url(self):
path = reverse("anafero_process_referral", kwargs={"code": self.code})
domain = Site.objects.get_current().domain
- protocol = "https" if SECURE_URLS else "http"
+ protocol = "https" if settings.ANAFERO_SECURE_URLS else "http"
return "%s://%s%s" % (protocol, domain, path)
@property
@@ -71,7 +55,7 @@ def response_count(self):
@classmethod
def create(cls, redirect_to, user=None, label="", target=None):
- code = CODE_GENERATOR(cls)
+ code = settings.ANAFERO_CODE_GENERATOR_CALLBACK(cls)
if target:
obj, _ = cls.objects.get_or_create(
@@ -117,7 +101,10 @@ def respond(self, request, action_string, user=None):
else:
user = None
- ip_address = request.META.get(IP_ADDRESS_FIELD, "")
+ ip_address = request.META.get(
+ settings.ANAFERO_IP_ADDRESS_META_FIELD,
+ ""
+ )
return ReferralResponse.objects.create(
referral=self,
@@ -126,6 +113,11 @@ def respond(self, request, action_string, user=None):
action=action_string,
user=user
)
+
+ def filtered_responses(self):
+ return settings.ANAFERO_RESPONSES_FILTER_CALLBACK(
+ referral=self
+ )
class ReferralResponse(models.Model):
View
9 anafero/templatetags/anafero_tags.py
@@ -2,7 +2,7 @@
from django.contrib.contenttypes.models import ContentType
-from anafero.models import ReferralResponse, ACTION_DISPLAY
+from anafero.conf import settings
register = template.Library()
@@ -29,8 +29,9 @@ def __init__(self, user_var, target_var):
def render(self, context):
user = self.user_var.resolve(context)
- qs = ReferralResponse.objects.filter(referral__user=user)
- qs = qs.order_by("-created_at")
+ qs = settings.ANAFERO_RESPONSES_FILTER_CALLBACK(
+ user=user
+ )
context[self.target_var] = qs
return ""
@@ -50,4 +51,4 @@ def referral_responses(parser, token):
@register.filter
def action_display(value):
- return ACTION_DISPLAY.get(value, value)
+ return settings.ANAFERO_ACTION_DISPLAY.get(value, value)
View
28 anafero/utils.py
@@ -1,15 +1,5 @@
-import random
-
-
-def generate_code(referral_class):
- def _generate_code():
- t = "abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890"
- return "".join([random.choice(t) for i in range(40)])
-
- code = _generate_code()
- while referral_class.objects.filter(code=code).exists():
- code = _generate_code()
- return code
+from django.core.exceptions import ImproperlyConfigured
+from django.utils import importlib
def ensure_session_key(request):
@@ -28,3 +18,17 @@ def ensure_session_key(request):
request.session.modified = True
key = request.session.session_key
return key
+
+
+def load_path_attr(path):
+ i = path.rfind(".")
+ module, attr = path[:i], path[i + 1:]
+ try:
+ mod = importlib.import_module(module)
+ except ImportError, e:
+ raise ImproperlyConfigured("Error importing %s: '%s'" % (module, e))
+ try:
+ attr = getattr(mod, attr)
+ except AttributeError:
+ raise ImproperlyConfigured("Module '%s' does not define a '%s'" % (module, attr))
+ return attr
View
3  setup.py
@@ -132,6 +132,9 @@ def find_package_data(
"Programming Language :: Python",
"Framework :: Django",
],
+ install_requires=[
+ "django-appconf==0.5"
+ ],
zip_safe=False
)
Please sign in to comment.
Something went wrong with that request. Please try again.