# Path: apps/ot/views/main.py
# class MainView(TemplateView):
#     template_name = 'main.html'
# 
#     def get_context_data(self, **kwargs):
#         context = super(MainView, self).get_context_data(**kwargs)
# 
#         context['available'] = vote_available(self.request.user)
# 
#         if hasattr(self.request.user, 'freshman'):
#             voted_clubs = self.request.user.freshman.voted_clubs
#             context['voted_band'] = voted_clubs.filter(is_band=True)
#             context['voted_non_band'] = voted_clubs.filter(is_band=False)
#         else:
#             context['voted_band'] = []
#             context['voted_non_band'] = []
# 
#         context['band_limit'] = Freshman.BAND_VOTE_LIMIT
#         context['non_band_limit'] = Freshman.NON_BAND_VOTE_LIMIT
#         context['total_limit'] = Freshman.BAND_VOTE_LIMIT + Freshman.NON_BAND_VOTE_LIMIT
# 
#         return context
# 
#     def post(self, request, *args, **kwargs):
#         pass
# 
# Path: apps/ot/views/club.py
# class ClubDetailView(DetailView):
#     template_name = 'club_detail.html'
#     model = Club
# 
#     def get_context_data(self, **kwargs):
#         context = super(ClubDetailView, self).get_context_data(**kwargs)
# 
#         user = self.request.user
#         available = vote_available(user)
#         context['available'] = available
# 
#         if hasattr(user, 'freshman'):
#             club = self.object
# 
#             # 유저가 이미 해당 동아리에 투표했는지 여부
#             context['voted'] = bool(user.freshman.voted_clubs.filter(pk=club.pk))
#             # 유저가 해당 동아리 계열(밴드/비밴드) 최대 투표수를 넘었는지 여부
#             context['exceeded'] = user.freshman.vote_limit_exceeded(club.is_band)
# 
#         return context
# 
#     @method_decorator(login_required())
#     def post(self, request, *args, **kwargs):
#         """
#         현재 동아리에 투표
#         """
#         if vote_available(request.user):
#             club = self.get_object()
#             freshman = request.user.freshman
# 
#             voted = freshman.voted_clubs.filter(pk=club.pk)
# 
#             if voted:
#                 freshman.voted_clubs.remove(club)
#                 freshman.save()
#             elif not freshman.vote_limit_exceeded(club.is_band):
#                 freshman.voted_clubs.add(club)
#                 freshman.save()
# 
#         return redirect('.')
# 
# class ClubListView(ListView):
#     template_name = 'club_list.html'
#     context_object_name = 'clubs'
# 
#     def get_queryset(self):
#         if 'is_band' in self.request.GET:
#             if self.request.GET['is_band'] == '0':
#                 queryset = Club.objects.filter(is_band=False)
#             elif self.request.GET['is_band'] == '1':
#                 queryset = Club.objects.filter(is_band=True)
#             else:
#                 raise Http404
#         else:
#             queryset = Club.objects.all()
# 
#         sorted_queryset = list()
# 
#         for pk in [20, 18, 15, 17, 9, 21, 16, 8, 23, 13, 11, 10, 14]:
#             sorted_queryset.append(queryset.get(pk=pk))
# 
#         return sorted_queryset
# 
#         # return queryset.order_by('?')
# 
# Path: apps/ot/views/user.py
# class TSizeView(FormView):
#     template_name = 'tsize.html'
#     form_class = TSizeForm
#     success_url = '/ot/'
# 
#     def form_valid(self, form):
#         if hasattr(self.request.user, 'freshman'):
#             self.request.user.freshman.tsize = form.cleaned_data['tsize']
#             self.request.user.freshman.save()
#         else:
#             freshman = form.save(commit=False)
#             freshman.user = self.request.user
#             freshman.save()
# 
#         return super(TSizeView, self).form_valid(form)
# 
#     def get_initial(self):
#         initial = super(TSizeView, self).get_initial()
# 
#         if hasattr(self.request.user, 'freshman'):
#             initial['tsize'] = self.request.user.freshman.tsize
# 
#         return initial
# 
# class ResultView(TemplateView):
#     template_name = 'result.html'
# 
#     def get_context_data(self, **kwargs):
#         context = super(ResultView, self).get_context_data(**kwargs)
# 
#         context['clubs'] = Club.objects.all().annotate(cnt=Count('votes')).order_by('-cnt')
#         context['bands'] = Club.objects.filter(is_band=True).annotate(cnt=Count('votes')).order_by('-cnt')
#         context['non_bands'] = Club.objects.filter(is_band=False).annotate(cnt=Count('votes')).order_by('-cnt')
# 
#         context['cnt_voted'] = Freshman.objects.all().count()
# 
#         # Personal T shirts size
#         freshmen = Freshman.objects.all()
# 
#         size_list = ""
#         for freshman in freshmen:
#             info = freshman.user.portal_info
#             size_list += "<p>%s,%s,%s</p>" % (info.ku_kname, info.ku_std_no, freshman.tsize)
#         context['size_list'] = size_list
# 
#         return context


from django.conf.urls import url
from .views.main import MainView
from .views.club import ClubDetailView, ClubListView
from .views.user import TSizeView, ResultView


urlpatterns = [
    url(r'^$', MainView.as_view(), name='main'),
    url(r'^result/$', ResultView.as_view()),

# Path: apps/ot/views/main.py
# class MainView(TemplateView):
#     template_name = 'main.html'
# 
#     def get_context_data(self, **kwargs):
#         context = super(MainView, self).get_context_data(**kwargs)
# 
#         context['available'] = vote_available(self.request.user)
# 
#         if hasattr(self.request.user, 'freshman'):
#             voted_clubs = self.request.user.freshman.voted_clubs
#             context['voted_band'] = voted_clubs.filter(is_band=True)
#             context['voted_non_band'] = voted_clubs.filter(is_band=False)
#         else:
#             context['voted_band'] = []
#             context['voted_non_band'] = []
# 
#         context['band_limit'] = Freshman.BAND_VOTE_LIMIT
#         context['non_band_limit'] = Freshman.NON_BAND_VOTE_LIMIT
#         context['total_limit'] = Freshman.BAND_VOTE_LIMIT + Freshman.NON_BAND_VOTE_LIMIT
# 
#         return context
# 
#     def post(self, request, *args, **kwargs):
#         pass
# 
# Path: apps/ot/views/club.py
# class ClubDetailView(DetailView):
#     template_name = 'club_detail.html'
#     model = Club
# 
#     def get_context_data(self, **kwargs):
#         context = super(ClubDetailView, self).get_context_data(**kwargs)
# 
#         user = self.request.user
#         available = vote_available(user)
#         context['available'] = available
# 
#         if hasattr(user, 'freshman'):
#             club = self.object
# 
#             # 유저가 이미 해당 동아리에 투표했는지 여부
#             context['voted'] = bool(user.freshman.voted_clubs.filter(pk=club.pk))
#             # 유저가 해당 동아리 계열(밴드/비밴드) 최대 투표수를 넘었는지 여부
#             context['exceeded'] = user.freshman.vote_limit_exceeded(club.is_band)
# 
#         return context
# 
#     @method_decorator(login_required())
#     def post(self, request, *args, **kwargs):
#         """
#         현재 동아리에 투표
#         """
#         if vote_available(request.user):
#             club = self.get_object()
#             freshman = request.user.freshman
# 
#             voted = freshman.voted_clubs.filter(pk=club.pk)
# 
#             if voted:
#                 freshman.voted_clubs.remove(club)
#                 freshman.save()
#             elif not freshman.vote_limit_exceeded(club.is_band):
#                 freshman.voted_clubs.add(club)
#                 freshman.save()
# 
#         return redirect('.')
# 
# class ClubListView(ListView):
#     template_name = 'club_list.html'
#     context_object_name = 'clubs'
# 
#     def get_queryset(self):
#         if 'is_band' in self.request.GET:
#             if self.request.GET['is_band'] == '0':
#                 queryset = Club.objects.filter(is_band=False)
#             elif self.request.GET['is_band'] == '1':
#                 queryset = Club.objects.filter(is_band=True)
#             else:
#                 raise Http404
#         else:
#             queryset = Club.objects.all()
# 
#         sorted_queryset = list()
# 
#         for pk in [20, 18, 15, 17, 9, 21, 16, 8, 23, 13, 11, 10, 14]:
#             sorted_queryset.append(queryset.get(pk=pk))
# 
#         return sorted_queryset
# 
#         # return queryset.order_by('?')
# 
# Path: apps/ot/views/user.py
# class TSizeView(FormView):
#     template_name = 'tsize.html'
#     form_class = TSizeForm
#     success_url = '/ot/'
# 
#     def form_valid(self, form):
#         if hasattr(self.request.user, 'freshman'):
#             self.request.user.freshman.tsize = form.cleaned_data['tsize']
#             self.request.user.freshman.save()
#         else:
#             freshman = form.save(commit=False)
#             freshman.user = self.request.user
#             freshman.save()
# 
#         return super(TSizeView, self).form_valid(form)
# 
#     def get_initial(self):
#         initial = super(TSizeView, self).get_initial()
# 
#         if hasattr(self.request.user, 'freshman'):
#             initial['tsize'] = self.request.user.freshman.tsize
# 
#         return initial
# 
# class ResultView(TemplateView):
#     template_name = 'result.html'
# 
#     def get_context_data(self, **kwargs):
#         context = super(ResultView, self).get_context_data(**kwargs)
# 
#         context['clubs'] = Club.objects.all().annotate(cnt=Count('votes')).order_by('-cnt')
#         context['bands'] = Club.objects.filter(is_band=True).annotate(cnt=Count('votes')).order_by('-cnt')
#         context['non_bands'] = Club.objects.filter(is_band=False).annotate(cnt=Count('votes')).order_by('-cnt')
# 
#         context['cnt_voted'] = Freshman.objects.all().count()
# 
#         # Personal T shirts size
#         freshmen = Freshman.objects.all()
# 
#         size_list = ""
#         for freshman in freshmen:
#             info = freshman.user.portal_info
#             size_list += "<p>%s,%s,%s</p>" % (info.ku_kname, info.ku_std_no, freshman.tsize)
#         context['size_list'] = size_list
# 
#         return context


# Path: apps/ot/urls.py
from django.conf.urls import url
from .views.main import MainView
from .views.club import ClubDetailView, ClubListView
from .views.user import TSizeView, ResultView



urlpatterns = [
    url(r'^$', MainView.as_view(), name='main'),
    url(r'^result/$', ResultView.as_view()),

    url(r'^tshirt/$', TSizeView.as_view(), name='t_size'),