# -*- coding: utf-8 -*-

import calendar
import datetime
from collections import OrderedDict

from django.utils import timezone

from .models import Category, Event


class EventsMixin(object):
    def get_context_data(self, **kwargs):
        context = super(EventsMixin, self).get_context_data(**kwargs)
        context['events_categories'] = True
        context['categories'] = Category.objects.all()
        return context


class CategoryMixin(object):
    def get_context_data(self, **kwargs):
        context = super(CategoryMixin, self).get_context_data(**kwargs)
        context['current_category'] = self.category
        return context


class EventsQuerysetMixin(object):
    model = Event
    queryset = Event.objects.published()


class CalendarMixin(EventsQuerysetMixin):
    allow_future = True
    allow_empty = True
    month_format = '%m'
    date_field = 'date_url'

    def get_time_now(self):
        return timezone.now()

    def get_current_month(self):
        return datetime.date(year=int(self.get_year()), month=int(self.get_month()), day=1)

    def get_context_data(self, **kwargs):
        context = super(CalendarMixin, self).get_context_data(**kwargs)

        now = self.get_time_now()
        current_month = self.get_current_month()
        previous_month = self.get_previous_month(current_month)
        next_month = self.get_next_month(current_month)

        context['calendar_headings'] = self.get_calendar_day_names()
        context['event_list'] = self.get_events_list()
        context['now_month'] = now
        context['previous_month'] = previous_month
        context['next_month'] = next_month
        context['current_month'] = current_month

        return context

    def get_events_list(self):
        current_month = self.get_current_month()
        next_month = self.get_next_month(current_month)

        month_days = OrderedDict()

        cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
        for week in cal.monthdatescalendar(current_month.year, current_month.month):
            for i in week:
                month_days[i] = []

        # Get queryset from Mixin
        qs = self.get_queryset()

        qs = qs.filter(start__gte=current_month, start__lte=next_month)

        for i in qs:
            event_date = i.start.date()
            month_days[event_date].append(i)

        return month_days.items()

    def get_calendar_day_names(self):
        calendar_days = []
        day_names = list(calendar.day_name)
        cal = calendar.Calendar(firstweekday=calendar.SUNDAY)

        for i in cal.iterweekdays():
            calendar_days.append(day_names[i])

        return calendar_days

@@ -1185,212 +1185,8 @@
 s)%0A%0A
-        now = self.get_time_now()%0A        current_month = self.get_current_month()%0A        previous_month = self.get_previous_month(current_month)%0A        next_month = self.get_next_month(current_month)%0A%0A
     
@@ -1271,18 +1271,23 @@
 xt%5B'
-event_list
+calendar_events
 '%5D =
@@ -1313,186 +1313,8 @@
 st()
-%0A        context%5B'now_month'%5D = now%0A        context%5B'previous_month'%5D = previous_month%0A        context%5B'next_month'%5D = next_month%0A        context%5B'current_month'%5D = current_month
 %0A%0A  
@@ -1419,65 +1419,8 @@
 h()%0A
-        next_month = self.get_next_month(current_month)%0A%0A
     
@@ -1673,160 +1673,35 @@
     
-# Get queryset from Mixin%0A        qs = self.get_queryset()%0A%0A        qs = qs.filter(start__gte=current_month, start__lte=next_month)%0A%0A        for i in qs
+for obj in self.object_list
 :%0A  
@@ -1723,17 +1723,19 @@
 _date = 
-i
+obj
 .start.d
@@ -1782,17 +1782,19 @@
 .append(
-i
+obj
 )%0A%0A     

Reduce the amount of context needed