diff --git a/remo/base/helpers.py b/remo/base/helpers.py index 574ede6d3..16fe3323d 100644 --- a/remo/base/helpers.py +++ b/remo/base/helpers.py @@ -14,6 +14,7 @@ from funfactory import utils from jingo import register from jinja2 import Markup +from product_details import product_details AES_PADDING = 16 @@ -21,6 +22,11 @@ LINE_LIMIT = 75 FOLD_SEP = u'\r\n ' +COUNTRIES_NAME_TO_CODE = {} +for code, name in product_details.get_regions('en').items(): + name = name.lower() + COUNTRIES_NAME_TO_CODE[name] = code + @register.filter def markdown(text): @@ -264,3 +270,9 @@ def formset_errors_exist(formset): if form.errors: return True return False + + +@register.filter +def get_country_code(country_name): + """Return country code from country name.""" + return COUNTRIES_NAME_TO_CODE.get(country_name.lower(), '') diff --git a/remo/base/templates/multi_event_ical_template.ics b/remo/base/templates/multi_event_ical_template.ics index 1ec6dd8f1..b5b183a07 100644 --- a/remo/base/templates/multi_event_ical_template.ics +++ b/remo/base/templates/multi_event_ical_template.ics @@ -16,6 +16,7 @@ DTEND;TZID={{ event.timezone }}:{{ event.end|format_datetime_utc }} SEQUENCE:{{ event.times_edited }} X-COORDINATES-LAT:{{ event.lat }} X-COORDINATES-LON:{{ event.lon }} +X-COUNTRY-CODE:{{ event.country|get_country_code }} END:VEVENT {% endfor %} END:VCALENDAR diff --git a/remo/base/tests/test_helpers.py b/remo/base/tests/test_helpers.py new file mode 100644 index 000000000..2b315ff1f --- /dev/null +++ b/remo/base/tests/test_helpers.py @@ -0,0 +1,13 @@ +from nose.tools import eq_ + +from remo.base.helpers import get_country_code +from remo.base.tests import RemoTestCase + + +class GetCountryNameTests(RemoTestCase): + def test_base(self): + eq_(get_country_code('Greece'), 'gr') + eq_(get_country_code('greece'), 'gr') + + def test_country_name_does_not_match(self): + eq_(get_country_code('FOO'), '')