# -*- coding: utf-8 -*- from django.contrib.contenttypes.models import ContentType from .models import SectionStats, ObjectStats class SectionBaseMixin(object): """ Section stat base mixin """ section = None stat_kind = None def render_to_response(self, context, **response_kwargs): try: SectionStats.cache_count_add( self.section, self.stat_kind, None, None, 1) # ['section', 'kind', 'content_type', 'object_id', 'value'] except: pass return super(SectionBaseMixin, self).render_to_response(context, **response_kwargs) class ExpoSectionBaseMixin(object): section = 'expo' class ExpoSectionMixin(ExpoSectionBaseMixin, SectionBaseMixin): pass class ConfSectionBaseMixin(object): section = 'conf' class ConfSectionMixin(ConfSectionBaseMixin, SectionBaseMixin): pass class SectionKindBaseMixin(object): section = None stat_kind = None def render_to_response(self, context, **response_kwargs): if self.stat_kind is not None and self.stat_kind in self.kwargs: try: obj = self.kwargs.get(self.stat_kind) content_type = ContentType.objects.get_for_model(obj) SectionStats.cache_count_add( self.section, self.stat_kind, content_type.pk, obj.pk, 1) # ['section', 'kind', 'content_type', 'object_id', 'value'] except: pass # except Exception as e: # print(e) return super(SectionKindBaseMixin, self).render_to_response(context, **response_kwargs) class ExpoSectionKindMixin(ExpoSectionBaseMixin, SectionKindBaseMixin): pass class ConfSectionKindMixin(ConfSectionBaseMixin, SectionKindBaseMixin): pass class ObjectStatMixin(object): def render_to_response(self, context, **response_kwargs): try: obj = self.object if obj.__class__.__name__.lower() == 'conference': ObjectStats.cache_count_add(obj.pk, None, 1) else: ObjectStats.cache_count_add(None, obj.pk, 1) # ['conference_id', 'exposition_id', 'value'] except: pass return super(ObjectStatMixin, self).render_to_response(context, **response_kwargs)