From 8bb1733427557268a6eb956574a8aa39be0fbdfb Mon Sep 17 00:00:00 2001 From: heinzel Date: Sat, 20 Jan 2018 15:23:58 +0100 Subject: [PATCH] Replaced own country selection with django-countries package. --- dav_events/choices.py | 15 ++++++++------- .../Resources/django.main.additional_settings.py | 1 + dav_events/forms.py | 9 ++++----- dav_events/models.py | 4 ++-- setup.py | 1 + 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/dav_events/choices.py b/dav_events/choices.py index 052b899..a5e7255 100644 --- a/dav_events/choices.py +++ b/dav_events/choices.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from django.utils.translation import ugettext_lazy as _ +from django_countries import Countries class ChoiceSet(object): @@ -32,6 +33,12 @@ class ChoiceSet(object): return self._labels[code] +class CountryChoiceSet(Countries): + first = ['DE', 'AU', 'CH', 'FR', 'IT'] + first_repeat = True + first_break = '--' + + ACCOMMODATION_CHOICES = ChoiceSet([ ('NONE', _(u'Keine (tägliche Anreise)')), ('biwi', _(u'Biwak')), @@ -41,13 +48,7 @@ ACCOMMODATION_CHOICES = ChoiceSet([ ('OTHER', _(u'Andere Unterkunft (zusätzliches Feld)')), ]) -COUNTRY_CHOICES = ChoiceSet([ - ('DE', _(u'Deutschland')), - ('AU', _(u'Österreich')), - ('CH', _(u'Schweiz')), - ('FR', _(u'Frankreich')), - ('IT', _(u'Italien')), -]) +COUNTRY_CHOICES = CountryChoiceSet() DEADLINE_CHOICES = ChoiceSet([ ('month', _(u'Einen Monat vorher')), diff --git a/dav_events/console_scripts/Resources/django.main.additional_settings.py b/dav_events/console_scripts/Resources/django.main.additional_settings.py index 4cd6fc8..199a0f5 100644 --- a/dav_events/console_scripts/Resources/django.main.additional_settings.py +++ b/dav_events/console_scripts/Resources/django.main.additional_settings.py @@ -9,6 +9,7 @@ BASE_SHARE_DIR = os.path.join(BASE_DIR, 'common') INSTALLED_APPS += [ 'bootstrap3', 'datetimewidget', + 'django_countries', 'django_extensions', # Our main app 'dav_events', diff --git a/dav_events/forms.py b/dav_events/forms.py index 529cfc2..d6836c7 100644 --- a/dav_events/forms.py +++ b/dav_events/forms.py @@ -3,11 +3,11 @@ import calendar import datetime import logging from babel.dates import format_date - from django import forms from django.contrib.auth import forms as auth_forms, password_validation from django.core.exceptions import ImproperlyConfigured from django.utils.translation import get_language, ugettext, ugettext_lazy as _ +from django_countries.fields import LazyTypedChoiceField from datetimewidget.widgets import DateWidget, TimeWidget, DateTimeWidget from . import choices @@ -318,10 +318,9 @@ class LocationForm(EventCreateForm): _form_title = _(u'Ort') _next_form_name = 'ApproachForm' - country = forms.ChoiceField(choices=choices.COUNTRY_CHOICES, - initial='DE', - label=_(u'Land'), - ) + country = LazyTypedChoiceField(choices=choices.COUNTRY_CHOICES, + initial='DE', + label=_(u'Land')) terrain = forms.ChoiceField(choices=choices.TERRAIN_CHOICES, initial='submountains', diff --git a/dav_events/models.py b/dav_events/models.py index 3a6ce4e..cc68090 100644 --- a/dav_events/models.py +++ b/dav_events/models.py @@ -6,6 +6,7 @@ from django.db import models from django.template.loader import get_template from django.utils import timezone from django.utils.translation import get_language, ugettext_lazy as _ +from django_countries.fields import CountryField from . import choices from . import config @@ -59,8 +60,7 @@ class Event(models.Model): course_goal_6 = models.TextField(blank=True) # LocationForm - country = models.CharField(max_length=CHOICE_FIELD_MAX_LENGTH, - choices=choices.COUNTRY_CHOICES) + country = CountryField() terrain = models.CharField(max_length=CHOICE_FIELD_MAX_LENGTH, choices=choices.TERRAIN_CHOICES) location = models.CharField(max_length=config.LOCATION_MAX_LENGTH) diff --git a/setup.py b/setup.py index b62654e..17b8269 100644 --- a/setup.py +++ b/setup.py @@ -69,6 +69,7 @@ setup( 'django >= 1.11, < 2.0', 'django-extensions', 'django-bootstrap3', + 'django-countries', 'django-datetime-widget', ], extras_require={