""" tags to help with showing counter type stuff in templates """ __revision__ = "$Rev$" __date__ = "$Date$" from django import template from django.conf import settings from django.core.cache import cache from zilbo.common.utils.misc import get_content_type_id, get_object, get_content_type_id_by_modelname from zilbo.common.counter.models import ObjectCounter, ObjectDateCounter, ObjectHourCounter, ObjectDateUserCounter import datetime register = template.Library() @register.inclusion_tag('counter/top_objects.html') def top_objects(hours=24, url=None, RSS=None): objs = cache.get( 'top_objects_%s_%d' % ( settings.SITE_ID, hours )) c_ids=[] try: models= settings.POPULAR_MODELS if models is not None: for model in models.split(","): c_ids.append( get_content_type_id_by_modelname(model)) except: pass if objs is None: objs=[] u = ObjectHourCounter.most_popular( hours, content_type_id = c_ids ) try: ids = u['rows'] except: ids = [] for i in ids: try: obj = get_object( i[0], i[1] ) objs.append( {'url': obj.get_absolute_url(), 'name': str( obj ) }) except: pass cache.set( 'top_objects_%s_%d' % (settings.SITE_ID, hours ), objs, 1800 ) top_objects = { 'topobjects': objs , 'topurl': url, 'toprss':RSS, } return top_objects class popular_objects_class(template.Node): """ return a object reference to a given object """ def __init__(self, obj_type, var, limit ): self.obj_type= obj_type self.var= var self.limit = limit def render(self, context): if self.obj_type.lower() == 'all': c_id=[] try: models= settings.POPULAR_MODELS if models is not None: for model in models.split(","): c_id.append( get_content_type_id_by_modelname(model)) except: pass u = ObjectHourCounter.most_popular( 24, limit=self.limit, content_type_id = c_id ) else: c_id = [get_content_type_id_by_modelname(z) for z in self.obj_type.split(",")] u = ObjectHourCounter.most_popular(24, limit=self.limit, content_type_id= c_id ) list = [] try: ids = u["rows"] except: pass for i in ids: try: list.append( ( get_object( i[0], i[1]), i[2] )) except: pass context[self.var] = list return "" def popular_objects(parser, token): """ {% popular_objects type|ALL [as] xyz limit%} """ bits = token.contents.split() tok="" if len(bits) > 5: raise template.TemplateSyntaxError, "'popular_objects' statements must be 'popular_objects type|ALL as limit': %s" % token.contents if len(bits) < 4: raise template.TemplateSyntaxError, "'popular_objects' statements must be 'popular_objects type|ALL as limit': %s" % token.contents if len(bits) == 4: obj_type = bits[1] tok = bits[2] max = bits[3] else: obj_type = bits[1] tok = bits[3] max = bits[4] return popular_objects_class(obj_type, tok,max) register.tag('popular_objects', popular_objects) def object_views(object): views=0 ct = get_content_type_id ( object ) for x in ObjectCounter.objects.filter(content_type__id__exact = ct, object_id__exact = object.id ): views += x.unique_views return str(views) def object_hour_views(object, arg=None): if arg is None: arg = 24 now = datetime.datetime.now() past = now - datetime.timedelta(hours=arg) views=0 ct = get_content_type_id ( object ) for x in ObjectHourCounter.objects.filter( effective_date__gte = past, content_type__id__exact = ct, object_id__exact = object.id ): views += x.unique_views return str(views) def object_date_views(object, arg=None): if arg is None: arg = 7 ct = get_content_type_id ( object ) now = datetime.datetime.now() past = now - datetime.timedelta(days=arg) views=0 for x in ObjectDateCounter.objects.filter( effective_date__gte = past, content_type__id__exact = ct, object_id__exact = object.id ): views += x.unique_views return str(views) register.filter(object_date_views) register.filter(object_hour_views) register.filter(object_views) class last_viewed_class(template.Node): """ return a object reference to a given object """ def __init__(self, obj_type, var, limit=5 ): self.obj_type = obj_type self.var = var self.limit = limit def render(self, context): if context.has_key('user'): user = context['user'] if user.is_anonymous(): odc = ObjectDateUserCounter.objects.filter( session_key = context['session_key'] ) else: odc = ObjectDateUserCounter.objects.filter( user__id = user.id) else: odc = ObjectDateUserCounter.objects.filter( session_key = context['session_key'] ) c_id = [] if self.obj_type.lower() == 'all': try: models= settings.POPULAR_MODELS if models is not None: for model in models.split(","): c_id.append( get_content_type_id_by_modelname(model)) except: pass else: c_id = [get_content_type_id_by_modelname(z) for z in self.obj_type.split(",")] if len(c_id) >0: odc = odc.filter( content_type__id__in = c_id) list = [] inlist={} for i in odc.order_by('-effective_date')[:self.limit]: key = "%s_%s" % (i.content_type_id, i.object_id) if not inlist.has_key(key): try: inlist[key] = 1 list.append( (i.effective_date, get_object( i.content_type_id, i.object_id))) except : pass print list context[self.var] = list return "" def last_viewed(parser, token): """ {% last_viewed type|ALL [as] xyz limit%} """ bits = token.contents.split() tok="" if len(bits) > 5: raise template.TemplateSyntaxError, "'last_viewed' statements must be 'last_viewed type|ALL as limit': %s" % token.contents if len(bits) < 4: raise template.TemplateSyntaxError, "'last_viewed' statements must be 'last_viewed type|ALL as limit': %s" % token.contents if len(bits) == 4: obj_type = bits[1] tok = bits[2] max = bits[3] else: obj_type = bits[1] tok = bits[3] max = bits[4] return last_viewed_class(obj_type, tok,max) register.tag('last_viewed', last_viewed)