SHARE
TWEET

Untitled

a guest Feb 17th, 2016 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.contrib.auth.models import User
  2. from django.shortcuts import get_object_or_404
  3. from django.views.generic import CreateView, DeleteView, DetailView, ListView, TemplateView, UpdateView
  4. from staffManagement.models import Coach, Member, Player
  5. from django.core.urlresolvers import reverse_lazy
  6.  
  7. """
  8.        Abstract view to add user context data
  9. """
  10. class AbstractWithUserView():
  11.     def get_my_context_data(self, context):
  12.         context['number_of_player'] = self.request.user.member.team.get_number_of_player()
  13.         try:
  14.             context['head_coach'] = self.request.user.member.team.get_head_coach()
  15.         except:
  16.             context['head_coach'] = None
  17.         context['number_of_coach'] = self.request.user.member.team.get_number_of_coach()
  18.         context['team'] = self.request.user.member.team
  19.         return context
  20.  
  21.     class Meta:
  22.         abstract = True
  23.  
  24. """
  25.        Display a list of players and coachs of user.member.team
  26. """
  27. class ListPlayerAndCoach(ListView, AbstractWithUserView):
  28.     template_name = "staffManagement/playerAndCoach_list.html"
  29.  
  30.     def get_context_data(self, **kwargs):
  31.         context = super(ListPlayerAndCoach, self).get_context_data(**kwargs)
  32.         try:
  33.             context = self.get_my_context_data(context)
  34.             context['coach_list'] = Coach.objects.filter(member__team =
  35.                 self.request.user.member.team)
  36.         except:
  37.             pass
  38.  
  39.         return context
  40.  
  41.     def get_queryset(self):
  42.         return Player.objects.filter(member__team =
  43.                 self.request.user.member.team)
  44.  
  45. """
  46.        Display a list of coachs into user's team
  47. """
  48. class ListCoach(AbstractWithUserView, ListView):
  49.  
  50.     def get_context_data(self, **kwargs):
  51.         context = super(ListCoach, self).get_context_data(**kwargs)
  52.         try:
  53.             context = self.get_my_context_data(context)
  54.             context['coach_list'] = Coach.objects.filter(member__team =
  55.                 self.request.user.member.team)
  56.         except:
  57.             pass
  58.  
  59.         return context
  60.  
  61.     def get_queryset(self):
  62.         return Coach.objects.filter(member__team =
  63.                 self.request.user.member.team)
  64.  
  65. """
  66.        Display a list of players into user's team
  67. """
  68. class ListPlayer(AbstractWithUserView, ListView):
  69.  
  70.     def get_context_data(self, **kwargs):
  71.         context = super(ListPlayer, self).get_context_data(**kwargs)
  72.         try:
  73.             context = self.get_my_context_data(context)
  74.             context['coach_list'] = Coach.objects.filter(member__team =
  75.                 self.request.user.member.team)
  76.         except:
  77.             pass
  78.  
  79.         return context
  80.  
  81.  
  82.     def get_queryset(self):
  83.         return Player.objects.filter(member__team =
  84.                 self.request.user.member.team)
  85.  
  86. """
  87.        Display detail of a player
  88. """
  89. class PlayerDetailView(AbstractWithUserView, DetailView):
  90.     model = Player
  91.  
  92.     def get_context_data(self, **kwargs):
  93.         context = super(PlayerDetailView, self).get_context_data(**kwargs)
  94.         context = self.get_my_context_data(context)
  95.         context['coach_list'] = Coach.objects.filter(member__team =
  96.                 self.request.user.member.team)
  97.         return context
  98.  
  99.  
  100.     def get_object(self):
  101.         print self.kwargs
  102.         return get_object_or_404(Player, member__user__username =
  103.                 self.kwargs['username'])
  104.  
  105. """
  106.        Display detail of a coach
  107. """
  108. class CoachDetailView(AbstractWithUserView, DetailView):
  109.     model = Coach
  110.  
  111.     def get_context_data(self, **kwargs):
  112.         context = super(CoachDetailView, self).get_context_data(**kwargs)
  113.         context = self.get_my_context_data(context)
  114.         context['coach_list'] = Coach.objects.filter(member__team =
  115.                 self.request.user.member.team)
  116.         return context
  117.  
  118.     def get_object(self):
  119.         return get_object_or_404(Coach, member__user__username =
  120.                 self.kwargs['username'])
  121.  
  122. """
  123.        Create a coach
  124. """
  125. class CoachCreateView(AbstractWithUserView, CreateView):
  126.     model = Coach
  127.     slug_field = 'member__user__username'
  128.     fields = ['grade', 'squad', 'first_position', 'second_position']
  129.     success_url = reverse_lazy('playerAndCoach')
  130.  
  131.     def get_context_data(self, **kwargs):
  132.         context = super(CoachCreateView, self).get_context_data(**kwargs)
  133.         context = self.get_my_context_data(context)
  134.         context['coach_list'] = Coach.objects.filter(member__team =
  135.                 self.request.user.member.team)
  136.         return context
  137.  
  138. """
  139.        Create a player
  140. """
  141. class PlayerCreateView(AbstractWithUserView, CreateView):
  142.     model = Player
  143.     slug_field = 'member__user__username'
  144.     fields = ['number', 'weight', 'height', 'first_position', 'second_position']
  145.     success_url = reverse_lazy('playerAndCoach')
  146.  
  147.     def get_context_data(self, **kwargs):
  148.         context = super(PlayerCreateView, self).get_context_data(**kwargs)
  149.         context = self.get_my_context_data(context)
  150.         context['coach_list'] = Coach.objects.filter(member__team =
  151.                 self.request.user.member.team)
  152.         return context
  153.  
  154. """
  155.        Update a coach
  156. """
  157. class CoachUpdateView(AbstractWithUserView, UpdateView):
  158.     model = Coach
  159.     slug_field = 'member__user__username'
  160.     fields = ['grade', 'squad', 'first_position', 'second_position']
  161.     success_url = reverse_lazy('playerAndCoach')
  162.  
  163.     def get_context_data(self, **kwargs):
  164.         context = super(CoachUpdateView, self).get_context_data(**kwargs)
  165.         context = self.get_my_context_data(context)
  166.         context['coach_list'] = Coach.objects.filter(member__team =
  167.                 self.request.user.member.team)
  168.         return context
  169.  
  170. """
  171.        Update a player
  172. """
  173. class PlayerUpdateView(AbstractWithUserView, UpdateView):
  174.     model = Player
  175.     slug_field = 'member__user__username'
  176.     fields = ['number', 'weight', 'height', 'first_position', 'second_position']
  177.     success_url = reverse_lazy('playerAndCoach')
  178.  
  179.     def get_context_data(self, **kwargs):
  180.         context = super(PlayerUpdateView, self).get_context_data(**kwargs)
  181.         context = self.get_my_context_data(context)
  182.         context['coach_list'] = Coach.objects.filter(member__team =
  183.                 self.request.user.member.team)
  184.         return context
  185.  
  186. """
  187.        Delete a coach
  188. """
  189. class CoachDeleteView(AbstractWithUserView, DeleteView):
  190.     model = Coach
  191.     slug_field = 'member__user__username'
  192.     success_url = reverse_lazy('playerAndCoach')
  193.  
  194.     def get_context_data(self, **kwargs):
  195.         context = super(CoachDeleteView, self).get_context_data(**kwargs)
  196.         context = self.get_my_context_data(context)
  197.         context['coach_list'] = Coach.objects.filter(member__team =
  198.                 self.request.user.member.team)
  199.         return context
  200.  
  201. """
  202.        Delete a player
  203. """
  204. class PlayerDeleteView(AbstractWithUserView, DeleteView):
  205.     model = Player
  206.     slug_field = 'member__user__username'
  207.     success_url = reverse_lazy('playerAndCoach')
  208.  
  209.     def get_context_data(self, **kwargs):
  210.         context = super(PlayerDeleteView, self).get_context_data(**kwargs)
  211.         context = self.get_my_context_data(context)
  212.         context['coach_list'] = Coach.objects.filter(member__team =
  213.                 self.request.user.member.team)
  214.         return context
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top