Skip to content

Commit

Permalink
Updated usages to support joining a team before the event started
Browse files Browse the repository at this point in the history
feat: added a @upcoming_hunt_required decorator that confirms .upcoming or .current exists (todo cache?)
  • Loading branch information
JasonLovesDoggo committed Dec 4, 2023
1 parent 4e0506e commit 5dc0256
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
5 changes: 2 additions & 3 deletions core/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@


def start(request):
hunt = Hunt.current_hunt()
now = timezone.now()
if hunt is not None:
if hunt := Hunt.current_hunt() is not None:
event_start = hunt.start
event_end = hunt.end
elif Hunt.next_hunt() is not None:
Expand All @@ -17,7 +16,7 @@ def start(request):
else:
print("WARNING: No hunt is scheduled")
print("Please add a hunt in the future to the database")
event_start = timezone.now() + datetime.timedelta(weeks=75)
event_start = now + datetime.timedelta(weeks=75)
event_end = event_start + datetime.timedelta(hours=3)

return dict(
Expand Down
24 changes: 22 additions & 2 deletions core/views/qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ def wrapped(*args, **kwargs):

return wrapped

def upcoming_hunt_required(f):
@wraps(f)
def wrapped(*args, **kwargs):
request = args[0]
if Hunt.current_hunt() is None and Hunt.next_hunt() is None:
messages.error(
request,
_("No hunts are in the database, please contact an admin.")
)
return redirect(reverse("index"))
return f(*args, **kwargs) # todo: maybe return the hunt object to be used in the view? (more efficient)

return wrapped

def during_hunt(f):
"""
Expand All @@ -45,10 +58,17 @@ def during_hunt(f):
"""

@wraps(f)
@upcoming_hunt_required
def wrapped(*args, **kwargs):
hunt_ = Hunt.current_hunt()

hunt_ = Hunt.current_hunt() or Hunt.next_hunt()
request = args[0]
if hunt_ is None:
messages.error(
request,
_("No hunt is currently active."),
)
return redirect(reverse("index"))

if any(
[
hunt_.started and not hunt_.ended,
Expand Down
17 changes: 11 additions & 6 deletions core/views/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

from ..models import Team, Invite, generate_invite_code, Hunt
from ..forms import TeamJoinForm, TeamMakeForm
from .qr import team_required
from .qr import team_required, upcoming_hunt_required


@login_required
@require_http_methods(["GET", "POST"])
@upcoming_hunt_required
def join(request):
hunt_ = Hunt.current_hunt()
hunt_ = Hunt.current_hunt() or Hunt.next_hunt()
if hunt_.started and request.user.team is not None:
messages.error(
request,
Expand Down Expand Up @@ -61,9 +62,10 @@ def join(request):

@login_required
@require_http_methods(["GET", "POST"])
@upcoming_hunt_required
def make(request):
hunt_ = Hunt.current_hunt()
if hunt_.start < datetime.datetime.now() and request.user.team is not None:
hunt_ = Hunt.current_hunt() or Hunt.next_hunt()
if hunt_.started and request.user.team is not None:
messages.error(
request,
_("Since the hunt has already begun, making new teams is disallowed."),
Expand All @@ -73,7 +75,7 @@ def make(request):
form = TeamMakeForm(request.POST)
if form.is_valid():
raw: Team = form.save(commit=False)
raw.hunt = Hunt.current_hunt()
raw.hunt = Hunt.current_hunt() or Hunt.next_hunt()
raw.save()
request.user.team = raw
request.user.save()
Expand All @@ -92,9 +94,11 @@ def make(request):


@login_required
@upcoming_hunt_required
def solo(q: HttpRequest):
hunt_ = Hunt.current_hunt() or Hunt.next_hunt()
team = Team.objects.create(
solo=True, hunt=Hunt.current_hunt(), name=f"{q.user.username}'s Solo Team"
solo=True, hunt=hunt_, name=f"{q.user.username}'s Solo Team"
)
q.user.team = team
q.user.save()
Expand All @@ -104,6 +108,7 @@ def solo(q: HttpRequest):
@login_required
@require_http_methods(["GET"])
@team_required
@upcoming_hunt_required # redundant
def invite(q):
invites = Invite.objects.filter(team=q.user.team).values_list("code", flat=True)
if invites.count() == 0:
Expand Down

0 comments on commit 5dc0256

Please sign in to comment.