Skip to content

Commit 1ead8a2

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

3 files changed

Lines changed: 11 additions & 10 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: 9 additions & 9 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

@@ -27,14 +28,17 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
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+
yourself = get_user_model().objects.get(username="Yourself")
32+
33+
self.fields["include_users"].queryset = self.fields["include_users"].queryset.exclude(user=yourself)
34+
self.fields["bye_player"].queryset = self.fields["bye_player"].queryset.exclude(user=yourself)
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!")
3440
if cd["include_users"].count() < 2:
3541
raise ValidationError("A Tournament must include at least 2 players!")
36-
if cd["include_users"].filter(user__username="Yourself").exists() or cd["bye_player"].user.username == "Yourself":
37-
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')
3842
if cd["include_users"].filter(id=cd["bye_player"].id).exists():
3943
raise ValidationError("The bye player cannot participate in the Tournament!")
4044
if cd["include_users"].filter(is_legacy=True).exists():
@@ -54,6 +58,7 @@ def __init__(self, tournament: Tournament, *args: Any, **kwargs: Any) -> None:
5458
super(TournamentManagementForm, self).__init__(*args, **kwargs)
5559
self.tournament = tournament
5660
self.status = "future" if tournament in Tournament.objects.filter_future() else "in_progress"
61+
qs = Submission.objects.latest().exclude(user=get_user_model().objects.get(username="Yourself"))
5762

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

6873
self.fields["num_rounds"] = forms.IntegerField(max_value=settings.MAX_ROUND_NUM, required=False)
6974
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)
75+
self.fields["bye_user"] = forms.ModelChoiceField(queryset=qs, required=False)
76+
self.fields["add_users"] = forms.ModelMultipleChoiceField(queryset=qs, required=False)
7277
self.fields["bye_user"].label_from_instance = Submission.get_game_name
7378
self.fields["add_users"].label_from_instance = Submission.get_game_name
7479
else:
@@ -85,9 +90,6 @@ def clean(self) -> None:
8590
raise ValidationError("Number of rounds must be within 15-60 rounds")
8691

8792
if cd.get("add_users", False):
88-
if cd["add_users"].filter(user__username="Yourself").exists():
89-
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')
90-
9193
if cd.get("bye_user", False):
9294
if cd["add_users"].filter(id=cd["bye_user"].id).exists():
9395
raise ValidationError("The bye player cannot participate in the Tournament!")
@@ -98,7 +100,5 @@ def clean(self) -> None:
98100
if cd.get("bye_player", False):
99101
if self.tournament.include_users.filter(id=cd["bye_user"].id).exists():
100102
raise ValidationError("Cannot set a bye player that is already participating in the Tournament")
101-
if cd["bye_user"].user.username == "Yourself":
102-
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')
103103
if cd["bye_user"].filter(is_legacy=True).exists():
104104
cd["using_legacy"] = True

othello/settings/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
8888
}
8989
}
90+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
9091

9192
# Authentication
9293
AUTH_PASSWORD_VALIDATORS = [

0 commit comments

Comments
 (0)