Skip to content

Commit

Permalink
exclude Yourself player from Tournament set
Browse files Browse the repository at this point in the history
  • Loading branch information
abagali1 committed Aug 4, 2022
1 parent 966f92b commit 1ead8a2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The Othello server uses Ion OAuth, you will need to register an application [her
* Acceptable hosts can be found in the `ALLOWED_HOSTS` list in `othello/settings/__init__.py`
* url must be inputted exactly or OAuth will fail

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.
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.

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`

Expand Down
18 changes: 9 additions & 9 deletions othello/apps/tournaments/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.utils import timezone

Expand All @@ -27,14 +28,17 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.fields["include_users"].label_from_instance = Submission.get_user_name
self.fields["bye_player"].label_from_instance = Submission.get_user_name

yourself = get_user_model().objects.get(username="Yourself")

self.fields["include_users"].queryset = self.fields["include_users"].queryset.exclude(user=yourself)
self.fields["bye_player"].queryset = self.fields["bye_player"].queryset.exclude(user=yourself)

def clean(self) -> None:
cd = self.cleaned_data
if cd["start_time"] < timezone.now():
raise ValidationError("A Tournament cannot take place in the past!")
if cd["include_users"].count() < 2:
raise ValidationError("A Tournament must include at least 2 players!")
if cd["include_users"].filter(user__username="Yourself").exists() or cd["bye_player"].user.username == "Yourself":
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')
if cd["include_users"].filter(id=cd["bye_player"].id).exists():
raise ValidationError("The bye player cannot participate in the Tournament!")
if cd["include_users"].filter(is_legacy=True).exists():
Expand All @@ -54,6 +58,7 @@ def __init__(self, tournament: Tournament, *args: Any, **kwargs: Any) -> None:
super(TournamentManagementForm, self).__init__(*args, **kwargs)
self.tournament = tournament
self.status = "future" if tournament in Tournament.objects.filter_future() else "in_progress"
qs = Submission.objects.latest().exclude(user=get_user_model().objects.get(username="Yourself"))

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

self.fields["num_rounds"] = forms.IntegerField(max_value=settings.MAX_ROUND_NUM, required=False)
self.fields["game_time_limit"] = forms.IntegerField(min_value=1, max_value=15, required=False)
self.fields["bye_user"] = forms.ModelChoiceField(queryset=Submission.objects.latest(), required=False)
self.fields["add_users"] = forms.ModelMultipleChoiceField(queryset=Submission.objects.latest(), required=False)
self.fields["bye_user"] = forms.ModelChoiceField(queryset=qs, required=False)
self.fields["add_users"] = forms.ModelMultipleChoiceField(queryset=qs, required=False)
self.fields["bye_user"].label_from_instance = Submission.get_game_name
self.fields["add_users"].label_from_instance = Submission.get_game_name
else:
Expand All @@ -85,9 +90,6 @@ def clean(self) -> None:
raise ValidationError("Number of rounds must be within 15-60 rounds")

if cd.get("add_users", False):
if cd["add_users"].filter(user__username="Yourself").exists():
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')

if cd.get("bye_user", False):
if cd["add_users"].filter(id=cd["bye_user"].id).exists():
raise ValidationError("The bye player cannot participate in the Tournament!")
Expand All @@ -98,7 +100,5 @@ def clean(self) -> None:
if cd.get("bye_player", False):
if self.tournament.include_users.filter(id=cd["bye_user"].id).exists():
raise ValidationError("Cannot set a bye player that is already participating in the Tournament")
if cd["bye_user"].user.username == "Yourself":
raise ValidationError('The "Yourself" player cannot participate in Tournaments!')
if cd["bye_user"].filter(is_legacy=True).exists():
cd["using_legacy"] = True
1 change: 1 addition & 0 deletions othello/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Authentication
AUTH_PASSWORD_VALIDATORS = [
Expand Down

0 comments on commit 1ead8a2

Please sign in to comment.