PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tournament/views.py

https://bitbucket.org/davidsiu/nextagsl
Python | 161 lines | 65 code | 59 blank | 37 comment | 7 complexity | cea3d5e87743a22301cf9117e4205015 MD5 | raw file
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2010 David J Siu
  4. #
  5. # Source Code: https://bitbucket.org/davidsiu/nextagsl
  6. # Website: http://nextagsl.appspot.com/
  7. #
  8. # -----------------------------------------------------------------------------
  9. __author__ = 'David J Siu <davidsiu@gmail.com>'
  10. # -----------------------------------------------------------------------------
  11. import os
  12. import logging
  13. import time
  14. import userutils
  15. from tournament.models import Tournament
  16. from tournament.models import Registration
  17. from match.models import Match
  18. from django.views.generic.simple import direct_to_template
  19. from django.forms.models import modelformset_factory
  20. from django.http import HttpResponseRedirect
  21. from django.shortcuts import get_object_or_404
  22. # -----------------------------------------------------------------------------
  23. def tournamentlist(request):
  24. # Start the performance timer
  25. startTime = time.time()
  26. # The model to pass into the view
  27. model = {}
  28. # Get the current user data and populate the model
  29. userutils.getCurrentUserData(request, model)
  30. # Get a list of all the current tournaments
  31. all_tournaments = Tournament.objects.filter()
  32. model['all_tournaments'] = all_tournaments
  33. # Calculate the performance of the request
  34. endTime = time.time()
  35. model['elapsed_time'] = round(endTime - startTime, 5)
  36. return direct_to_template(request, 'tournament/tournament.html', model)
  37. # -----------------------------------------------------------------------------
  38. def tournamentregister(request, tournament_id):
  39. # The model to pass into the view
  40. model = {}
  41. # Get the current user data and populate the model
  42. userutils.getCurrentUserData(request, model)
  43. # Get a list of all the current tournaments
  44. current_tournament = get_object_or_404(Tournament, id=tournament_id)
  45. model['tournament'] = current_tournament
  46. # Register this user if they are not already registered
  47. if not current_tournament.is_registration_locked:
  48. current_member = model['current_member']
  49. if current_member:
  50. current_registration = Registration.objects.filter(tournament=tournament_id, member=current_member)
  51. if not current_registration:
  52. current_registration = Registration(tournament=current_tournament, member=current_member)
  53. current_registration.save()
  54. return HttpResponseRedirect('/tournament/' + str(tournament_id))
  55. # -----------------------------------------------------------------------------
  56. def tournamentdetails(request, tournament_id):
  57. # Start the performance timer
  58. startTime = time.time()
  59. # The model to pass into the view
  60. model = {}
  61. # Get the current user data and populate the model
  62. userutils.getCurrentUserData(request, model)
  63. # Get a list of all the current tournaments
  64. model['tournament'] = get_object_or_404(Tournament, id=tournament_id)
  65. # Get a list of all the registrations for this tournament
  66. registrations = Registration.objects.filter(tournament=tournament_id)
  67. model['registrations'] = registrations
  68. # Determine if we need to show the registration link for current user
  69. if 'current_member' in model:
  70. current_member = model['current_member']
  71. try:
  72. current_registration = Registration.objects.get(tournament=tournament_id, member=current_member)
  73. model['is_signed_up'] = True
  74. except Registration.DoesNotExist:
  75. model['is_signed_up'] = False
  76. # Get the list of matches for the Ro8
  77. model['ro8_matches'] = Match.objects.filter(tournament=tournament_id, match_type="Round of 8").order_by('match_number')
  78. # Get the list of matches for the Semi-Final
  79. model['semi_matches'] = Match.objects.filter(tournament=tournament_id, match_type="Semi Finals").order_by('match_number')
  80. # Get the match for the Final
  81. final_match = Match.objects.filter(tournament=tournament_id, match_type="Final")
  82. if final_match:
  83. model['final_match'] = final_match[0]
  84. # Calculate the performance of the request
  85. endTime = time.time()
  86. model['elapsed_time'] = round(endTime - startTime, 5)
  87. return direct_to_template(request, 'tournament/tournamentdetails.html', model)
  88. # -----------------------------------------------------------------------------
  89. def registrationlist(request):
  90. # Start the performance timer
  91. startTime = time.time()
  92. # The model to pass into the view
  93. model = {}
  94. # Get the current user data and populate the model
  95. userutils.getCurrentUserData(request, model)
  96. # Get a list of all the current registrations
  97. all_registrations = Registration.objects.filter()
  98. model['all_registrations'] = all_registrations
  99. # Calculate the performance of the request
  100. endTime = time.time()
  101. model['elapsed_time'] = round(endTime - startTime, 5)
  102. return direct_to_template(request, 'tournament/registration.html', model)