Example #1
0
def pricingtier_group_change_listener(action=None, reverse=None, instance=None, **kwargs):
    """Listens for changes of m2m relation between auth.User/Group and resets related threadlocals cached object"""
    if action in ('post_add', 'pre_remove', 'pre_clear'):
        if not reverse:
            modified_users_id = [instance.id]
        else:
            modified_users_id = [u.id for u in instance.user_set.all()]
            # After required Django version will be 1.3+, the previous line can be replaced by:
            #modified_users = kwargs['pk_set']
        for user_id in modified_users_id:
            key = 'TIER_%i' % user_id
            threadlocals.set_thread_variable(key, None)
Example #2
0
def pricingtier_group_change_listener(action=None, reverse=None, instance=None, **kwargs):
    """Listens for changes of m2m relation between auth.User/Group and resets related threadlocals cached object"""
    if action in ('post_add', 'pre_remove', 'pre_clear'):
        if not reverse:
            modified_users_id = [instance.id]
        else:
            modified_users_id = [u.id for u in instance.user_set.all()]
            # After required Django version will be 1.3+, the previous line can be replaced by:
            #modified_users = kwargs['pk_set']
        for user_id in modified_users_id:
            if user_id:
                key = 'TIER_%i' % user_id
                threadlocals.set_thread_variable(key, None)
Example #3
0
def _get_taxprocessor(request=None):
    taxprocessor = get_thread_variable('taxer', None)
    if not taxprocessor:
        if request:
            user = request.user
            if user.is_authenticated():
                user = user
            else:
                user = None
        else:
            user = get_current_user()

        taxprocessor = get_tax_processor(user=user)
        set_thread_variable('taxer', taxprocessor)

    return taxprocessor
Example #4
0
def _get_taxprocessor(request=None):
    taxprocessor = get_thread_variable("taxer", None)
    if not taxprocessor:
        if request:
            user = request.user
            if user.is_authenticated():
                user = user
            else:
                user = None
        else:
            user = get_current_user()

        taxprocessor = get_tax_processor(user=user)
        set_thread_variable("taxer", taxprocessor)

    return taxprocessor
Example #5
0
 def setUp(self):
     keyedcache.cache_delete()
     # remove users stored by previous requests into threadlocals
     set_thread_variable("request", None)
     tieruser = User.objects.create_user("timmy", "*****@*****.**", "12345")
     stduser = User.objects.create_user("tommy", "*****@*****.**", "12345")
     tieruser.save()
     stduser.save()
     self.tieruser = tieruser
     self.stduser = stduser
     tiergroup = Group(name="tiertest")
     tiergroup.save()
     tieruser.groups.add(tiergroup)
     tieruser.save()
     self.tier = PricingTier(group=tiergroup, title="Test Tier", discount_percent=Decimal("10.0"))
     self.tier.save()
Example #6
0
def _get_taxprocessor(request=None):
    if request:
        user = request.user
        if user.is_authenticated():
            user_id = user.id
        else:
            user = None
            user_id = "None"
    else:
        user = get_current_user()
        user_id = user and user.id
    thread_key = "taxer-%s" % user_id
    taxprocessor = get_thread_variable(thread_key, None)
    if not taxprocessor:
        taxprocessor = get_tax_processor(user=user)    
        set_thread_variable(thread_key, taxprocessor)
    return taxprocessor
Example #7
0
def _get_taxprocessor(request=None):
    if request:
        user = request.user
        if user.is_authenticated:
            user_id = user.id
        else:
            user = None
            user_id = "None"
    else:
        user = get_current_user()
        user_id = user and user.id
    thread_key = "taxer-%s" % user_id
    taxprocessor = get_thread_variable(thread_key, None)
    if not taxprocessor:
        taxprocessor = get_tax_processor(user=user)
        set_thread_variable(thread_key, taxprocessor)
    return taxprocessor
Example #8
0
    def by_user(self, user):
        """Get the pricing tiers for a user"""
        key = 'TIER_%i' % user.id
        current = threadlocals.get_thread_variable(key)

        if current is None:
            groups = user.groups.all()
            current = []
            if groups and not user.is_superuser and not user.is_staff:
                filterQ = Q()
                for group in groups:
                    filterQ = filterQ | Q(group=group)
                current = list(self.filter(filterQ))

            threadlocals.set_thread_variable(key, current)

        if not current:
            raise PricingTier.DoesNotExist

        return current
Example #9
0
 def setUp(self):
     keyedcache.cache_delete()
     # remove users stored by previous requests into threadlocals
     set_thread_variable('request', None)
     tieruser = User.objects.create_user('timmy', '*****@*****.**',
                                         '12345')
     stduser = User.objects.create_user('tommy', '*****@*****.**',
                                        '12345')
     tieruser.save()
     stduser.save()
     self.tieruser = tieruser
     self.stduser = stduser
     tiergroup = Group(name="tiertest")
     tiergroup.save()
     tieruser.groups.add(tiergroup)
     tieruser.save()
     self.tier = PricingTier(group=tiergroup,
                             title="Test Tier",
                             discount_percent=Decimal('10.0'))
     self.tier.save()
Example #10
0
    def by_user(self, user):
        """Get the pricing tiers for a user"""
        key = 'TIER_%i' % user.id
        current = threadlocals.get_thread_variable(key)

        if current is None:
            groups = user.groups.all()
            current = []
            if groups and not user.is_superuser and not user.is_staff:
                filterQ = Q()
                for group in groups:
                    filterQ = filterQ | Q(group=group)
                current = list(self.filter(filterQ))

            threadlocals.set_thread_variable(key, current)

        if not current:
            raise PricingTier.DoesNotExist

        return current
Example #11
0
    def by_user(self, user):
        """Get the pricing tiers for a user"""
        key = 'TIER_%i' % user.id
        current = threadlocals.get_thread_variable(key)

        if current is None:
            groups = user.groups.all()
            if groups:
                q = self.all()
                for group in groups:
                    q = q.filter(group=group)
                if q.count() > 0:
                    current = list(q)

            if current is None:
                current = "no"

            threadlocals.set_thread_variable(key, current)

        if current == "no":
            raise PricingTier.DoesNotExist

        return current
Example #12
0
    def by_user(self, user):
        """Get the pricing tiers for a user"""
        key = 'TIER_%i' % user.id
        current = threadlocals.get_thread_variable(key)
        
        if current is None:
            groups = user.groups.all()
            if groups:            
                q = self.all()
                for group in groups:
                    q = q.filter(group=group)
                if q.count() > 0:
                    current = list(q)
                    
            if current is None:
                current = "no"
                
            threadlocals.set_thread_variable(key, current)

        if current == "no":
            raise PricingTier.DoesNotExist
        
        return current
Example #13
0
def set_request_uid(sender, *args, **kwargs):
    """Puts a unique id into the thread"""
    tid = random.randrange(1,10000000)
    threadlocals.set_thread_variable('request_uid', tid)
Example #14
0
 def __call__(self, request):
     set_thread_variable('request', request)
     set_current_user(request.user)
     return self.get_response(request)
 def process_request(self, request):
     set_thread_variable('request', request)
     set_current_user(request.user)
Example #16
0
 def process_request(self, request):
     set_thread_variable('request', request)
     set_current_user(request.user)
 def process_response(self, request, response):
     set_thread_variable('request', None)
     set_current_user(None)
     return response