# Path: livestream/models.py
# class Stream(models.Model):
#     ''' This is the model for each live stream event nothing really special
#     except for the iframe field maybe. It contains the loaded iframe for 
#     each stream event and is marked as safe. Maybe we change this or add
#     support for streams directly loaded into Projekktor'''
#     title = models.CharField(_(u"Stream title"),max_length=200)
#     slug = AutoSlugField(populate_from='title',unique=True,verbose_name=_(u"Slug"),help_text=_(u"Slugs are parts of an URL that you can define"))
#     startDate = models.DateTimeField(_(u"Beginning of the event"))
#     endDate = models.DateTimeField(_(u"End of the event"))
#     description = models.TextField(_(u"Description"))
#     link = models.URLField(_(u"Link"),blank=True)
#     rtmpLink = models.URLField(_(u"RTMP Link"),blank=True,help_text=_(u"RTMP is a protocol to stream video. If you have a server as host, you can insert a link of the output"))
#     audioOnlyLink = models.URLField(_(u"Audio-only Link"),blank=True)
#     iframe = models.TextField(_(u"iFrame of the Stream"))
#     published = models.BooleanField(verbose_name=_(u"Published"),default=False)
#     created = models.DateTimeField(auto_now_add=True,verbose_name=_(u"Created"))
#     modified = models.DateTimeField(auto_now=True, verbose_name=_(u"Modified"))
#     def __unicode__(self):
#         return self.title
#     def get_absolute_url(self):
#         return "/stream/%s/" % self.slug
#     def display_iFrameSafeField(self): 
#         return mark_safe(self.iframe)


from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from livestream.models import Stream
from django.utils import timezone



def current(request):
    ''' This view gets all streams that are scheduled to be live right now
    if there are no streams it redirects to the liste view. The upcoming_streams_list
    is used to show upcoming events in a side bar (see the template)'''
    stream_list = Stream.objects.filter(published=True,startDate__lt=timezone.now, endDate__gt=timezone.now).order_by('-startDate')
    upcoming_streams_list = Stream.objects.filter(published=True,endDate__gt=timezone.now).order_by('-startDate')[:5]
    if not stream_list:
        return redirect(list_streams)
    else:
        return TemplateResponse(request, 'livestream/current.html', {'stream_list': stream_list, 'upcoming_streams_list': upcoming_streams_list})

def list_streams(request):
    ''' This view shows gets all upcoming streaming events
    and forwards them to our template '''
    stream_list = Stream.objects.filter(published=True,endDate__gt=timezone.now).order_by('-startDate')
    return TemplateResponse(request, 'livestream/list.html', {'stream_list': stream_list})

def detail(request, slug):
    ''' This view shows the detail of a stream, it is used to
    show the user more information on one event but not
    for showing the player'''
    stream = get_object_or_404(Stream, slug=slug)

# Path: livestream/models.py
# class Stream(models.Model):
#     ''' This is the model for each live stream event nothing really special
#     except for the iframe field maybe. It contains the loaded iframe for 
#     each stream event and is marked as safe. Maybe we change this or add
#     support for streams directly loaded into Projekktor'''
#     title = models.CharField(_(u"Stream title"),max_length=200)
#     slug = AutoSlugField(populate_from='title',unique=True,verbose_name=_(u"Slug"),help_text=_(u"Slugs are parts of an URL that you can define"))
#     startDate = models.DateTimeField(_(u"Beginning of the event"))
#     endDate = models.DateTimeField(_(u"End of the event"))
#     description = models.TextField(_(u"Description"))
#     link = models.URLField(_(u"Link"),blank=True)
#     rtmpLink = models.URLField(_(u"RTMP Link"),blank=True,help_text=_(u"RTMP is a protocol to stream video. If you have a server as host, you can insert a link of the output"))
#     audioOnlyLink = models.URLField(_(u"Audio-only Link"),blank=True)
#     iframe = models.TextField(_(u"iFrame of the Stream"))
#     published = models.BooleanField(verbose_name=_(u"Published"),default=False)
#     created = models.DateTimeField(auto_now_add=True,verbose_name=_(u"Created"))
#     modified = models.DateTimeField(auto_now=True, verbose_name=_(u"Modified"))
#     def __unicode__(self):
#         return self.title
#     def get_absolute_url(self):
#         return "/stream/%s/" % self.slug
#     def display_iFrameSafeField(self): 
#         return mark_safe(self.iframe)


# Path: livestream/views.py
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from livestream.models import Stream
from django.utils import timezone




def current(request):
    ''' This view gets all streams that are scheduled to be live right now
    if there are no streams it redirects to the liste view. The upcoming_streams_list
    is used to show upcoming events in a side bar (see the template)'''
    stream_list = Stream.objects.filter(published=True,startDate__lt=timezone.now, endDate__gt=timezone.now).order_by('-startDate')
    upcoming_streams_list = Stream.objects.filter(published=True,endDate__gt=timezone.now).order_by('-startDate')[:5]
    if not stream_list:
        return redirect(list_streams)
    else:
        return TemplateResponse(request, 'livestream/current.html', {'stream_list': stream_list, 'upcoming_streams_list': upcoming_streams_list})

def list_streams(request):
    ''' This view shows gets all upcoming streaming events
    and forwards them to our template '''
    stream_list = Stream.objects.filter(published=True,endDate__gt=timezone.now).order_by('-startDate')
    return TemplateResponse(request, 'livestream/list.html', {'stream_list': stream_list})

def detail(request, slug):
    ''' This view shows the detail of a stream, it is used to
    show the user more information on one event but not
    for showing the player'''
    stream = get_object_or_404(Stream, slug=slug)

    upcoming_streams_list = Stream.objects.filter(published=True,endDate__gt=timezone.now).order_by('-startDate')[:5]