Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

managers.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from datetime import timedelta
  4. from django.db import models
  5. from django.conf import settings
  6. from django.utils import timezone
  7. from django.contrib.contenttypes.models import ContentType
  8. class HitCountManager(models.Manager):
  9. def get_for_object(self, obj):
  10. ctype = ContentType.objects.get_for_model(obj)
  11. hit_count, created = self.get_or_create(
  12. content_type=ctype, object_pk=obj.pk)
  13. return hit_count
  14. class HitManager(models.Manager):
  15. def filter_active(self, *args, **kwargs):
  16. """
  17. Return only the 'active' hits.
  18. How you count a hit/view will depend on personal choice: Should the
  19. same user/visitor *ever* be counted twice? After a week, or a month,
  20. or a year, should their view be counted again?
  21. The defaulf is to consider a visitor's hit still 'active' if they
  22. return within a the last seven days.. After that the hit
  23. will be counted again. So if one person visits once a week for a year,
  24. they will add 52 hits to a given object.
  25. Change how long the expiration is by adding to settings.py:
  26. HITCOUNT_KEEP_HIT_ACTIVE = {'days' : 30, 'minutes' : 30}
  27. Accepts days, seconds, microseconds, milliseconds, minutes,
  28. hours, and weeks. It's creating a datetime.timedelta object.
  29. """
  30. grace = getattr(settings, 'HITCOUNT_KEEP_HIT_ACTIVE', {'days': 7})
  31. period = timezone.now() - timedelta(**grace)
  32. return self.filter(created__gte=period).filter(*args, **kwargs)