def event_signup(request, year, event_pk): event = get_object_or_404(Event, pk=event_pk) if not event.published: raise Http404 payment_url = reverse('events_api:payment', args=[event_pk]) signup_url = reverse('events_api:signup', args=[event_pk]) # Will be populated if user has completed signup before participant = Participant.objects.filter(user_s=request.user, event=event).first() open_student_teams = Team.objects.filter(event=event, allow_join_s=True) react_props = { 'event': serializers.event(event, request), 'teams': [serializers.team(team) for team in open_student_teams], 'payment_url': payment_url, 'signup_url': signup_url, 'stripe_publishable': settings.STRIPE_PUBLISHABLE, 'participant': serializers.participant(participant) if participant else None } return render( request, 'events/event_signup.html', { 'event': event, 'participant': participant, 'react_props': json.dumps(react_props, cls=DjangoJSONEncoder) })
def join_team(request, event_pk, team_pk): """ Endpoint to join teams """ event = get_object_or_404(Event, pk=event_pk) team = get_object_or_404(Team, pk=team_pk) if not request.user: return JsonResponse({'message': 'Authentication required.'}, status=403) if team.is_full(): return JsonResponse({'message': 'Team is full.'}, status=400) participant = Participant.objects.get(user_s=request.user, event=event) TeamMember.objects.update_or_create(participant=participant, defaults={ 'team': team, 'leader': False }) teams = Team.objects.filter(event=event, allow_join_s=True).all() return JsonResponse({'teams': [serializers.team(team) for team in teams]}, status=201)
def leave_team(request, event_pk): """ Endpoint to leave teams for the event """ event = get_object_or_404(Event, pk=event_pk) if not request.user: return JsonResponse({'message': 'Authentication required.'}, status=403) Participant.objects.get(user_s=request.user, event=event).teammember_set.all().delete() teams = Team.objects.filter(event=event, allow_join_s=True).all() return JsonResponse({'teams': [serializers.team(team) for team in teams]}, status=200)