Skip to content

Commit

Permalink
Merge pull request #146 from ZdruzenieSTROM/138-team-edit-form
Browse files Browse the repository at this point in the history
138 team edit form
  • Loading branch information
michalmasrna1 authored Sep 27, 2024
2 parents 9c88ded + 7bd0c80 commit ecd2780
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
9 changes: 8 additions & 1 deletion kos/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def __init__(self, *args, **kwargs):

class EditTeamForm(forms.Form):
"""Form na úpravu tímových údajov"""
team_name = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control main-input'}),
label='Názov tímu')
team_member_1 = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control main-input'}),
label='1. člen tímu')
Expand All @@ -146,4 +149,8 @@ class EditTeamForm(forms.Form):
widget=forms.TextInput(attrs={'class': 'form-control main-input'}),
label='5. člen tímu', required=False
)
is_online = forms.BooleanField(required=False)
is_online = forms.BooleanField(
required=False,
widget=forms.CheckboxInput(attrs={'class': 'checkbox-input'}),
label='Chcem riešiť online'
)
9 changes: 9 additions & 0 deletions kos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@ class Meta:
is_public = models.BooleanField(
verbose_name='Tím je verejný', default=True)

@property
def editable(self) -> bool:
"""
Vráti, či je ešte možné robiť zmeny v tíme.
Aktuálne tímom dovoľujeme meniť meno a členov tímu
do konca šifrovačky. Zákaz menenia toho, či je tím
online je implementovaný inde."""
return self.game.year.end > now()

def __str__(self):
return f'{self.name}'

Expand Down
11 changes: 11 additions & 0 deletions kos/templates/kos/change_profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ <h2>Zmena údajov tímu</h2>
{% else %}
<div class="answer-alert error">Platbu za súťaž zatiaľ neevidujeme.</div>
{% endif %}

<script>
// TODO: Solve this more properly and also check for this on backend
// Hack to remove the disable attribute of is_online field on submit
// Otherwise the value of is_online is not sent at post and the team
// is marked as offline upon e. g. changing their name after the
// registration ends but before the game concludes.
$("form").submit(function() {
$("input").removeAttr("disabled");
});
</script>
{% endblock %}
13 changes: 10 additions & 3 deletions kos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ def get(self, request, *args, **kwargs):
def get_initial(self):
team = self.get_team()
init_dict = {
'team_name': team.name,
'is_online': team.is_online
}
for i, member in enumerate(team.members.all()):
Expand All @@ -421,25 +422,31 @@ def get_context_data(self, **kwargs):
context['paid'] = (
team.paid if hasattr(team, 'paid') else False
)
context['disabled'] = team.game.year.registration_deadline < now()
context['disabled'] = not team.editable
return context

def post(self, request, *args, **kwargs):
team = self.get_team()
if team is None:
return redirect('kos:game')
if team.game.year.registration_deadline < now():
if not team.editable:
messages.error(
request, 'Tieto údaje nie je možné meniť po skončení registrácie')
return redirect('kos:change-profile')
# TODO: Check that the team did not change their is_online
# status after the registration deadline. Currently this
# is only disabled on frontend but can be bypassed.
return super().post(request, *args, **kwargs)

def get_form(self, form_class=None):
form = super().get_form(form_class)
team = self.get_team()
if team.game.year.registration_deadline < now():
if not team.editable:
for field in form.fields.values():
field.widget.attrs['disabled'] = True
# Po konci registracie uz nechceme timu dovolit menit ci je online
if team.game.year.registration_deadline < now():
form.fields['is_online'].widget.attrs['disabled'] = True
return form

def form_valid(self, form):
Expand Down

0 comments on commit ecd2780

Please sign in to comment.