Skip to content

Commit

Permalink
Change PARegistration form (#293) (#298)
Browse files Browse the repository at this point in the history
* Change PARegistration form (#293)

* Add checkbox to PARegistrationForm

* Change PARegistration model to allow null values

* Add logic to verify address only when needed

* Change PARegistrationForm

* Add javascript to form

* Add 'no_prizes' field to PARegistrationModel

---------

Co-authored-by: Mateusz Jacniacki <[email protected]>
  • Loading branch information
zggf-zggf and zggf-zggf authored Dec 20, 2023
1 parent 0468d92 commit e9faa28
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
2 changes: 2 additions & 0 deletions oioioi/pa/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@


class PARegistrationController(ParticipantsController):
registration_template = 'pa/registration.html'

@property
def form_class(self):
from oioioi.pa.forms import PARegistrationForm
Expand Down
24 changes: 24 additions & 0 deletions oioioi/pa/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,33 @@


class PARegistrationForm(forms.ModelForm):

class Meta(object):
model = PARegistration
exclude = ['participant']
fields = [
'job',
'job_name',
'no_prizes',
'address',
'postal_code',
'city',
't_shirt_size',
'newsletter',
'terms_accepted',
]

def clean(self):
cleaned_data = super().clean()
prize_fields_required = not cleaned_data.get('no_prizes')
prize_fields_error_msg = _("This field is required to be eligible for prizes.")
prize_fields = ['address', 'postal_code', 'city', 't_shirt_size']

for field in prize_fields:
if prize_fields_required and not cleaned_data.get(field):
self.add_error(field, prize_fields_error_msg)

return cleaned_data

def set_terms_accepted_text(self, terms_accepted_phrase):
if terms_accepted_phrase is None:
Expand Down
2 changes: 1 addition & 1 deletion oioioi/pa/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ class Migration(migrations.Migration):
},
bases=(models.Model,),
),
]
]
52 changes: 52 additions & 0 deletions oioioi/pa/migrations/0003_paregistration_no_prizes_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 4.2.6 on 2023-12-13 15:38

from django.db import migrations, models
import django.db.models.deletion
import oioioi.base.fields
import oioioi.participants.fields


class Migration(migrations.Migration):

dependencies = [
('participants', '0011_alter_onsiteregistration_participant_and_more'),
('pa', '0002_auto_20181117_1141'),
]

operations = [
migrations.AddField(
model_name='paregistration',
name='no_prizes',
field=models.BooleanField(default=False, verbose_name="I don't want to provide my address (opt out of prizes)"),
),
migrations.AlterField(
model_name='paregistration',
name='address',
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='address'),
),
migrations.AlterField(
model_name='paregistration',
name='city',
field=models.CharField(blank=True, max_length=100, null=True, verbose_name='city'),
),
migrations.AlterField(
model_name='paregistration',
name='job',
field=models.CharField(choices=[('PS', 'Szkoła podstawowa'), ('MS', 'Gimnazjum'), ('HS', 'Szkoła ponadgimnazjalna'), ('OTH', 'Inne'), ('AS', 'Szkoła wyższa - student'), ('AD', 'Szkoła wyższa - doktorant'), ('COM', 'Firma')], max_length=7, verbose_name='job or school kind'),
),
migrations.AlterField(
model_name='paregistration',
name='participant',
field=oioioi.participants.fields.OneToOneBothHandsCascadingParticipantField(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s', to='participants.participant'),
),
migrations.AlterField(
model_name='paregistration',
name='postal_code',
field=oioioi.base.fields.PostalCodeField(blank=True, null=True, verbose_name='postal code'),
),
migrations.AlterField(
model_name='paregistration',
name='t_shirt_size',
field=models.CharField(blank=True, choices=[('S', 'S'), ('M', 'M'), ('L', 'L'), ('XL', 'XL'), ('XXL', 'XXL')], max_length=7, null=True, verbose_name='t-shirt size'),
),
]
19 changes: 15 additions & 4 deletions oioioi/pa/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@


class PARegistration(RegistrationModel):
address = models.CharField(max_length=255, verbose_name=_("address"))
postal_code = PostalCodeField(verbose_name=_("postal code"))
city = models.CharField(max_length=100, verbose_name=_("city"))
address = models.CharField(
max_length=255, blank=True, null=True, verbose_name=_("address")
)
postal_code = PostalCodeField(null=True, blank=True, verbose_name=_("postal code"))
city = models.CharField(
max_length=100, blank=True, null=True, verbose_name=_("city")
)
job = models.CharField(
max_length=7, choices=JOB_TYPES, verbose_name=_("job or school kind")
)
job_name = models.CharField(max_length=255, verbose_name=_("job or school name"))
t_shirt_size = models.CharField(
max_length=7, choices=T_SHIRT_SIZES, verbose_name=_("t-shirt size")
max_length=7,
null=True,
blank=True,
choices=T_SHIRT_SIZES,
verbose_name=_("t-shirt size")
)
newsletter = models.BooleanField(
_("newsletter"),
Expand All @@ -47,6 +55,9 @@ class PARegistration(RegistrationModel):
# It is presented with the default verbose name in all contexts, except for
# the custom registration form (in contests like OI and PA)
terms_accepted = models.BooleanField(_("terms accepted"), default=False)
no_prizes = models.BooleanField(
_("I don't want to provide my address (opt out of prizes)"), default=False
)

def erase_data(self):
self.address = 'Account deleted'
Expand Down
21 changes: 21 additions & 0 deletions oioioi/pa/templates/pa/registration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "participants/registration.html" %}
{% load i18n %}

{% block scripts %}
{{ block.super }}
<script>
var prizeFieldsIDs = ["id_address", "id_postal_code", "id_city", "id_t_shirt_size"];

function togglePrizeFields() {
var hidePrizeFields = document.getElementById("id_no_prizes").checked;

prizeFieldsIDs.forEach(function (id) {
var input = document.getElementById(id);
var formGroup = input.closest(".form-group");
formGroup.style.display = hidePrizeFields ? "none" : "block";
});
}

document.getElementById("id_no_prizes").addEventListener("change", togglePrizeFields);
</script>
{% endblock %}

0 comments on commit e9faa28

Please sign in to comment.