first draft for team events

This commit is contained in:
Danhia 2021-12-17 19:02:19 +01:00
parent caf494ca98
commit 46dbc7182d
9 changed files with 232 additions and 9 deletions

View File

@ -1,5 +1,5 @@
from django.contrib import admin from django.contrib import admin
from .models import Event, Scores from .models import Event, Scores, EventPlayer, Team
#admin.site.register(Event) #admin.site.register(Event)
#admin.site.register(Scores) #admin.site.register(Scores)
@ -21,3 +21,21 @@ class score(admin.ModelAdmin):
search_fields = ['user__username', 'score', 'event__name'] search_fields = ['user__username', 'score', 'event__name']
# Register your models here. # 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']

View File

@ -1,4 +1,11 @@
from django import forms from django import forms
from .models import Team
class submit_flag(forms.Form): class submit_flag(forms.Form):
flag = forms.CharField(label="Flag", max_length=100) 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')

View File

@ -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)),
],
),
]

View File

@ -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'),
),
]

View File

@ -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'),
),
]

View File

@ -24,3 +24,21 @@ class Scores(models.Model):
last_submission_date = models.DateTimeField('Last Submission Date', default=timezone.now) last_submission_date = models.DateTimeField('Last Submission Date', default=timezone.now)
class Meta: class Meta:
ordering = ['-score', 'last_submission_date', 'user__username'] 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)

View File

@ -0,0 +1,60 @@
{% extends 'base.html' %}
{% block content %}
{% load i18n %}
<div class="row">
<div class="col-sm-12 col-md-9">
<div class="ctf-block">
<div class="ctf-head">
<h3>{{ event.name }}</h3>
<small>{% trans "This event start at" %} : {{ event.start_date }}</small>
</div>
<div class="ctf-footer">
{% if logged == True%}
{% if registered == False %}
<span class="message error-msg">{% trans "You need to be registered to the event." %}</span>
{% else %}
{% if notexist == True %}
<span class="message error-msg">{% trans "Team does not exist." %}</span>
{% elif wrongpwd == True %}
<span class="message error-msg">{% trans "Wrong password submited." %}</span>
{% endif %}
<h2>Create Team</h2>
<form method="post" action="{% url 'events:create_team' event_slug=event.slug %}" class="create-team-form">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" name="teamname" placeholder="{% trans "Team name" %} *" maxlength="150" required="" id="id_teamname"></br>
<input class="form-control" type="password" name="password" placeholder="{% trans "Password" %} *" required="" id="id_password"></br>
<!-- <input class="form-control" type="email" name="email" placeholder="pleasedontgivemy@private.infos*" required="" maxlength="254" id="id_email"></br> -->
<!-- <input class="form-control" type="url" name="portfolio_site" placeholder="{% trans "Personal website" %}"maxlength="200" id="id_portfolio_site"></br> -->
<input type="submit" name="" class="form-control" value="{% trans "Create Team" %}">
</div>
</form>
<h2>Join Team</h2>
<form method="post" action="{% url 'events:join_team' event_slug=event.slug %}" class="join-team-form">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" name="teamname" placeholder="{% trans "Team name" %} *" maxlength="150" required="" id="id_teamname"></br>
<input class="form-control" type="password" name="password" placeholder="{% trans "Password" %} *" required="" id="id_password"></br>
<!-- <input class="form-control" type="email" name="email" placeholder="pleasedontgivemy@private.infos*" required="" maxlength="254" id="id_email"></br> -->
<!-- <input class="form-control" type="url" name="portfolio_site" placeholder="{% trans "Personal website" %}"maxlength="200" id="id_portfolio_site"></br> -->
<input type="submit" name="" class="form-control" value="{% trans "Join Team" %}">
</div>
</form>
{% endif %}
{% else %}
<h4>{% trans "You need to be logged to access this event." %}</h4>
{% endif %}
</div>
</div>
</div>
<div class="d-none d-md-block col-10 col-md-3 right-sidebar">
<ul class="list-group">
<li class="list-group-item">{{ event.name }}</li>
<li class="list-group-item">{% trans "Start at" %} : {{ event.start_date }}</li>
<li class="list-group-item">{% trans "End at" %} : {{ event.end_date }}</li>
</ul>
</div>
</div>
{% endblock %}

View File

@ -9,5 +9,7 @@ urlpatterns = [
path('<str:event_slug>/challenge/<str:chall_slug>', views.chall_event_info, name='event_chall_info'), path('<str:event_slug>/challenge/<str:chall_slug>', views.chall_event_info, name='event_chall_info'),
path('pwd/<str:event_slug>', views.submit_pwd, name='submit_pwd'), path('pwd/<str:event_slug>', views.submit_pwd, name='submit_pwd'),
path('submitEventFlag/<str:event_slug>/<str:chall_slug>', views.submit_event_flag, name='submit_event_flag'), path('submitEventFlag/<str:event_slug>/<str:chall_slug>', views.submit_event_flag, name='submit_event_flag'),
path('subscribe/<str:event_slug>', views.subscribe_to_event, name='subscribe_event') path('subscribe/<str:event_slug>', views.subscribe_to_event, name='subscribe_event'),
path('create_team/<str:event_slug>', views.create_team, name='create_team'),
path('join_team/<str:event_slug>', views.join_team, name='join_team'),
] ]

View File

@ -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.decorators import login_required
from django.contrib.auth.models import timezone from django.contrib.auth.models import timezone
from .forms import submit_flag 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 ctfs.models import CTF, CTF_flags
from django.utils.translation import get_language from django.utils.translation import get_language
@ -71,12 +71,13 @@ def event(request, event_slug):
subisover = True subisover = True
if request.user.is_authenticated: if request.user.is_authenticated:
userScore = Scores.objects.filter(event=event_info, user=request.user) 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 IsRegistered = True
if event_info.password: if event_info.password:
if request.user.is_authenticated: if request.user.is_authenticated:
if request.user.is_staff is False: 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}) return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': True, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered})
else: 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})
@ -137,6 +138,7 @@ def submit_event_flag(request, event_slug, chall_slug):
@login_required @login_required
def submit_pwd(request, event_slug): def submit_pwd(request, event_slug):
response = redirect('events:event_info', event_slug=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.method == 'POST':
if request.user.is_authenticated: if request.user.is_authenticated:
ev = get_object_or_404(Event, slug=event_slug) ev = get_object_or_404(Event, slug=event_slug)
@ -148,13 +150,16 @@ def submit_pwd(request, event_slug):
response['Location'] += '?WrongPassword=1' response['Location'] += '?WrongPassword=1'
return response 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' response['Location'] += '?AlreadyRegistered=1'
return response return response
else: 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() 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 @login_required
def subscribe_to_event(request, event_slug): 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 = Scores(user=request.user, event=ev, score=0)
new.save() new.save()
return redirect('events:event_info', event_slug=event_slug) 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)