From 46dbc7182d8bdff9ece2cd2dae3647fed80bf75b Mon Sep 17 00:00:00 2001 From: Danhia Date: Fri, 17 Dec 2021 19:02:19 +0100 Subject: [PATCH] first draft for team events --- src/events/admin.py | 20 ++++++- src/events/forms.py | 7 +++ .../migrations/0004_auto_20211215_2105.py | 42 +++++++++++++ .../migrations/0005_auto_20211215_2124.py | 19 ++++++ src/events/migrations/0006_team_event.py | 19 ++++++ src/events/models.py | 20 ++++++- src/events/templates/events/create_team.html | 60 +++++++++++++++++++ src/events/urls.py | 4 +- src/events/views.py | 50 ++++++++++++++-- 9 files changed, 232 insertions(+), 9 deletions(-) create mode 100644 src/events/migrations/0004_auto_20211215_2105.py create mode 100644 src/events/migrations/0005_auto_20211215_2124.py create mode 100644 src/events/migrations/0006_team_event.py create mode 100644 src/events/templates/events/create_team.html diff --git a/src/events/admin.py b/src/events/admin.py index 77ae37b..9c943eb 100644 --- a/src/events/admin.py +++ b/src/events/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from .models import Event, Scores +from .models import Event, Scores, EventPlayer, Team #admin.site.register(Event) #admin.site.register(Scores) @@ -21,3 +21,21 @@ class score(admin.ModelAdmin): search_fields = ['user__username', 'score', 'event__name'] # Register your models here. + +@admin.register(Team) +class team(admin.ModelAdmin): + #list display + list_display = ['name', 'password'] + #list Filter + # list_filter = ('event',) + # search list + search_fields = ['name'] + +@admin.register(EventPlayer) +class eventplayer(admin.ModelAdmin): + #list display + list_display = ['user', 'event', 'team'] + #list Filter + list_filter = ('event', 'team__name') + # search list + search_fields = ['user__username', 'event__name'] \ No newline at end of file diff --git a/src/events/forms.py b/src/events/forms.py index acdc47e..108700d 100644 --- a/src/events/forms.py +++ b/src/events/forms.py @@ -1,4 +1,11 @@ from django import forms +from .models import Team class submit_flag(forms.Form): flag = forms.CharField(label="Flag", max_length=100) + +class create_team(forms.ModelForm): + password = forms.CharField(widget=forms.PasswordInput()) + class Meta(): + model = Team + fields = ('name','password') \ No newline at end of file diff --git a/src/events/migrations/0004_auto_20211215_2105.py b/src/events/migrations/0004_auto_20211215_2105.py new file mode 100644 index 0000000..76b77fa --- /dev/null +++ b/src/events/migrations/0004_auto_20211215_2105.py @@ -0,0 +1,42 @@ +# Generated by Django 3.1.5 on 2021-12-15 21:05 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('events', '0003_auto_20211019_1519'), + ] + + operations = [ + migrations.CreateModel( + name='Team', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=200)), + ('password', models.CharField(max_length=200)), + ('size', models.PositiveIntegerField(default=1)), + ('score', models.PositiveIntegerField(db_index=True, default=0)), + ('last_submission_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Last Submission Date')), + ], + ), + migrations.AlterField( + model_name='scores', + name='id', + field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + migrations.CreateModel( + name='EventPlayer', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.event')), + ('team', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.team')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/src/events/migrations/0005_auto_20211215_2124.py b/src/events/migrations/0005_auto_20211215_2124.py new file mode 100644 index 0000000..8c6d03b --- /dev/null +++ b/src/events/migrations/0005_auto_20211215_2124.py @@ -0,0 +1,19 @@ +# Generated by Django 3.1.5 on 2021-12-15 21:24 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0004_auto_20211215_2105'), + ] + + operations = [ + migrations.AlterField( + model_name='eventplayer', + name='team', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='events.team'), + ), + ] diff --git a/src/events/migrations/0006_team_event.py b/src/events/migrations/0006_team_event.py new file mode 100644 index 0000000..fde2f2b --- /dev/null +++ b/src/events/migrations/0006_team_event.py @@ -0,0 +1,19 @@ +# Generated by Django 3.1.5 on 2021-12-15 22:16 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0005_auto_20211215_2124'), + ] + + operations = [ + migrations.AddField( + model_name='team', + name='event', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='events.event'), + ), + ] diff --git a/src/events/models.py b/src/events/models.py index ea8edea..7abf92c 100644 --- a/src/events/models.py +++ b/src/events/models.py @@ -23,4 +23,22 @@ class Scores(models.Model): score = models.PositiveIntegerField(default=0, db_index=True) last_submission_date = models.DateTimeField('Last Submission Date', default=timezone.now) class Meta: - ordering = ['-score', 'last_submission_date', 'user__username'] \ No newline at end of file + ordering = ['-score', 'last_submission_date', 'user__username'] + +class Team(models.Model): + name = models.CharField(max_length=200) + password = models.CharField(max_length=200) + event = models.ForeignKey(Event, on_delete=models.CASCADE, null=True) + size = models.PositiveIntegerField(default=1) + score = models.PositiveIntegerField(default=0, db_index=True) + last_submission_date = models.DateTimeField('Last Submission Date', default=timezone.now) + def __str__(self): + return self.name + +class EventPlayer(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + event = models.ForeignKey(Event, on_delete=models.CASCADE) + # score = models.PositiveIntegerField(default=0, db_index=True) + # last_submission_date = models.DateTimeField('Last Submission Date', default=timezone.now) + team = models.ForeignKey(Team, on_delete=models.CASCADE, null=True) + diff --git a/src/events/templates/events/create_team.html b/src/events/templates/events/create_team.html new file mode 100644 index 0000000..e62920b --- /dev/null +++ b/src/events/templates/events/create_team.html @@ -0,0 +1,60 @@ +{% extends 'base.html' %} +{% block content %} +{% load i18n %} +
+
+
+
+

{{ event.name }}

+ {% trans "This event start at" %} : {{ event.start_date }} +
+ + +
+
+ +
+{% endblock %} \ No newline at end of file diff --git a/src/events/urls.py b/src/events/urls.py index 1f8d495..91ce4e4 100644 --- a/src/events/urls.py +++ b/src/events/urls.py @@ -9,5 +9,7 @@ urlpatterns = [ path('/challenge/', views.chall_event_info, name='event_chall_info'), path('pwd/', views.submit_pwd, name='submit_pwd'), path('submitEventFlag//', views.submit_event_flag, name='submit_event_flag'), - path('subscribe/', views.subscribe_to_event, name='subscribe_event') + path('subscribe/', views.subscribe_to_event, name='subscribe_event'), + path('create_team/', views.create_team, name='create_team'), + path('join_team/', views.join_team, name='join_team'), ] diff --git a/src/events/views.py b/src/events/views.py index f3abe04..961c7b9 100644 --- a/src/events/views.py +++ b/src/events/views.py @@ -2,7 +2,7 @@ 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, Scores +from .models import Event, Scores, EventPlayer, Team from ctfs.models import CTF, CTF_flags from django.utils.translation import get_language @@ -71,12 +71,13 @@ def event(request, event_slug): subisover = True if request.user.is_authenticated: userScore = Scores.objects.filter(event=event_info, user=request.user) - if userScore: + player = EventPlayer.objects.filter(event=event_info, user=request.user) + if userScore or player: IsRegistered = True if event_info.password: if request.user.is_authenticated: if request.user.is_staff is False: - if not userScore: + if not userScore and not player: return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': True, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered}) else: return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': False, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered}) @@ -137,6 +138,7 @@ def submit_event_flag(request, event_slug, chall_slug): @login_required def submit_pwd(request, event_slug): response = redirect('events:event_info', event_slug=event_slug) + event_info = get_object_or_404(Event, slug=event_slug) if request.method == 'POST': if request.user.is_authenticated: ev = get_object_or_404(Event, slug=event_slug) @@ -148,13 +150,16 @@ def submit_pwd(request, event_slug): response['Location'] += '?WrongPassword=1' return response - if Scores.objects.filter(user=request.user, event=ev).exists(): + if Scores.objects.filter(user=request.user, event=ev).exists() or EventPlayer.objects.filter(user=request.user, event=ev).exists(): response['Location'] += '?AlreadyRegistered=1' return response else: - new = Scores(user=request.user, event=ev, score=0) + new = EventPlayer(user=request.user, event=ev) + # new = Scores(user=request.user, event=ev, score=0) new.save() - return redirect('events:event_info', event_slug=event_slug) + # return redirect('events:event_info', event_slug=event_slug) + return render(request, 'events/create_team.html', {'event' : event_info, 'logged': True, 'wrongpwd': False, 'registered' : True, 'notexist' : False}) + # return redirect('events:create_team', event_slug=event_slug) @login_required def subscribe_to_event(request, event_slug): @@ -175,3 +180,36 @@ def subscribe_to_event(request, event_slug): new = Scores(user=request.user, event=ev, score=0) new.save() return redirect('events:event_info', event_slug=event_slug) + +@login_required +def create_team(request, event_slug): + response = redirect('events:create_team', event_slug=event_slug) + if request.method == 'POST': + if request.user.is_authenticated: + ev = get_object_or_404(Event, slug=event_slug) + new = Team(name=request.POST.get('teamname'), password=request.POST.get('password'), event=ev) + new.save() + player = EventPlayer.objects.get(user=request.user, event=ev) + player.team = new + player.save() + return redirect('events:event_info', event_slug=event_slug) + +@login_required +def join_team(request, event_slug): + response = redirect('events:join_team', event_slug=event_slug) + if request.method == 'POST': + if request.user.is_authenticated: + ev = get_object_or_404(Event, slug=event_slug) + try: + team = Team.objects.get(name=request.POST.get('teamname'), event=ev) + except: + team = None + if team is None: + return render(request, 'events/create_team.html', {'event' : ev, 'logged': True, 'wrongpwd': True, 'registered' : True, 'notexist' : True}) + if request.POST.get('password') != team.password: + return render(request, 'events/create_team.html', {'event' : ev, 'logged': True, 'wrongpwd': True, 'registered' : True, 'notexist' : False}) + else: + player = EventPlayer.objects.get(user=request.user, event=ev) + player.team = team + player.save() + return redirect('events:event_info', event_slug=event_slug)