From ff657c070a6869cf95955952b828156b4d7470a8 Mon Sep 17 00:00:00 2001 From: Arthur-TRT Date: Mon, 30 May 2022 10:25:18 +0200 Subject: [PATCH] Limit events to certain campus --- src/events/migrations/0010_event_campus.py | 19 +++++++++ .../migrations/0011_alter_event_campus.py | 19 +++++++++ src/events/models.py | 5 ++- src/events/templates/events/event_pwd.html | 42 +++++++++++++------ src/events/views/events.py | 24 ++++++++--- 5 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 src/events/migrations/0010_event_campus.py create mode 100644 src/events/migrations/0011_alter_event_campus.py diff --git a/src/events/migrations/0010_event_campus.py b/src/events/migrations/0010_event_campus.py new file mode 100644 index 0000000..9914495 --- /dev/null +++ b/src/events/migrations/0010_event_campus.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.11 on 2022-05-30 07:30 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0011_migration_campus'), + ('events', '0009_auto_20220215_1706'), + ] + + operations = [ + migrations.AddField( + model_name='event', + name='campus', + field=models.ManyToManyField(blank=True, null=True, to='accounts.Campus'), + ), + ] diff --git a/src/events/migrations/0011_alter_event_campus.py b/src/events/migrations/0011_alter_event_campus.py new file mode 100644 index 0000000..2c89d81 --- /dev/null +++ b/src/events/migrations/0011_alter_event_campus.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.11 on 2022-05-30 07:30 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0011_migration_campus'), + ('events', '0010_event_campus'), + ] + + operations = [ + migrations.AlterField( + model_name='event', + name='campus', + field=models.ManyToManyField(blank=True, to='accounts.Campus'), + ), + ] diff --git a/src/events/models.py b/src/events/models.py index 6b8b0b0..20adb61 100644 --- a/src/events/models.py +++ b/src/events/models.py @@ -2,6 +2,8 @@ from django.db import models from django.contrib.auth.models import User from django.contrib.auth.models import timezone import uuid +from accounts.models import Campus +from scoreboard.views import campus # Create your models here. class Event(models.Model): @@ -17,6 +19,7 @@ class Event(models.Model): team_size = models.PositiveIntegerField(default=1) auto_match = models.BooleanField(default=False) dynamic = models.BooleanField(default=False) + campus = models.ManyToManyField(Campus, blank=True) def __str__(self): return self.name @@ -28,7 +31,7 @@ class Team(models.Model): last_submission_date = models.DateTimeField('Last Submission Date', default=timezone.now) auto = models.BooleanField(default=False) def __str__(self): - return self.name + return self.name class EventPlayer(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) diff --git a/src/events/templates/events/event_pwd.html b/src/events/templates/events/event_pwd.html index 63987d6..9528e6d 100644 --- a/src/events/templates/events/event_pwd.html +++ b/src/events/templates/events/event_pwd.html @@ -8,22 +8,38 @@

{{ event.name }}

{% trans "This event start at" %} : {{ event.start_date }} - + diff --git a/src/events/views/events.py b/src/events/views/events.py index 204e744..063e381 100644 --- a/src/events/views/events.py +++ b/src/events/views/events.py @@ -1,9 +1,11 @@ +from pprint import pprint from django.shortcuts import render, get_object_or_404, redirect from django.contrib.auth.decorators import login_required from django.contrib.auth.models import timezone from ..forms import submit_flag from ..models import Event, EventPlayer, Team from ctfs.models import CTF, CTF_flags, Category +from accounts.models import UserProfileInfo from django.utils.translation import get_language from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ @@ -113,9 +115,19 @@ def event(request, event_slug): if request.user.is_authenticated: if request.user.is_staff is False: if not player: - return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': True, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered}) + return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': True, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered, 'userHasCampus': True, 'campusCanJoin': True}) else: - return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': False, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered}) + return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': False, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered, 'userHasCampus': True, 'campusCanJoin': True}) + if event_info.campus.all(): + if request.user.is_authenticated: + if request.user.is_staff is False: + user = UserProfileInfo.objects.get(user=request.user) + if user.campus is None: + return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': True, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered, 'userHasCampus': False, 'campusCanJoin': True}) + elif user.campus not in event_info.campus.all(): + return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': True, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered, 'userHasCampus': True, 'campusCanJoin': False}) + else: + return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': False, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered, 'userHasCampus': True, 'campusCanJoin': True}) ended = False if timezone.now() >= event_info.end_date: ended = True @@ -127,7 +139,7 @@ def event(request, event_slug): solved_list = EventPlayer.objects.filter(event=event_info).order_by('-score', 'last_submission_date', 'user__username') else: solved_list = Team.objects.filter(event=event_info).order_by('-score', 'last_submission_date', 'name') - return render(request, 'events/event_info.html', {'event' : event_info, 'IsRegistered': IsRegistered, 'ctfs': challenges, 'solved_list':solved_list, + return render(request, 'events/event_info.html', {'event' : event_info, 'IsRegistered': IsRegistered, 'ctfs': challenges, 'solved_list':solved_list, 'ended': ended, 'begun': begun, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered, 'subisover': subisover}) @login_required @@ -145,12 +157,12 @@ def submit_event_flag(request, event_slug, chall_slug): if not ctf_info: response['Location'] += '?ChallengeNotFound=1' return response - + try: player = EventPlayer.objects.get(event=ev, user=request.user) except: player = None - + if player: if ev.team_size > 1 and player.team is None: response['Location'] += '?NoTeam=1' @@ -219,7 +231,7 @@ def submit_pwd(request, event_slug): new = EventPlayer(user=request.user, event=ev) new.save() return redirect('events:event_info', event_slug=event_slug) - + @login_required def register_to_event(request, event_slug):