from django import template

from fullcalendar.models import Occurrence

register = template.Library()

@register.inclusion_tag('events/agenda_tag.html')
def show_agenda(*args, **kwargs):
    qs = Occurrence.objects.upcoming()

    if 'limit' in kwargs:
        qs = qs[:int(kwargs['limit'])]

    return {
        'occurrences': qs,
        'all_sites': True,
    }

@register.assignment_tag
def get_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        return qs[:int(kwargs['limit'])]

    return qs

@register.inclusion_tag('events/agenda_tag.html')
def show_site_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        qs = qs[:int(kwargs['limit'])]

    return {
        'occurrences': qs
    }

@register.assignment_tag
def get_site_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        return qs[:int(kwargs['limit'])]

    return qs


from django import template
from django.utils import timezone

from fullcalendar.models import Occurrence

register = template.Library()

@register.inclusion_tag('events/agenda_tag.html')
def show_agenda(*args, **kwargs):
    qs = Occurrence.objects.upcoming()

    if 'limit' in kwargs:
        qs = qs[:int(kwargs['limit'])]

    return {
        'occurrences': qs,
        'all_sites': True,
    }

@register.assignment_tag
def get_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        return qs[:int(kwargs['limit'])]

    return qs

@register.inclusion_tag('events/agenda_tag.html')
def show_site_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        qs = qs[:int(kwargs['limit'])]

    return {
        'occurrences': qs
    }

@register.assignment_tag
def get_site_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        return qs[:int(kwargs['limit'])]

    return qs

@register.simple_tag
def occurrence_duration(occurrence):
    start = timezone.localtime(occurrence.start_time)
    end = timezone.localtime(occurrence.end_time)
    result = start.strftime('%A, %d %B %Y %H:%M')

    if (start.day == end.day and start.month == end.month and
            start.year == end.year):
        result += ' - {:%H:%M}'.format(end)
    else:
        result += ' - {:%A, %d %B %Y %H:%M}'.format(end)

    return result

Add extra tag which displays the occurrence duration in a smart way
Add extra tag which displays the occurrence duration in a smart way

  from django import template
+ from django.utils import timezone
  
  from fullcalendar.models import Occurrence
  
  register = template.Library()
  
  @register.inclusion_tag('events/agenda_tag.html')
  def show_agenda(*args, **kwargs):
      qs = Occurrence.objects.upcoming()
  
      if 'limit' in kwargs:
          qs = qs[:int(kwargs['limit'])]
  
      return {
          'occurrences': qs,
          'all_sites': True,
      }
  
  @register.assignment_tag
  def get_agenda(*args, **kwargs):
      qs = Occurrence.site_related.upcoming()
  
      if 'limit' in kwargs:
          return qs[:int(kwargs['limit'])]
  
      return qs
  
  @register.inclusion_tag('events/agenda_tag.html')
  def show_site_agenda(*args, **kwargs):
      qs = Occurrence.site_related.upcoming()
  
      if 'limit' in kwargs:
          qs = qs[:int(kwargs['limit'])]
  
      return {
          'occurrences': qs
      }
  
  @register.assignment_tag
  def get_site_agenda(*args, **kwargs):
      qs = Occurrence.site_related.upcoming()
  
      if 'limit' in kwargs:
          return qs[:int(kwargs['limit'])]
  
      return qs
  
+ @register.simple_tag
+ def occurrence_duration(occurrence):
+     start = timezone.localtime(occurrence.start_time)
+     end = timezone.localtime(occurrence.end_time)
+     result = start.strftime('%A, %d %B %Y %H:%M')
  
+     if (start.day == end.day and start.month == end.month and
+             start.year == end.year):
+         result += ' - {:%H:%M}'.format(end)
+     else:
+         result += ' - {:%A, %d %B %Y %H:%M}'.format(end)
+ 
+     return result
+ 
Add extra tag which displays the occurrence duration in a smart way
## Code Before:
from django import template

from fullcalendar.models import Occurrence

register = template.Library()

@register.inclusion_tag('events/agenda_tag.html')
def show_agenda(*args, **kwargs):
    qs = Occurrence.objects.upcoming()

    if 'limit' in kwargs:
        qs = qs[:int(kwargs['limit'])]

    return {
        'occurrences': qs,
        'all_sites': True,
    }

@register.assignment_tag
def get_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        return qs[:int(kwargs['limit'])]

    return qs

@register.inclusion_tag('events/agenda_tag.html')
def show_site_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        qs = qs[:int(kwargs['limit'])]

    return {
        'occurrences': qs
    }

@register.assignment_tag
def get_site_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        return qs[:int(kwargs['limit'])]

    return qs


## Instruction:
Add extra tag which displays the occurrence duration in a smart way
## Code After:
from django import template
from django.utils import timezone

from fullcalendar.models import Occurrence

register = template.Library()

@register.inclusion_tag('events/agenda_tag.html')
def show_agenda(*args, **kwargs):
    qs = Occurrence.objects.upcoming()

    if 'limit' in kwargs:
        qs = qs[:int(kwargs['limit'])]

    return {
        'occurrences': qs,
        'all_sites': True,
    }

@register.assignment_tag
def get_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        return qs[:int(kwargs['limit'])]

    return qs

@register.inclusion_tag('events/agenda_tag.html')
def show_site_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        qs = qs[:int(kwargs['limit'])]

    return {
        'occurrences': qs
    }

@register.assignment_tag
def get_site_agenda(*args, **kwargs):
    qs = Occurrence.site_related.upcoming()

    if 'limit' in kwargs:
        return qs[:int(kwargs['limit'])]

    return qs

@register.simple_tag
def occurrence_duration(occurrence):
    start = timezone.localtime(occurrence.start_time)
    end = timezone.localtime(occurrence.end_time)
    result = start.strftime('%A, %d %B %Y %H:%M')

    if (start.day == end.day and start.month == end.month and
            start.year == end.year):
        result += ' - {:%H:%M}'.format(end)
    else:
        result += ' - {:%A, %d %B %Y %H:%M}'.format(end)

    return result
