PageRenderTime 141ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/cal/views.py

https://gitlab.com/lburdzy/asg
Python | 170 lines | 113 code | 36 blank | 21 comment | 4 complexity | 8fd286386c80ecf0c45eae22ebcb1b10 MD5 | raw file
  1. from braces.views import LoginRequiredMixin, UserPassesTestMixin, StaffuserRequiredMixin
  2. from django.shortcuts import redirect
  3. from django.views.generic.edit import DeleteView
  4. from django.conf import settings
  5. from cal.models import Event, PGroup, Faction, Slot, Entry
  6. from cal.serializers import EventSerializer, FactionSerializer, SlotSerializer
  7. from cal.serializers import PGroupSerializer, EntrySerializer
  8. from rest_framework import viewsets
  9. from cal.permissions import IsOwnerOrReadOnly
  10. from django.views.generic import ListView, DetailView, CreateView
  11. from django.utils import timezone
  12. from django.core.urlresolvers import reverse, reverse_lazy
  13. from pure_pagination.mixins import PaginationMixin
  14. from django.template.loader import get_template
  15. from django.http import HttpResponse
  16. from weasyprint import HTML
  17. #
  18. # ---- API Views
  19. #
  20. class EventViewSet(viewsets.ModelViewSet):
  21. """
  22. API endpoint for events
  23. """
  24. queryset = Event.objects.all().order_by('datetime')
  25. serializer_class = EventSerializer
  26. def update(self, request, *args, **kwargs):
  27. self.methods = ('put', )
  28. self.permission_classes = (IsOwnerOrReadOnly, )
  29. return super(self.__class__, self).update(request, *args, **kwargs)
  30. class PGroupViewSet(viewsets.ModelViewSet):
  31. """
  32. API endpoint for Player groups
  33. """
  34. queryset = PGroup.objects.all().order_by('name')
  35. serializer_class = PGroupSerializer
  36. class FactionViewSet(viewsets.ModelViewSet):
  37. """
  38. API endpoint for factions
  39. """
  40. queryset = Faction.objects.all().order_by('event', 'name')
  41. serializer_class = FactionSerializer
  42. class SlotViewSet(viewsets.ModelViewSet):
  43. """
  44. API endpoint for slots
  45. """
  46. queryset = Slot.objects.all().order_by('faction', 'name')
  47. serializer_class = SlotSerializer
  48. class EntryViewSet(viewsets.ModelViewSet):
  49. """
  50. API endpoint for entries
  51. """
  52. queryset = Entry.objects.all().order_by('faction', 'slot')
  53. serializer_class = EntrySerializer
  54. #
  55. # ---- Generic Django Views
  56. #
  57. class EventListView(PaginationMixin, ListView):
  58. queryset = Event.objects.filter(datetime__gte=timezone.now()).order_by('datetime')
  59. context_object_name = 'events'
  60. paginate_by = 6
  61. class EventArchiveView(PaginationMixin, ListView):
  62. queryset = Event.objects.all()
  63. context_object_name = 'events'
  64. paginate_by = 6
  65. template_name = 'cal/event_list.html'
  66. class EventDetailView(DetailView):
  67. model = Event
  68. context_object_name = 'event'
  69. def get_context_data(self, **kwargs):
  70. context = super().get_context_data(**kwargs)
  71. context['user_can_signup'] = self.object.user_can_sign_up(self.request.user)
  72. return context
  73. class EntryCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
  74. model = Entry
  75. fields = []
  76. login_url = settings.LOGIN_REDIRECT_URL
  77. def get_success_url(self):
  78. return reverse('cal:event-detail', kwargs={'pk': self.kwargs.get('pk')})
  79. def get_event(self):
  80. faction_id = self.kwargs.get('faction_id')
  81. slot_id = self.kwargs.get('slot_id')
  82. if faction_id:
  83. faction = Faction.objects.get(id=faction_id)
  84. event = faction.event
  85. elif slot_id:
  86. slot = Slot.objects.get(id=slot_id)
  87. event = slot.faction.event
  88. return event
  89. def form_valid(self, form):
  90. form.instance.user = self.request.user
  91. faction_id = self.kwargs.get('faction_id')
  92. slot_id = self.kwargs.get('slot_id')
  93. if faction_id:
  94. form.instance.faction = Faction.objects.get(id=faction_id)
  95. elif slot_id:
  96. slot = Slot.objects.get(id=slot_id)
  97. form.instance.slot = slot
  98. form.instance.faction = Faction.objects.get(id=slot.faction.id)
  99. return super(EntryCreateView, self).form_valid(form)
  100. def test_func(self, user):
  101. event = self.get_event()
  102. return event.user_can_sign_up(user)
  103. def no_permissions_fail(self, request=None):
  104. return redirect(reverse_lazy('cal:event-detail', kwargs={'pk': self.get_event().pk}))
  105. def get_context_data(self, **kwargs):
  106. context = super().get_context_data(**kwargs)
  107. context['action_name'] = 'Zapisz się'
  108. return context
  109. class EntryDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
  110. model = Entry
  111. login_url = settings.LOGIN_REDIRECT_URL
  112. template_name = 'cal/entry_form.html'
  113. def get_success_url(self):
  114. return reverse('cal:event-detail', kwargs={'pk': self.object.faction.event.pk})
  115. def test_func(self, user):
  116. return self.get_object().user == user
  117. def no_permissions_fail(self, request=None):
  118. return redirect(reverse_lazy('cal:event-detail', kwargs={'pk': self.get_object().faction.event.pk}))
  119. def get_context_data(self, **kwargs):
  120. context = super().get_context_data(**kwargs)
  121. context['action_name'] = 'Wypisz się'
  122. return context
  123. class EventDetailPDFView(StaffuserRequiredMixin, DetailView):
  124. template_name = 'cal/event_list_pdf.html'
  125. model = Event
  126. context_object_name = 'event'
  127. def render_to_response(self, context, **response_kwargs):
  128. context = self.get_context_data()
  129. html_template = get_template(self.template_name)
  130. rendered_html = html_template.render(context).encode(encoding="UTF-8")
  131. pdf_file = HTML(string=rendered_html).write_pdf()
  132. http_response = HttpResponse(pdf_file, content_type='application/pdf')
  133. http_response['Content-Disposition'] = 'filename="report.pdf"'
  134. return http_response