# 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
        "Jeremy's Calendar"
        """
        try:
            return self.get_calendar_for_object(obj, distinction)
        except Calendar.DoesNotExist:
            if name is None:
                calendar = Calendar(name=str(obj))
            else:
                calendar = Calendar(name=name)
            calendar.slug = slugify(calendar.name)
            calendar.save()
            calendar.create_relation(obj, distinction)
            return calendar

    def get_calendars_for_object(self, obj, distinction=None):
        """
        This function allows you to get calendars for a specific object

        If distinction is set it will filter out any relation that doesnt have
        that distinction.
        """
        ct = ContentType.objects.get_for_model(obj)
        if distinction:
            dist_q = Q(calendarrelation__distinction=distinction)
        else:
            dist_q = Q()
        return self.filter(dist_q, calendarrelation__object_id=obj.id, calendarrelation__content_type=ct)


@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

        "Jeremy's Calendar"
        """
        try:
            return self.get_calendar_for_object(obj, distinction)
        except Calendar.DoesNotExist:
            if name is None:
                calendar = Calendar(name=str(obj))
            else:
                calendar = Calendar(name=name)
            calendar.slug = slugify(calendar.name)
            calendar.save()
            calendar.create_relation(obj, distinction)
            return calendar

    def get_calendars_for_object(self, obj, distinction=None):
        """
        This function allows you to get calendars for a specific object

        If distinction is set it will filter out any relation that doesnt have
        that distinction.
        """
        ct = ContentType.objects.get_for_model(obj)
        if distinction:
            dist_q = Q(calendarrelation__distinction=distinction)
        else:
            dist_q = Q()
        return self.filter(dist_q, calendarrelation__object_id=obj.id, calendarrelation__content_type=ct)


@python_2_unicode_compatible

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