diff --git a/core/models.py b/core/models.py index fc0161f..a013a94 100644 --- a/core/models.py +++ b/core/models.py @@ -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) @@ -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() @@ -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) diff --git a/core/views/team.py b/core/views/team.py index 62d94b6..bb65650 100644 --- a/core/views/team.py +++ b/core/views/team.py @@ -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 ) @@ -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"])