Commits

Anonymous committed db0b0f5

fixing logic after real project: seems that session key is changed upon login, workaround.

Comments (0)

Files changed (4)

 \.ropeproject/.*
+.*\.pyc
 from django.contrib import admin
-from affiliate.models import Visit, Affiliate
+from affiliate.models import Visit, Affiliate, AFFILIATE_KEY
 from django.core.urlresolvers import reverse
 
 class AffiliateOptions(admin.ModelAdmin):
     name.short_description = 'Affiliate Name'
 
     def query_string(self, obj):
-        return "?affiliate="+obj.slug
+        return "?{0}={1}".format(AFFILIATE_KEY, obj.slug)
     query_string.short_description = "Query String"
 
     def anon_visits(self, obj):

affiliate/middleware.py

 
 from django.http import HttpResponseRedirect
 
-from affiliate.models import Affiliate, Visit
-
-
-AFFILIATE_KEY = 'affiliate'
+from affiliate.models import Affiliate, Visit, AFFILIATE_KEY
 
 
 def read_affiliate(somedict):
+    affiliate = None
     if AFFILIATE_KEY in somedict:
         affiliate_slug = somedict[AFFILIATE_KEY]
         try:
             affiliate = Affiliate.objects.get(
                 slug=affiliate_slug)
         except Affiliate.DoesNotExist:
-            affiliate = None
-            logging.warn('Request from non-existing affiliate: {0}'.format(
+            logging.warn('Request from non-existing affiliate: "{0}"'.format(
                 affiliate_slug))
     return affiliate
 
     if request.user.is_authenticated():
         visit.user = request.user
     else:
-        request.session[AFFILIATE_KEY] = affiliate
+        request.session[AFFILIATE_KEY] = affiliate.slug
+        request.session[AFFILIATE_KEY+'_session_key'] = request.session.session_key
     visit.save()
 
 
     if not request.user.is_authenticated():
         return
 
+    visit = None
     try:
         this_session_visits = Visit.objects.filter(
-            session_key=request.session.session_key).order_by('-date')
+            session_key=request.session[AFFILIATE_KEY+'_session_key']
+            ).order_by('-date')
         if this_session_visits.count() > 0:
             visit = this_session_visits[0]
     except Visit.DoesNotExist:
-        visit = None
+        pass
 
     if visit \
        and not visit.user:
         visit.user = request.user
         visit.save()
         del request.session[AFFILIATE_KEY]
-
+        del request.session[AFFILIATE_KEY+'_session_key']
 
 
 class AffiliateMiddleware(object):
     """
 
     def process_request(self, request):
-
         affiliate_from_request = read_affiliate_from_request(request)
         affiliate_from_session = read_affiliate_from_session(request)
 
             q = request.GET.urlencode().replace(
                 'affiliate=' + request.GET[AFFILIATE_KEY],
                 '').replace('&&', '&').rstrip('&').lstrip('&')
+            redirect_to = request.path_info
+            if len(q) > 0:
+                redirect_to += "?" + q
+            return HttpResponseRedirect(redirect_to)
 
-            if len(q) > 0:
-                return HttpResponseRedirect(request.path_info + "?" + q)
 from django.db import models
 from django.contrib.auth.models import User
 
+AFFILIATE_KEY = 'affiliate'
+
 
 class Affiliate(models.Model):
     """
     slug = models.SlugField(unique=True, blank=True, null=True)
 
     def __unicode__(self):
-        return self.name + " ?affiliate=" + self.slug
+        return "<affiliate:{0}>".format(self.slug)
+
+    class Meta:
+        db_table = "affiliate"
 
 
 class Visit(models.Model):
     affiliate = models.ForeignKey(Affiliate, related_name="visit_set")
     user = models.ForeignKey(User, blank=True, null=True)
     date = models.DateTimeField(auto_now_add=True)
-    session_key = models.CharField(max_length=64, unique=True)
+    session_key = models.CharField(max_length=64)
 
     def __unicode__(self):
         if self.user:
 
     class Meta:
         ordering = ('date',)
+        db_table = "affiliate_visit"
 
     @classmethod
     def get_last_visit(cls, user):