it's possible to modify and leave team

This commit is contained in:
Danhia 2022-01-02 20:42:10 +01:00
parent 411896bf01
commit 7ae36e2cee
7 changed files with 157 additions and 12 deletions

View File

@ -9,3 +9,12 @@ class create_team(forms.ModelForm):
class Meta():
model = Team
fields = ('name','password')
class TeamUpdateForm(forms.ModelForm):
class Meta:
model = Team
fields=('name', 'password',)
def __init__(self, *args, **kwargs):
super(TeamUpdateForm, self).__init__(*args, **kwargs)
for key in self.fields:
self.fields[key].required = True

View File

@ -20,6 +20,8 @@
<span class="message error-msg">{% trans "Wrong password submited." %}</span>
{% elif max == True %}
<span class="message error-msg">{% trans "Maximum size reached." %}</span>
{% elif exist == True %}
<span class="message error-msg">{% trans "Name already taken." %}</span>
{% endif %}
<h2>Create Team</h2>
<form method="post" action="{% url 'events:create_team' event_slug=event.slug %}" class="create-team-form">

View File

@ -108,8 +108,8 @@
<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>
<li class="list-group-item">{% trans "Starts at" %} : {{ event.start_date }}</li>
<li class="list-group-item">{% trans "Ends at" %} : {{ event.end_date }}</li>
{% if ended == False and IsRegistered == False %}
<form method='POST' action="{% url 'events:subscribe_event' event_slug=event.slug %}">
{%csrf_token%}
@ -119,6 +119,16 @@
</form>
{% endif %}
</ul>
{% if event.team_size > 1 and IsRegistered == True and ended == False %}
<ul class="list-group">
<form method='GET' action="{% url 'events:manage_team' event_slug=event.slug %}">
{%csrf_token%}
<li class="list-group-item">
<input class="form-control" type="submit" value="{% trans "Manage my team" %}">
</li>
</form>
</ul>
{% endif %}
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,59 @@
{% extends 'base.html' %}
{% block content %}
{% load i18n %}
<div class="row">
<div class="col-sm-12 col-md-9">
<div class="ctf-block">
<a href="{% url 'events:event_info' event_slug=player.event.slug %}">< Back to event</a>
<div class="ctf-head">
<h3>Edit info</h3>
</div>
<div class="bloc-body">
<div class="col-sm-12 col-md-12 mx-auto">
<!-- {{ u_form.non_field_errors }} -->
{% if error is not None %}
<span class="message error-msg">{{ error }}</span>
{% elif success is not None %}
<span class="message success-msg">{{ success }}</span>
{% endif %}
<form method='POST'>
<div class="edit-infos-grp">
{%csrf_token%}
<label for="{{ p_form.name.id_for_label }}">{% trans "Team name" %} *</label>
{{ p_form.name.errors}}
{{p_form.name}}
</br>
<label for="{{ p_form.password.id_for_label }}">{% trans "Team password" %} *</label>
{{p_form.password}}
</br>
<input class="form-control" type="submit" value="{% trans "Apply" %}">
</div>
</form>
</div>
</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">{{ player.team.name }}</li>
<li class="list-group-item">{% trans "Score" %} : {{ player.team.score }}</li>
<!-- <li class="list-group-item">{% trans "Rank" %} : {{ rank }}</li> -->
</ul>
<ul class="list-group">
<li class="list-group-item">{% trans "Members" %}</li>
{% for p in members %}
<li class="list-group-item"><a class="profile_link" href="{% url 'accounts:profile' user_name=p.user.username %}">{{ p.user.username }}</a></li>
{% endfor %}
<form method='POST' action="{% url 'events:leave_team' event_slug=player.event.slug %}">
{%csrf_token%}
<li class="list-group-item">
<input class="form-control" type="submit" value="{% trans "Leave Team" %}">
</li>
</form>
</ul>
</div>
</div>
{% endblock %}

View File

@ -4,8 +4,9 @@
{% load key_value %}
<div class="row">
<div class="col-sm-12 col-md-9">
<a href="{% url 'events:event_info' event_slug=event.slug %}">< Back to event</a>
<div>
<h4>Challenges Solved by {{ team.name }}</h4>
<h4>Challenges Solved by {{ team.name }} - {{ event.name }}</h4>
{% if solves%}
<div class="table table-dark">
@ -25,7 +26,7 @@
<tbody>
{% for s in solves %}
<tr>
<th scope="row"><a href="{% url 'ctf' cat_slug=s.ctf.category.slug ctf_slug=s.ctf.slug %}">{{ s.ctf.name }}</a></th>
<th scope="row"><a href="{% url 'events:event_chall_info' event_slug=event.slug chall_slug=s.ctf.slug %}">{{ s.ctf.name }}</a></th>
<td>{{ s.ctf.category.name}}</td>
<td>{{ s.ctf.points }}</td>
<td>{{ s.flag_date|date:"Y-m-d H:i:s" }}</td>

View File

@ -14,4 +14,6 @@ urlpatterns = [
path('join_team/<str:event_slug>', views.join_team, name='join_team'),
path('<str:event_slug>/profile/<str:user_name>', views.profile, name='profile'),
path('<str:event_slug>/team/<str:name>', views.team_info, name='team_info'),
path('<str:event_slug>/manage_team', views.manage_team, name='manage_team'),
path('<str:event_slug>/leave_team', views.leave_team, name='leave_team'),
]

View File

@ -1,13 +1,14 @@
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 .forms import submit_flag, TeamUpdateForm
from .models import Event, EventPlayer, Team
from ctfs.models import CTF, CTF_flags, Category
from django.utils.translation import get_language
from django.contrib.auth.models import User
from accounts.models import UserProfileInfo
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
def get_description_by_lang(ctf):
lang = get_language()
@ -78,7 +79,10 @@ def event(request, event_slug):
if request.GET.get('SubscriptionIsOver'):
subisover = True
if request.user.is_authenticated:
player = EventPlayer.objects.filter(event=event_info, user=request.user)
try:
player = EventPlayer.objects.get(event=event_info, user=request.user)
except:
player = None
if player:
IsRegistered = True
if event_info.password:
@ -86,6 +90,8 @@ def event(request, event_slug):
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})
elif not player.team and event_info.team_size > 1:
return render(request, 'events/create_team.html', {'event' : event_info, 'logged': True, 'wrongpwd': False, 'registered' : True, 'notexist' : False})
else:
return render(request, 'events/event_pwd.html', {'event' : event_info, 'logged': False, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered})
ended = False
@ -99,7 +105,8 @@ 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, 'ended': ended, 'begun': begun, 'wrongpwd': wrongpwd, 'alreadyregistered': alreadyregistered, 'subisover': subisover})
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
def submit_event_flag(request, event_slug, chall_slug):
@ -138,10 +145,12 @@ def submit_event_flag(request, event_slug, chall_slug):
if ctf_info.flag == request.POST.get('flag'):
new = CTF_flags(user = request.user, ctf = ctf_info, flag_date = timezone.now())
new.save()
if ctf_info.points > 0:
player.last_submission_date = timezone.now()
player.score += ctf_info.points
player.save()
if player.team:
if ctf_info.points > 0:
player.team.last_submission_date = timezone.now()
player.team.score += ctf_info.points
player.team.save()
@ -212,6 +221,8 @@ def create_team(request, event_slug):
if request.method == 'POST':
if request.user.is_authenticated:
ev = get_object_or_404(Event, slug=event_slug)
if Team.objects.filter(name=request.POST.get('teamname'), event=ev).exists():
return render(request, 'events/create_team.html', {'event' : ev, 'logged': True, 'wrongpwd': False, 'registered' : True, 'exist' : True})
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)
@ -327,3 +338,54 @@ def team_info(request, name, event_slug):
return render(request,'events/team.html', {'users':users, 'solves':solves,'solved':solved,'catsDatas': catsDatas, 'pointDatas': pointDatas,
'rank': rank, 'team':team, 'score':somme, 'event':event_info})
@login_required
def manage_team(request, event_slug):
event_info = get_object_or_404(Event, slug=event_slug)
player = EventPlayer.objects.get(user=request.user, event=event_info)
members = EventPlayer.objects.filter(team=player.team, event=event_info)
if request.method == 'POST':
tname = player.team.name
p_form = TeamUpdateForm(request.POST, instance=player.team)
error = None
success = None
if p_form.is_valid():
pname = p_form.cleaned_data['name']
if pname == tname:
pass
else:
if Team.objects.filter(name=pname, event=event_info).exists():
error = _("Name already taken.")
ppassword = p_form.cleaned_data['password']
if error is None:
p_form.save()
success = _("Updated.")
context={'p_form': p_form, 'error':error, 'success' : success, 'player':player, 'members':members}
return render(request, 'events/manage_team.html', context)
else:
p_form = TeamUpdateForm(instance=player.team)
context={'p_form': p_form, 'player':player, 'members':members}
return render(request, 'events/manage_team.html',context)
@login_required
def leave_team(request, event_slug):
event_info = get_object_or_404(Event, slug=event_slug)
player = EventPlayer.objects.get(user=request.user, event=event_info)
team = Team.objects.get(event=event_info, name=player.team.name)
team.score -= player.score
team.save()
player.team = None
solved = CTF_flags.objects.filter(user=player.user, ctf__event=event_info)
player.score = 0
solved.delete()
player.save()
members = EventPlayer.objects.filter(team=team, event=event_info)
if members.count() == 0:
team.delete()
return render(request, 'events/create_team.html', {'event' : event_info, 'logged': True, 'wrongpwd': False, 'registered' : True, 'notexist' : False})