[go: up one dir, main page]

Skip to content

Commit

Permalink
refactored country form
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreloicq committed Mar 2, 2024
1 parent b2c99b7 commit 93c7cfc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 79 deletions.
2 changes: 1 addition & 1 deletion locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ msgstr "Appelle les États membres à introduire des prélèvements solidaires s

#: .\yvm\forms.py:6 .\yvm\templates\yvm\index.html:25
msgid "Select a country"
msgstr "Sélectionner un pays"
msgstr "Sélectionnez un pays"

#: .\yvm\forms.py:7 .\yvm\templates\yvm\index.html:26
msgid "Austria"
Expand Down
10 changes: 4 additions & 6 deletions yvm/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django import forms
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy as _

class CountrySelectionForm(forms.Form):
# just the values on the left are useful here, since the display form is in the html
# which was easier to style it with css and maybe to get instant country translations
country_choices = [
('', _('Select a country')),
('Austria', _('Austria')),
('Belgium', _('Belgium')),
('Bulgaria', _('Bulgaria')),
Expand Down Expand Up @@ -33,9 +34,6 @@ class CountrySelectionForm(forms.Form):
('Sweden', _('Sweden'))
]

country = forms.ChoiceField(choices=country_choices, required=True,
widget=forms.Select(attrs={#'class': 'form-select w-100',
'aria-label': 'select the country where you vote'})
)
country = forms.ChoiceField(choices=country_choices, required=True)


75 changes: 13 additions & 62 deletions yvm/templates/yvm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,86 +9,37 @@
<h2 class="title is-3 ">
{% translate "Be ready for the European elections of June 2024" %}
</h2>
<h1 class="title is-2">
<h1 class="title is-3 pb-6">
{% translate "Find out how the current Members of the European Parliament voted on 17 key recent votes" %}
</h1>
</div>
<div class="form_container">

<div class="form_container pb-6">
<form method="get">
{% csrf_token %}
<div class="field has-addons">
<div class="control">
<div class="select">
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}">
<select name="country" onchange="this.form.submit()">
<option value="selectCountry" disabled selected>{% translate 'Select a country' %}</option>
<option value="Austria">{% translate 'Austria' %}</option>
<option value="Belgium">{% translate 'Belgium' %}</option>
<option value="Bulgaria">{% translate 'Bulgaria' %}</option>
<option value="Croatia">{% translate 'Croatia' %}</option>
<option value="Cyprus">{% translate 'Cyprus' %}</option>
<option value="Czechia">{% translate 'Czechia' %}</option>
<option value="Denmark">{% translate 'Denmark' %}</option>
<option value="Estonia">{% translate 'Estonia' %}</option>
<option value="Finland">{% translate 'Finland' %}</option>
<option value="France">{% translate 'France' %}</option>
<option value="Germany">{% translate 'Germany' %}</option>
<option value="Greece">{% translate 'Greece' %}</option>
<option value="Hungary">{% translate 'Hungary' %}</option>
<option value="Ireland">{% translate 'Ireland' %}</option>
<option value="Italy">{% translate 'Italy' %}</option>
<option value="Latvia">{% translate 'Latvia' %}</option>
<option value="Lithuania">{% translate 'Lithuania' %}</option>
<option value="Luxembourg">{% translate 'Luxembourg' %}</option>
<option value="Malta">{% translate 'Malta' %}</option>
<option value="Netherlands">{% translate 'Netherlands' %}</option>
<option value="Poland">{% translate 'Poland' %}</option>
<option value="Portugal">{% translate 'Portugal' %}</option>
<option value="Romania">{% translate 'Romania' %}</option>
<option value="Slovakia">{% translate 'Slovakia' %}</option>
<option value="Slovenia">{% translate 'Slovenia' %}</option>
<option value="Spain">{% translate 'Spain' %}</option>
<option value="Sweden">{% translate 'Sweden' %}</option>
<option value="selectCountry" selected disabled>{% translate 'Select a country' %}</option>
{% for value, label in countryform.fields.country.choices %}
{% if value %}
<option value="{{ value }}">{{ label }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
<div class="control">
<button type="submit" class="button is-link">ok</button>
</div>
</div>
</div>
</form>
</div>



</section>

<script>
// sort from A to Z the country names after translation
document.addEventListener('DOMContentLoaded', function() {
var select = document.querySelector('select[name="country"]');
var options = Array.from(select.options);

// Remove the placeholder option from the array
var placeholderOption = options.find(option => option.value === "selectCountry");
options = options.filter(option => option !== placeholderOption);

// Sort the remaining options alphabetically
options.sort(function(a, b) {
return a.text.localeCompare(b.text);
});

// Reinsert the placeholder option at the beginning of the sorted options
options.unshift(placeholderOption);

// Clear the select element
select.innerHTML = '';

// Append the sorted options back to the select element
options.forEach(function(option) {
select.add(option);
});
});
</script>


{% endblock %}
Expand Down
19 changes: 9 additions & 10 deletions yvm/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@
}

def index(request):
if request.method == 'GET':
countryform = CountrySelectionForm(request.GET)
if countryform.is_valid():
country = countryform.cleaned_data['country']
url = reverse('bycountryAllTexts') + f'?country={country}'
return redirect(url)
countryform = CountrySelectionForm(request.GET)
if countryform.is_valid():
# a country has been selected
country = countryform.cleaned_data['country']
url = reverse('bycountryAllTexts') + f'?country={country}'
return redirect(url)
else:
countryform = CountrySelectionForm()

return render(request, 'yvm/index.html', {'countryform': countryform})
countryform.fields['country'].choices = sorted(countryform.fields['country'].choices, key=lambda x: x[1])
return render(request, 'yvm/index.html', {'countryform': countryform})


def bycountryAllTexts(request):
Expand Down Expand Up @@ -94,7 +93,7 @@ def bycountryAllTexts(request):
# df = replaceLanguageCodes(df)

# print(df[['vote__summary_url', 'vote__procedure_url']])
# print(df.query("mep__fullname == 'François-Xavier BELLAMY'"))
# print(df.query("mep__fullname == 'mep_name'"))

# output {short_desc -> {mep__fullname -> comment}}
# dict_comm = df0.pivot(index=['mep__fullname'], columns=['vote__short_desc'], values="comment").to_dict()
Expand Down

0 comments on commit 93c7cfc

Please sign in to comment.