def test_caching(self):
        opts, object_id = self.object._meta, self.object.pk
        job = HitCountJob()

        # first hit...
        with self.assertNumQueries(5):
            hit_count = job.get(opts.app_label, opts.model_name, object_id)

        self.assertEqual(hit_count.hits, 0)  # first run, nothing yet

        # second hit, will return cached result...
        with self.assertNumQueries(0):  # nothing in background
            hit_count = job.get(opts.app_label, opts.model_name, object_id)

        self.assertEqual(hit_count.hits, 0)  # returns cached result
    def test_refresh_interval(self):
        # overriding default settings...
        with self.settings(HITCOUNT_REFRESH_INTERVAL=0):
            opts, object_id = self.object._meta, self.object.pk
            job = HitCountJob()

            # first hit...
            with self.assertNumQueries(5):
                hit_count = job.get(opts.app_label, opts.model_name, object_id)

            self.assertEqual(hit_count.hits, 0)  # first run, nothing yet

            # second hit, will return stale result, but starts async
            # refreshing...
            with self.assertNumQueries(2):  # aha, we do make queries
                hit_count = job.get(opts.app_label, opts.model_name, object_id)

            self.assertEqual(hit_count.hits, 0)  # returns cached result
Beispiel #3
0
    def get_context_data(self, **kwargs):
        context = super(PopularityMixin, self).get_context_data(**kwargs)

        if hasattr(self, 'object') and isinstance(self.object, Model): # hey we got a model instance
            from popularity.tasks import HitCountJob

            opts, pk = self.object._meta, self.object.pk
            hit_count = HitCountJob().get(opts.app_label, opts.model_name, pk)

            context['hitcount'] = {'pk': hit_count.pk, 'total_hits': hit_count.hits}

        return context
class HitCountJobTest(TestCase):

    def setUp(self):
        self.object = mommy.make('flatpages.FlatPage')
        self.job = HitCountJob()

    def tearDown(self):
        cache.clear()

    def test_caching(self):
        opts, object_id = self.object._meta, self.object.pk

        # first hit...
        with self.assertNumQueries(2):
            hits = self.job.get(opts.app_label, opts.module_name, object_id)

        self.assertEqual(hits['total'], 0)

        # second hit...
        with self.assertNumQueries(0):
            hits = self.job.get(opts.app_label, opts.module_name, object_id)

        self.assertEqual(hits['total'], 0)  # returns cached result
Beispiel #5
0
def get_hit_count_from_obj_variable(context, obj_variable, tag_name):
    """
    Helper function to return a HitCount for a given template object variable.
    Raises TemplateSyntaxError if the passed object variable cannot be parsed.
    """
    from popularity.tasks import HitCountJob

    error_to_raise = template.TemplateSyntaxError(
        "'%(a)s' requires a valid individual model variable "
        "in the form of '%(a)s for [model_obj]'.\n"
        "Got: %(b)s" % {'a': tag_name, 'b': obj_variable}
    )

    try:
        obj = obj_variable.resolve(context)
    except template.VariableDoesNotExist:
        raise error_to_raise
    
    opts, pk = obj._meta, obj.pk

    hit_count = HitCountJob().get(opts.app_label, opts.model_name, pk)

    return hit_count
 def setUp(self):
     self.object = mommy.make('flatpages.FlatPage')
     self.job = HitCountJob()