<s> from django . conf import settings <EOL> from django . contrib . contenttypes . fields import GenericForeignKey , GenericRelation <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import models <EOL> from django . utils import timezone <EOL> from videos . models import Video , Category <EOL> from . signals import page_view <EOL> class PageViewQuerySet ( models . query . QuerySet ) : <EOL> def videos ( self ) : <EOL> content_type = ContentType . objects . get_for_model ( Video ) <EOL> return self . filter ( primary_content_type = content_type ) <EOL> def categories ( self ) : <EOL> content_type = ContentType . objects . get_for_model ( Category ) <EOL> return self . filter ( primary_content_type = content_type ) <EOL> class PageViewManager ( models . Manager ) : <EOL> def get_queryset ( self ) : <EOL> return PageViewQuerySet ( self . model , using = self . _db ) <EOL> def get_videos ( self ) : <EOL> return self . get_queryset ( ) . videos ( ) <EOL> def get_categories ( self ) : <EOL> return self . get_queryset ( ) . categories ( ) <EOL> class PageView ( models . Model ) : <EOL> path = models . CharField ( max_length = <NUM_LIT> ) <EOL> user = models . ForeignKey ( settings . AUTH_USER_MODEL , null = True , blank = True ) <EOL> primary_content_type = models . ForeignKey ( ContentType , related_name = '<STR_LIT>' , null = True , blank = True ) <EOL> primary_object_id = models . PositiveIntegerField ( null = True , blank = True ) <EOL> primary_object = GenericForeignKey ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> secondary_content_type = models . ForeignKey ( ContentType , related_name = '<STR_LIT>' , null = True , blank = True ) <EOL> secondary_object_id = models . PositiveIntegerField ( null = True , blank = True ) <EOL> secondary_object = GenericForeignKey ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> timestamp = models . DateTimeField ( default = timezone . now ( ) ) <EOL> objects = PageViewManager ( ) <EOL> def __unicode__ ( self ) : <EOL> return self . path <EOL> class Meta : <EOL> ordering = [ '<STR_LIT>' ] <EOL> def page_view_received ( sender , ** kwargs ) : <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> page_path = kwargs . pop ( '<STR_LIT>' ) <EOL> primary_obj = kwargs . pop ( '<STR_LIT>' , None ) <EOL> secondary_obj = kwargs . pop ( '<STR_LIT>' , None ) <EOL> print secondary_obj <EOL> user = sender <EOL> if not user . is_authenticated ( ) : <EOL> new_page_view = PageView . objects . create ( path = page_path , timestamp = timezone . now ( ) ) <EOL> else : <EOL> new_page_view = PageView . objects . create ( path = page_path , user = user , timestamp = timezone . now ( ) ) <EOL> if primary_obj : <EOL> new_page_view . primary_object_id = primary_obj . id <EOL> new_page_view . primary_content_type = ContentType . objects . get_for_model ( primary_obj ) <EOL> new_page_view . save ( ) <EOL> if secondary_obj : <EOL> new_page_view . secondary_object_id = secondary_obj . id <EOL> new_page_view . secondary_content_type = ContentType . objects . get_for_model ( secondary_obj ) <EOL> new_page_view . save ( ) <EOL> page_view . connect ( page_view_received ) </s>
