Skip to content

Commit

Permalink
finalizes fixing team logic (next up: remove solo)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Dec 10, 2023
1 parent 55b08f4 commit f2d522f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
13 changes: 10 additions & 3 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def in_team(self) -> bool:
"""Returns True if the user is in a team for the current or upcoming hunt."""
return self.current_team is not None




def generate_hint_key():
return secrets.token_urlsafe(48)

Expand Down Expand Up @@ -166,7 +167,13 @@ class Team(models.Model):
related_name="teams", related_query_name="teams", through=TeamMembership
)
hunt = models.ForeignKey("Hunt", on_delete=models.CASCADE, related_name="teams")


def leave(self, member: User):
if self.members.filter(id=member.id).first() is None:
raise IndexError("User is not in team")
# remove the member
self.members.through.remove(member)

def update_current_qr_i(self, i: int):
self.current_qr_i = max(self.current_qr_i, i)
self.save()
Expand All @@ -184,7 +191,7 @@ def join(self, user: User):
return
if self.is_full:
raise IndexError("Team is full")
self.members.add(user)
self.members.through.add(user)

def invites(self):
return Invite.objects.filter(team=self)
Expand Down
20 changes: 5 additions & 15 deletions core/views/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def make(request):
raw.hunt = Hunt.current_hunt() or Hunt.next_hunt()
raw: Team = form.save()

raw.members.add(request.user)
if request.user.in_team:
team = Team.objects.get(id=request.user.current_team.id)
team.leave(request.user)
raw.join(request.user)
Invite.objects.get_or_create(
team=raw, code=generate_invite_code(), invites=0
)
Expand All @@ -76,20 +79,7 @@ def make(request):
else:
form = TeamMakeForm()
return render(request, "core/team_new.html", dict(form=form))


@login_required
@upcoming_hunt_required
@block_if_current_hunt
def solo(q: HttpRequest):
hunt_ = Hunt.current_hunt() or Hunt.next_hunt()
team_ = Team.objects.create(
solo=True, hunt=hunt_, name=f"{q.user.username}'s Solo Team"
)
team_.members.add(q.user)

return redirect(reverse("index"))



@login_required
@require_http_methods(["GET"])
Expand Down

0 comments on commit f2d522f

Please sign in to comment.