# Path: pycon/schedule/models.py
# class Session(models.Model):
#     
#     day = models.ForeignKey(Day, related_name="sessions")
#     slots = models.ManyToManyField(Slot, related_name="sessions")
#     
#     def sorted_slots(self):
#         return self.slots.order_by("start")
#     
#     def start(self):
#         slots = self.sorted_slots()
#         if slots:
#             return list(slots)[0].start
#         else:
#             return None
#     
#     def end(self):
#         slots = self.sorted_slots()
#         if slots:
#             return list(slots)[-1].end
#         else:
#             return None
#     
#     def __unicode__(self):
#         start = self.start()
#         end = self.end()
#         if start and end:
#             return u"%s: %s - %s" % (
#                 self.day.date.strftime("%a"),
#                 start.strftime("%X"),
#                 end.strftime("%X")
#             )
#         return u""
# 
# class SessionRole(models.Model):
#     
#     SESSION_ROLE_CHAIR = 1
#     SESSION_ROLE_RUNNER = 2
#     
#     SESSION_ROLE_TYPES = [
#         (SESSION_ROLE_CHAIR, "Session Chair"),
#         (SESSION_ROLE_RUNNER, "Session Runner"),
#     ]
#     
#     session = models.ForeignKey(Session)
#     user = models.ForeignKey(User)
#     role = models.IntegerField(choices=SESSION_ROLE_TYPES)
#     status = models.NullBooleanField()
#     
#     submitted = models.DateTimeField(default=datetime.now)
#     
#     class Meta:
#         unique_together = [("session", "user", "role")]


import hashlib
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .models import Session, SessionRole





def session_list(request):
    
    sessions = Session.objects.all()
    
    return render(request, "schedule/session_list.html", {
        "sessions": sessions,
    })


@login_required
def session_staff_email(request):
    
    if not request.user.is_staff:
        return redirect("schedule_session_list")
    
    data = "\n".join(user.email for user in User.objects.filter(sessionrole__isnull=False).distinct())
    
    return HttpResponse(data, content_type="text/plain;charset=UTF-8")


def session_detail(request, session_id):
    

# Path: pycon/schedule/models.py
# class Session(models.Model):
#     
#     day = models.ForeignKey(Day, related_name="sessions")
#     slots = models.ManyToManyField(Slot, related_name="sessions")
#     
#     def sorted_slots(self):
#         return self.slots.order_by("start")
#     
#     def start(self):
#         slots = self.sorted_slots()
#         if slots:
#             return list(slots)[0].start
#         else:
#             return None
#     
#     def end(self):
#         slots = self.sorted_slots()
#         if slots:
#             return list(slots)[-1].end
#         else:
#             return None
#     
#     def __unicode__(self):
#         start = self.start()
#         end = self.end()
#         if start and end:
#             return u"%s: %s - %s" % (
#                 self.day.date.strftime("%a"),
#                 start.strftime("%X"),
#                 end.strftime("%X")
#             )
#         return u""
# 
# class SessionRole(models.Model):
#     
#     SESSION_ROLE_CHAIR = 1
#     SESSION_ROLE_RUNNER = 2
#     
#     SESSION_ROLE_TYPES = [
#         (SESSION_ROLE_CHAIR, "Session Chair"),
#         (SESSION_ROLE_RUNNER, "Session Runner"),
#     ]
#     
#     session = models.ForeignKey(Session)
#     user = models.ForeignKey(User)
#     role = models.IntegerField(choices=SESSION_ROLE_TYPES)
#     status = models.NullBooleanField()
#     
#     submitted = models.DateTimeField(default=datetime.now)
#     
#     class Meta:
#         unique_together = [("session", "user", "role")]


# Path: pycon/schedule/views.py
import hashlib
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .models import Session, SessionRole






def session_list(request):
    
    sessions = Session.objects.all()
    
    return render(request, "schedule/session_list.html", {
        "sessions": sessions,
    })


@login_required
def session_staff_email(request):
    
    if not request.user.is_staff:
        return redirect("schedule_session_list")
    
    data = "\n".join(user.email for user in User.objects.filter(sessionrole__isnull=False).distinct())
    
    return HttpResponse(data, content_type="text/plain;charset=UTF-8")


def session_detail(request, session_id):
    

    session = get_object_or_404(Session, id=session_id)