From 56a4b6079ef78553b1263bf8bceabe83ae1fb705 Mon Sep 17 00:00:00 2001
From: Amit Kaplansky <106595737+amitKaplansky@users.noreply.github.com>
Date: Wed, 31 May 2023 17:14:30 +0300
Subject: [PATCH] Add logic to create or join event
---
game_event/models.py | 7 +++
game_event/templates/game_event/result.html | 58 ++++++++++++++++++---
game_event/views.py | 26 ++++++---
player/models.py | 10 ++++
4 files changed, 89 insertions(+), 12 deletions(-)
diff --git a/game_event/models.py b/game_event/models.py
index 3d07153..efdfdab 100644
--- a/game_event/models.py
+++ b/game_event/models.py
@@ -51,3 +51,10 @@ def validate_game_event(court, ball_game, min_number_of_players, max_number_of_p
@staticmethod
def is_ball_game_playable_at_court(court, ball_game):
return CourtBallGame.is_ball_game_playable(court, ball_game)
+
+
+ def is_event_full(self):
+ from game_event_player.models import GameEventPlayer
+
+ current_players = GameEventPlayer.objects.filter(game_event=self).count()
+ return current_players == self.max_number_of_players
\ No newline at end of file
diff --git a/game_event/templates/game_event/result.html b/game_event/templates/game_event/result.html
index afaf906..6362fd8 100644
--- a/game_event/templates/game_event/result.html
+++ b/game_event/templates/game_event/result.html
@@ -3,16 +3,62 @@
+
{% if event_exists == False %}
- Can not remove from event, Event Does not Exist!
- {% elif reached_max == True %}
- Max number of Player reached! You can't join
- {% elif in_event %}
- You joined the event!
+
+
Can not remove from event, Event Does not Exist!
+
{% else %}
- You are not in the event!
+
+ {% if messages %}
+
+
+
+
+ {% for message in messages %}
+ - {{ message }}
+ {% endfor %}
+
+
+
+
+ {% else %}
+ {% if in_event %}
+
You joined the event!
+ {% else %}
+
You are not in the event!
+ {% endif %}
+ {% endif %}
+
{% endif %}
diff --git a/game_event/views.py b/game_event/views.py
index 1308cc4..42bdca4 100644
--- a/game_event/views.py
+++ b/game_event/views.py
@@ -116,7 +116,7 @@ def join_event(request, id):
GameEvent.objects.get(pk=id)
except GameEvent.DoesNotExist:
return render(request, 'game_event/join-event.html', {'id': id, 'event_exists': False})
-
+
return render(request, 'game_event/join-event.html', {'id': id, 'event_exists': True})
@@ -139,14 +139,28 @@ def process_answer_game_event(request, id):
player = Player.objects.get(user=request.user)
ball_responsible = request.POST.get('answer') == 'yes'
content = {}
- num_of_players_in_event = len(GameEventPlayer.objects.filter(game_event=event))
- if num_of_players_in_event < event.max_number_of_players:
+
+ is_event_full = event.is_event_full()
+ is_event_time_available = player.is_event_time_available(event.time)
+
+ error_messages = []
+ if is_event_full:
+ error_messages.append("The event is full! cannot join event")
+ if not is_event_time_available:
+ error_messages.append("You have scheduling conflicts. Resolve them before joining the event")
+
+ if error_messages:
+ for error in error_messages:
+ messages.error(request, error)
+ else:
GameEventPlayer.objects.create(game_event=event, player=player, ball_responsible=ball_responsible).save()
in_event = GameEventPlayer.objects.filter(game_event=event, player=player).exists()
content["in_event"] = in_event
- content["reached_max"] = False
- else:
- content["reached_max"] = True
+
content['event_exists'] = True
return render(request, 'game_event/result.html', content)
+
+
+
+
diff --git a/player/models.py b/player/models.py
index b625d68..3a80d18 100644
--- a/player/models.py
+++ b/player/models.py
@@ -1,4 +1,5 @@
from django.contrib.auth.models import User
+from django.utils import timezone
from django.db import models
import datetime
from django.core.exceptions import ValidationError
@@ -54,3 +55,12 @@ def validate_and_save(self, birth_date, favorite_ball_game):
else:
self.save()
+
+ def is_event_time_available(self, desired_event_time):
+ from game_event_player.models import GameEventPlayer
+ TIME_LIMIT = 4
+ start_time_limit = desired_event_time - timezone.timedelta(hours=TIME_LIMIT)
+ end_time_limit = desired_event_time + timezone.timedelta(hours=TIME_LIMIT)
+ conflicting_events = GameEventPlayer.objects.filter(player=self, game_event__time__range=(start_time_limit, end_time_limit))
+ return not conflicting_events.exists()
+