Skip to content

Commit 07b53a4

Browse files
committed
exclude Yourself player from Tournament set
1 parent 966f92b commit 07b53a4

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The Othello server uses Ion OAuth, you will need to register an application [her
5555
* Acceptable hosts can be found in the `ALLOWED_HOSTS` list in `othello/settings/__init__.py`
5656
* url must be inputted exactly or OAuth will fail
5757

58-
If you are using `docker` to host the Othello services, you will have to manually copy `othello/settings/secret.py.sample` to `othello/settings/secret.py`. If you are using `vagrant`, this is automatically done for you.
58+
If you are using `docker` to host the Othello services, you will have to manually copy `othello/settings/secret.py.sample` to `othello/settings/secret.py`. If you are using `vagrant`, this is done for you automatically.
5959

6060
After registering an OAuth application enter the key and secret in the `SOCIAL_AUTH_ION_KEY` and `SOCIAL_AUTH_ION_SECRET` variables in `secret.py`
6161

othello/apps/tournaments/forms.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django import forms
44
from django.conf import settings
5+
from django.contrib.auth import get_user_model
56
from django.core.exceptions import ValidationError
67
from django.utils import timezone
78

@@ -18,23 +19,28 @@ class TournamentCreateForm(forms.ModelForm):
1819

1920
game_time_limit = forms.IntegerField(label="Game Time Limit: ", min_value=1, max_value=15)
2021
num_rounds = forms.IntegerField(label="Amount of Rounds: ", min_value=15, max_value=settings.MAX_ROUND_NUM)
21-
include_users = forms.ModelMultipleChoiceField(label="Include Users: ", queryset=Submission.objects.latest())
22-
bye_player = forms.ModelChoiceField(label="Bye Player: ", queryset=Submission.objects.latest())
22+
include_users = forms.ModelMultipleChoiceField(label="Include Users: ", queryset=None)
23+
bye_player = forms.ModelChoiceField(label="Bye Player: ", queryset=None)
2324
runoff = forms.BooleanField(label="Enable Time Hoarding?", initial=False, required=False)
2425

2526
def __init__(self, *args: Any, **kwargs: Any) -> None:
2627
super(TournamentCreateForm, self).__init__(*args, **kwargs)
2728
self.fields["include_users"].label_from_instance = Submission.get_user_name
2829
self.fields["bye_player"].label_from_instance = Submission.get_user_name
2930

31+
qs = Submission.objects.latest().exclude(user=get_user_model().objects.get(username="Yourself"))
32+
33+
self.fields["include_users"].queryset = qs
34+
self.fields["bye_player"].queryset = qs
35+
3036
def clean(self) -> None:
3137
cd = self.cleaned_data
3238
if cd["start_time"] < timezone.now():
3339
raise ValidationError("A Tournament cannot take place in the past!")
34-
if cd["include_users"].count() < 2:
35-
raise ValidationError("A Tournament must include at least 2 players!")
3640
if cd["include_users"].filter(user__username="Yourself").exists() or cd["bye_player"].user.username == "Yourself":
3741
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')
42+
if cd["include_users"].count() < 2:
43+
raise ValidationError("A Tournament must include at least 2 players!")
3844
if cd["include_users"].filter(id=cd["bye_player"].id).exists():
3945
raise ValidationError("The bye player cannot participate in the Tournament!")
4046
if cd["include_users"].filter(is_legacy=True).exists():
@@ -54,6 +60,7 @@ def __init__(self, tournament: Tournament, *args: Any, **kwargs: Any) -> None:
5460
super(TournamentManagementForm, self).__init__(*args, **kwargs)
5561
self.tournament = tournament
5662
self.status = "future" if tournament in Tournament.objects.filter_future() else "in_progress"
63+
qs = Submission.objects.latest().exclude(user=get_user_model().objects.get(username="Yourself"))
5764

5865
if self.status == "future":
5966
self.fields["remove_users"].queryset = tournament.include_users.all()
@@ -67,8 +74,8 @@ def __init__(self, tournament: Tournament, *args: Any, **kwargs: Any) -> None:
6774

6875
self.fields["num_rounds"] = forms.IntegerField(max_value=settings.MAX_ROUND_NUM, required=False)
6976
self.fields["game_time_limit"] = forms.IntegerField(min_value=1, max_value=15, required=False)
70-
self.fields["bye_user"] = forms.ModelChoiceField(queryset=Submission.objects.latest(), required=False)
71-
self.fields["add_users"] = forms.ModelMultipleChoiceField(queryset=Submission.objects.latest(), required=False)
77+
self.fields["bye_user"] = forms.ModelChoiceField(queryset=qs, required=False)
78+
self.fields["add_users"] = forms.ModelMultipleChoiceField(queryset=qs, required=False)
7279
self.fields["bye_user"].label_from_instance = Submission.get_game_name
7380
self.fields["add_users"].label_from_instance = Submission.get_game_name
7481
else:
@@ -87,18 +94,16 @@ def clean(self) -> None:
8794
if cd.get("add_users", False):
8895
if cd["add_users"].filter(user__username="Yourself").exists():
8996
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')
90-
9197
if cd.get("bye_user", False):
9298
if cd["add_users"].filter(id=cd["bye_user"].id).exists():
9399
raise ValidationError("The bye player cannot participate in the Tournament!")
94-
95100
if cd["add_users"].filter(is_legacy=True).exists():
96101
cd["using_legacy"] = True
97102

98103
if cd.get("bye_player", False):
99-
if self.tournament.include_users.filter(id=cd["bye_user"].id).exists():
100-
raise ValidationError("Cannot set a bye player that is already participating in the Tournament")
101104
if cd["bye_user"].user.username == "Yourself":
102105
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')
106+
if self.tournament.include_users.filter(id=cd["bye_user"].id).exists():
107+
raise ValidationError("Cannot set a bye player that is already participating in the Tournament")
103108
if cd["bye_user"].filter(is_legacy=True).exists():
104109
cd["using_legacy"] = True

0 commit comments

Comments
 (0)