Skip to content

Commit

Permalink
Revert "rework internal implementation of joining a contest solo."
Browse files Browse the repository at this point in the history
This reverts commit f0b93b9.
  • Loading branch information
JasonLovesDoggo committed Apr 5, 2024
1 parent dccd04a commit a044302
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 25 deletions.
20 changes: 13 additions & 7 deletions gameserver/models/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def is_visible_by(self, user):
if not user.is_authenticated:
return False

if self.organizations.exists() and self.organizations.filter(pk__in=user.organizations.all()).exists():
if self.organizations.filter(pk__in=user.organizations.all()).exists():
return True

return self.is_editable_by(user)
Expand All @@ -176,7 +176,7 @@ def is_accessible_by(self, user):
if not self.is_ongoing:
return False

if self.organizations.exists() and self.organizations.filter(pk__in=user.organizations.all()).exists():
if self.organizations.filter(pk__in=user.organizations.all()).exists():
return True

if self.is_editable_by(user):
Expand Down Expand Up @@ -241,19 +241,25 @@ def __init__(self, *args, **kwargs):
"Team",
on_delete=models.CASCADE,
related_name="contest_participations",
null=True,
blank=True,
)

@property
def name(self):
return self.team.name
participants = models.ManyToManyField("User", related_name="contest_participations", blank=True)

def __str__(self):
return f"{self.name}'s Participation in {self.contest.name}"
return f"{self.participant}'s Participation in {self.contest.name}"

def get_absolute_url(self):
return reverse("contest_participation_detail", args=[self.pk])

@cached_property
def participant(self):
if self.team is None:
return self.participants.first()
else:
return self.team

def _get_unique_correct_submissions(self):
# Switch to ContestProblem -> Problem Later
return (
Expand Down Expand Up @@ -400,7 +406,7 @@ class ContestSubmission(models.Model):
)

def __str__(self):
return f"{self.participation.name}'s submission for {self.problem.problem.name} in {self.problem.contest.name}"
return f"{self.participation.participant}'s submission for {self.problem.problem.name} in {self.problem.contest.name}"

@cached_property
def is_correct(self):
Expand Down
3 changes: 1 addition & 2 deletions gameserver/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def has_firstblooded(self, problem):
raise TypeError("problem must be a Problem or ContestProblem")

def participation_for_contest(self, contest):
return ContestParticipation.objects.filter(team__members=self.id, contest=contest).first()
return ContestParticipation.objects.filter(participants=self, contest=contest).first()

def remove_contest(self):
self.current_contest = None
Expand All @@ -181,7 +181,6 @@ def update_contest(self):
update_contest.alters_data = True



class Organization(models.Model):
owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name="organizations_owning")
admins = models.ManyToManyField("User", related_name="organizations_maintaining")
Expand Down
24 changes: 14 additions & 10 deletions gameserver/views/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def form_valid(self, form):
team = form.cleaned_data["participant"]

if (
self.request.in_contest
self.request.user.in_contest
and self.request.user.current_contest.contest == self.object
):
contest_participation = models.ContestParticipation.objects.get_or_create(
Expand All @@ -94,13 +94,17 @@ def form_valid(self, form):
team=team, contest=self.object
)[0]
else:
new_team = models.Team.objects.create(
name=self.request.user.username,
owner=self.request.user,
)
contest_participation = models.ContestParticipation.objects.get_or_create(
team=new_team, contest=self.object
)[0]
try:
contest_participation = models.ContestParticipation.objects.get(
team=None,
participants=self.request.user,
contest=self.object,
)
except models.ContestParticipation.DoesNotExist:
contest_participation = models.ContestParticipation(contest=self.object)
contest_participation.save()

contest_participation.participants.add(self.request.user)

self.request.user.current_contest = contest_participation
self.request.user.save()
Expand All @@ -109,14 +113,14 @@ def form_valid(self, form):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user: models.User = self.request.user
user = self.request.user
if user.is_authenticated:
if (data := cache.get(f"contest_{self.object.slug}_participant_count")) is None:
context["user_accessible"] = self.object.is_accessible_by(user)
context["user_participation"] = user.participation_for_contest(self.object)
team_participant_count = user.teams.annotate(
participant_count=Count(
"contest_participations__team",
"contest_participations__participants",
filter=Q(contest_participations__contest=self.object),
)
).values_list("pk", "participant_count")
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<h4 class="alert-heading" id="contestCountdown">Loading countdown...</h4>
<p class="mb-0">Currently participating in <a
href="{{ request.participation.contest.get_absolute_url }}">{{ request.participation.contest }}</a>
as <a href="{{ request.participation.get_absolute_url }}">{{ request.participation.name }}</a>.
as <a href="{{ request.participation.get_absolute_url }}">{{ request.participation.participant }}</a>.
</p>
</div>
{% endif %}
Expand Down
3 changes: 2 additions & 1 deletion templates/contest/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h5>{{ contest.duration }} long</h5>
{% if request.user.current_contest.contest == contest %}
<p>
You are currently in this contest as <a
href="{{ request.participation.team.get_absolute_url }}">{{ request.participation.team }}</a>.
href="{{ request.participation.participant.get_absolute_url }}">{{ request.participation.participant }}</a>.
</p>
{% else %}
<p>
Expand Down Expand Up @@ -108,6 +108,7 @@ <h5>{{ contest.duration }} long</h5>
{% bootstrap_field form.participant wrapper_class="mb-2" %}
<p id="team-size-display" class="text-muted mb-2"
style="font-size: 0.75rem;line-height:1.6"></p>
Disabled for performance reasons
<p class="text-muted fst-italic" style="font-size: 0.75rem;line-height:1.6">Once you
participate as part of
a team, you cannot change back to individual participation.</p>
Expand Down
4 changes: 2 additions & 2 deletions templates/contest/participation.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<div class="card mb-3">
<div class="card-body">
<h1 class="mt-0"><a
href="{{ participation.team.get_absolute_url }}">{{ participation.team }}</a>'s
href="{{ participation.participant.get_absolute_url }}">{{ participation.participant }}</a>'s
Participation in <a href="{{ participation.contest.get_absolute_url }}">{{ participation.contest }}</a>
</h1>
<hr class="mb-0">
</div>
</div>
{% include "snippets/group_detail/members.html" with member_list=participation.team.members.all entity="participation" %}
{% include "snippets/group_detail/members.html" with member_list=participation.participants.all entity="participation" %}
<div class="card mb-3 participation-problems">
<h2 class="card-header fs-6">Captured</h2>
<div class="card-body pt-3">
Expand Down
2 changes: 1 addition & 1 deletion templates/contest/snippet/submission/row.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% endif %}
{% if not hide_participant %}
<td>
<a href="{{ contest_submission.participation.get_absolute_url }}">{{ contest_submission.participation.name }}</a>
<a href="{{ contest_submission.participation.get_absolute_url }}">{{ contest_submission.participation.participant }}</a>
</td>
{% endif %}
{% if not hide_timestamp %}
Expand Down
2 changes: 1 addition & 1 deletion templates/contest/submission_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block heading %}
{% if contest_participation %}
Submissions by
<a href="{{ contest_participation.get_absolute_url }}">{{ contest_participation.name }}</a> in
<a href="{{ contest_participation.get_absolute_url }}">{{ contest_participation.participant }}</a> in
<a href="{{ contest.get_absolute_url }}">{{ contest }}</a>
{% else %}
Submissions for <a href="{{ contest.get_absolute_url }}">{{ contest }}</a>
Expand Down

0 comments on commit a044302

Please sign in to comment.