# Path: wprevents/events/models.py
# class Event(models.Model):
#   objects = CustomManager()
# 
#   created = models.DateTimeField(auto_now_add=True)
#   modified = models.DateTimeField(auto_now=True)
#   # Temporary id used when bulk creating events in cal format
#   bulk_id = models.IntegerField(null=True)
# 
#   title = models.CharField(max_length=EVENT_TITLE_LENGTH)
#   slug = models.SlugField(max_length=EVENT_TITLE_LENGTH, blank=True)
#   description = models.TextField(blank=True)
#   details = models.TextField(blank=True)
# 
#   start = models.DateTimeField(default=timezone.now)
#   end = models.DateTimeField(default=timezone.now)
# 
#   local_start = models.DateTimeField(default=timezone.now)
#   local_end = models.DateTimeField(default=timezone.now)
# 
#   space = models.ForeignKey(Space, null=True, blank=True, related_name='events_hosted', on_delete=models.SET_NULL)
# 
#   areas = models.ManyToManyField(FunctionalArea, blank=True)
# 
#   recurrence = RecurrenceModelField(null=True)
# 
#   class Meta:
#     permissions = (
#       ('can_administrate_events', 'Can administrate'),
#     )
#     index_together = [
#       ["title", "start", "end"],
#     ]
# 
#   def __unicode__(self):
#     return '#%s %s' % (self.id, self.title)
# 
#   def save(self, *args, **kwargs):
#     if not self.slug:
#       self.define_slug()
# 
#     self.update_local_datetimes()
# 
#     super(Event, self).save(*args, **kwargs)
# 
#   def make_local_to_space(self, dt):
#     if not dt:
#       return None
#     if self.space is not None and self.space.timezone is not None:
#       tz = pytz.timezone(self.space.timezone)
#       if not is_aware(dt):
#         dt = make_aware(dt, pytz.utc)
#       return tz.normalize(dt.astimezone(tz)).replace(tzinfo=None)
#     else:
#       return dt
# 
#   def update_local_datetimes(self):
#     self.local_start = self.make_local_to_space(self.start)
#     self.local_end = self.make_local_to_space(self.end)
# 
#   def define_slug(self):
#     slug = text.slugify(self.title)
#     # Fix an issue with slugify('MozBird_MakerParty')
#     slug = slug.replace('_', '-')
# 
#     self.slug = slug[:EVENT_TITLE_LENGTH]
# 
#   def get_duplicate_candidates(self, q=''):
#     event_day = datetime.date(self.start)
#     day_after_event = datetime.date(self.end) + timedelta(days=1)
# 
#     duplicate_candidates = Event.objects.filter(start__gte=event_day).filter(end__lte=day_after_event).exclude(id=self.id).filter(title__icontains=q)
# 
#     return duplicate_candidates
# 
#   def get_instances(self, after=None, before=None, inc=True):
#     if not self.recurring:
#       return []
# 
#     duration = self.end - self.start
# 
#     if not after and not before:
#       dts = list(self.recurrence.occurrences())
#     else:
#       dts = self.recurrence.between(after, before, inc=inc)
# 
#     instances = []
#     first_start = None
#     for dt in dts:
#       e = Instance(event=self)
#       e.start = self.make_local_to_space(dt)
# 
#       if first_start is None:
#         first_start = e.start
#       else:
#         e.start = e.start.replace(hour=first_start.hour, minute=first_start.minute, second=first_start.second)
# 
#       e.end = e.start + duration
#       instances.append(e)
# 
#     return instances
# 
#   def to_instance(self):
#     if self.recurring:
#       raise Exception("Only non-recurring events can be converted to a single instance")
# 
#     return Instance(event=self, start=self.local_start, end=self.local_end)
# 
#   @property
#   def area_names(self):
#     return [area.name for area in self.areas.all()]
# 
#   @property
#   def recurring(self):
#     return self.recurrence is not None
# 
#   @property
#   def url(self):
#     return self.instances.all()[0].url
# 
#   @property
#   def redirect_url(self):
#     return reverse('event_redirect_url', kwargs={
#       'id': self.id
#     })
# 
#   @property
#   def local_start_date(self):
#     return self.local_start.strftime('%Y-%m-%d')
# 
#   @property
#   def local_end_date(self):
#     return self.local_end.strftime('%Y-%m-%d')
# 
#   @property
#   def local_start_time(self):
#     return self.local_start.strftime('%H:%M')
# 
#   @property
#   def local_end_time(self):
#     return self.local_end.strftime('%H:%M')


from django.test import TestCase
from .models import Event


class EventTestCase(TestCase):
  def setUp(self):
    Event.objects.create(
      title="Test event",
      start="2014-05-14T14:00:00",
      end="2014-05-14T16:00:00"
    )

  def test_title_is_slugified(self):
    """event.title should be slugified on save"""

# Path: wprevents/events/models.py
# class Event(models.Model):
#   objects = CustomManager()
# 
#   created = models.DateTimeField(auto_now_add=True)
#   modified = models.DateTimeField(auto_now=True)
#   # Temporary id used when bulk creating events in cal format
#   bulk_id = models.IntegerField(null=True)
# 
#   title = models.CharField(max_length=EVENT_TITLE_LENGTH)
#   slug = models.SlugField(max_length=EVENT_TITLE_LENGTH, blank=True)
#   description = models.TextField(blank=True)
#   details = models.TextField(blank=True)
# 
#   start = models.DateTimeField(default=timezone.now)
#   end = models.DateTimeField(default=timezone.now)
# 
#   local_start = models.DateTimeField(default=timezone.now)
#   local_end = models.DateTimeField(default=timezone.now)
# 
#   space = models.ForeignKey(Space, null=True, blank=True, related_name='events_hosted', on_delete=models.SET_NULL)
# 
#   areas = models.ManyToManyField(FunctionalArea, blank=True)
# 
#   recurrence = RecurrenceModelField(null=True)
# 
#   class Meta:
#     permissions = (
#       ('can_administrate_events', 'Can administrate'),
#     )
#     index_together = [
#       ["title", "start", "end"],
#     ]
# 
#   def __unicode__(self):
#     return '#%s %s' % (self.id, self.title)
# 
#   def save(self, *args, **kwargs):
#     if not self.slug:
#       self.define_slug()
# 
#     self.update_local_datetimes()
# 
#     super(Event, self).save(*args, **kwargs)
# 
#   def make_local_to_space(self, dt):
#     if not dt:
#       return None
#     if self.space is not None and self.space.timezone is not None:
#       tz = pytz.timezone(self.space.timezone)
#       if not is_aware(dt):
#         dt = make_aware(dt, pytz.utc)
#       return tz.normalize(dt.astimezone(tz)).replace(tzinfo=None)
#     else:
#       return dt
# 
#   def update_local_datetimes(self):
#     self.local_start = self.make_local_to_space(self.start)
#     self.local_end = self.make_local_to_space(self.end)
# 
#   def define_slug(self):
#     slug = text.slugify(self.title)
#     # Fix an issue with slugify('MozBird_MakerParty')
#     slug = slug.replace('_', '-')
# 
#     self.slug = slug[:EVENT_TITLE_LENGTH]
# 
#   def get_duplicate_candidates(self, q=''):
#     event_day = datetime.date(self.start)
#     day_after_event = datetime.date(self.end) + timedelta(days=1)
# 
#     duplicate_candidates = Event.objects.filter(start__gte=event_day).filter(end__lte=day_after_event).exclude(id=self.id).filter(title__icontains=q)
# 
#     return duplicate_candidates
# 
#   def get_instances(self, after=None, before=None, inc=True):
#     if not self.recurring:
#       return []
# 
#     duration = self.end - self.start
# 
#     if not after and not before:
#       dts = list(self.recurrence.occurrences())
#     else:
#       dts = self.recurrence.between(after, before, inc=inc)
# 
#     instances = []
#     first_start = None
#     for dt in dts:
#       e = Instance(event=self)
#       e.start = self.make_local_to_space(dt)
# 
#       if first_start is None:
#         first_start = e.start
#       else:
#         e.start = e.start.replace(hour=first_start.hour, minute=first_start.minute, second=first_start.second)
# 
#       e.end = e.start + duration
#       instances.append(e)
# 
#     return instances
# 
#   def to_instance(self):
#     if self.recurring:
#       raise Exception("Only non-recurring events can be converted to a single instance")
# 
#     return Instance(event=self, start=self.local_start, end=self.local_end)
# 
#   @property
#   def area_names(self):
#     return [area.name for area in self.areas.all()]
# 
#   @property
#   def recurring(self):
#     return self.recurrence is not None
# 
#   @property
#   def url(self):
#     return self.instances.all()[0].url
# 
#   @property
#   def redirect_url(self):
#     return reverse('event_redirect_url', kwargs={
#       'id': self.id
#     })
# 
#   @property
#   def local_start_date(self):
#     return self.local_start.strftime('%Y-%m-%d')
# 
#   @property
#   def local_end_date(self):
#     return self.local_end.strftime('%Y-%m-%d')
# 
#   @property
#   def local_start_time(self):
#     return self.local_start.strftime('%H:%M')
# 
#   @property
#   def local_end_time(self):
#     return self.local_end.strftime('%H:%M')


# Path: wprevents/events/tests.py
from django.test import TestCase
from .models import Event



class EventTestCase(TestCase):
  def setUp(self):
    Event.objects.create(
      title="Test event",
      start="2014-05-14T14:00:00",
      end="2014-05-14T16:00:00"
    )

  def test_title_is_slugified(self):
    """event.title should be slugified on save"""

    event = Event.objects.get(title="Test event")