# Path: schedule/utils.py
# class EventListManager(object):
#     """
#     This class is responsible for doing functions on a list of events. It is
#     used to when one has a list of events and wants to access the occurrences
#     from these events in as a group
#     """
# 
#     def __init__(self, events):
#         self.events = events
# 
#     def occurrences_after(self, after=None, tzinfo=pytz.utc):
#         """
#         It is often useful to know what the next occurrence is given a list of
#         events.  This function produces a generator that yields the
#         the most recent occurrence after the date ``after`` from any of the
#         events in ``self.events``
#         """
#         from schedule.models import Occurrence
# 
#         if after is None:
#             after = timezone.now()
#         occ_replacer = OccurrenceReplacer(
#             Occurrence.objects.filter(event__in=self.events))
#         generators = [event._occurrences_after_generator(after) for event in self.events]
#         occurrences = []
# 
#         for generator in generators:
#             try:
#                 heapq.heappush(occurrences, (next(generator), generator))
#             except StopIteration:
#                 pass
# 
#         while True:
#             if len(occurrences) == 0:
#                 raise StopIteration
# 
#             generator = occurrences[0][1]
# 
#             try:
#                 next_occurence = heapq.heapreplace(occurrences, (next(generator), generator))[0]
#             except StopIteration:
#                 next_occurence = heapq.heappop(occurrences)[0]
#             yield occ_replacer.get_occurrence(next_occurence)
# 
# def get_model_bases():
#     from django.db.models import Model
#     baseStrings = getattr(settings, 'SCHEDULER_BASE_CLASSES', None)
#     if baseStrings is None:
#         return [Model]
#     else:
#         return [import_string(x) for x in baseStrings]


from django.utils.six.moves.builtins import str
from django.utils.six import with_metaclass
from django.contrib.contenttypes import fields
from django.db import models
from django.db.models.base import ModelBase
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import slugify
from schedule.utils import EventListManager, get_model_bases
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
import pytz
import datetime
        datetime.datetime.now
        """
        return self.events.order_by('-start').filter(start__lt=timezone.now())[:amount]

    def occurrences_after(self, date=None):
        return EventListManager(self.events.all()).occurrences_after(date)

    def get_absolute_url(self):
        return reverse('calendar_home', kwargs={'calendar_slug': self.slug})

    def add_event_url(self):
        return reverse('calendar_create_event', args=[self.slug])


class CalendarRelationManager(models.Manager):
    def create_relation(self, calendar, content_object, distinction=None, inheritable=True):
        """
        Creates a relation between calendar and content_object.
        See CalendarRelation for help on distinction and inheritable
        """
        cr = CalendarRelation(
            calendar=calendar,
            distinction=distinction,
            content_object=content_object
        )
        cr.save()
        return cr


@python_2_unicode_compatible

# Path: schedule/utils.py
# class EventListManager(object):
#     """
#     This class is responsible for doing functions on a list of events. It is
#     used to when one has a list of events and wants to access the occurrences
#     from these events in as a group
#     """
# 
#     def __init__(self, events):
#         self.events = events
# 
#     def occurrences_after(self, after=None, tzinfo=pytz.utc):
#         """
#         It is often useful to know what the next occurrence is given a list of
#         events.  This function produces a generator that yields the
#         the most recent occurrence after the date ``after`` from any of the
#         events in ``self.events``
#         """
#         from schedule.models import Occurrence
# 
#         if after is None:
#             after = timezone.now()
#         occ_replacer = OccurrenceReplacer(
#             Occurrence.objects.filter(event__in=self.events))
#         generators = [event._occurrences_after_generator(after) for event in self.events]
#         occurrences = []
# 
#         for generator in generators:
#             try:
#                 heapq.heappush(occurrences, (next(generator), generator))
#             except StopIteration:
#                 pass
# 
#         while True:
#             if len(occurrences) == 0:
#                 raise StopIteration
# 
#             generator = occurrences[0][1]
# 
#             try:
#                 next_occurence = heapq.heapreplace(occurrences, (next(generator), generator))[0]
#             except StopIteration:
#                 next_occurence = heapq.heappop(occurrences)[0]
#             yield occ_replacer.get_occurrence(next_occurence)
# 
# def get_model_bases():
#     from django.db.models import Model
#     baseStrings = getattr(settings, 'SCHEDULER_BASE_CLASSES', None)
#     if baseStrings is None:
#         return [Model]
#     else:
#         return [import_string(x) for x in baseStrings]


# Path: schedule/models/calendars.py
from django.utils.six.moves.builtins import str
from django.utils.six import with_metaclass
from django.contrib.contenttypes import fields
from django.db import models
from django.db.models.base import ModelBase
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import slugify
from schedule.utils import EventListManager, get_model_bases
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
import pytz
import datetime

        datetime.datetime.now
        """
        return self.events.order_by('-start').filter(start__lt=timezone.now())[:amount]

    def occurrences_after(self, date=None):
        return EventListManager(self.events.all()).occurrences_after(date)

    def get_absolute_url(self):
        return reverse('calendar_home', kwargs={'calendar_slug': self.slug})

    def add_event_url(self):
        return reverse('calendar_create_event', args=[self.slug])


class CalendarRelationManager(models.Manager):
    def create_relation(self, calendar, content_object, distinction=None, inheritable=True):
        """
        Creates a relation between calendar and content_object.
        See CalendarRelation for help on distinction and inheritable
        """
        cr = CalendarRelation(
            calendar=calendar,
            distinction=distinction,
            content_object=content_object
        )
        cr.save()
        return cr


@python_2_unicode_compatible

class CalendarRelation(with_metaclass(ModelBase, *get_model_bases())):