Skip to content

Commit 9d8874d

Browse files
committed
Add a suggestion to fix the country name when submitting a draft in
case we can't parse it and it's not empty, add page with list of countries (to be able to refer people to it), add ISO codes as country aliases - the country alias code is now more intelligent with respect to case so it's easier to keep these aliases explicitly - Legacy-Id: 12862
1 parent 4501cd6 commit 9d8874d

8 files changed

Lines changed: 112 additions & 22 deletions

File tree

ietf/stats/migrations/0002_add_initial_aliases.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ def add_affiliation_info(apps, schema_editor):
1818
AffiliationIgnoredEnding.objects.get_or_create(ending="GmbH\.?")
1919

2020
CountryAlias = apps.get_model("stats", "CountryAlias")
21+
for iso_country_code in ['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW',
22+
'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN',
23+
'BO', 'BQ', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG',
24+
'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ',
25+
'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI',
26+
'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL',
27+
'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR',
28+
'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM',
29+
'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA',
30+
'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME',
31+
'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU',
32+
'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP',
33+
'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR',
34+
'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD',
35+
'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV',
36+
'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO',
37+
'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE',
38+
'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW']:
39+
CountryAlias.objects.get_or_create(alias=iso_country_code, country_id=iso_country_code)
40+
2141
CountryAlias.objects.get_or_create(alias="russian federation", country_id="RU")
2242
CountryAlias.objects.get_or_create(alias="p. r. china", country_id="CN")
2343
CountryAlias.objects.get_or_create(alias="p.r. china", country_id="CN")
@@ -74,7 +94,6 @@ def add_affiliation_info(apps, schema_editor):
7494
CountryAlias.objects.get_or_create(alias="grand-duchy of luxembourg", country_id="LU")
7595
CountryAlias.objects.get_or_create(alias="brasil", country_id="BR")
7696

77-
7897

7998
class Migration(migrations.Migration):
8099

ietf/stats/tests.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.core.urlresolvers import reverse as urlreverse
44

55
from ietf.utils.test_data import make_test_data, make_review_data
6-
from ietf.utils.test_utils import login_testing_unauthorized, TestCase
6+
from ietf.utils.test_utils import login_testing_unauthorized, TestCase, unicontent
77
import ietf.stats.views
88

99
class StatisticsTests(TestCase):
@@ -39,6 +39,16 @@ def test_document_stats(self):
3939
self.assertTrue(q('#chart'))
4040
self.assertTrue(q('table.stats-data'))
4141

42+
def test_known_country_list(self):
43+
make_test_data()
44+
45+
# check redirect
46+
url = urlreverse(ietf.stats.views.known_country_list)
47+
48+
r = self.client.get(url)
49+
self.assertEqual(r.status_code, 200)
50+
self.assertTrue("United States" in unicontent(r))
51+
4252
def test_review_stats(self):
4353
doc = make_test_data()
4454
review_req = make_review_data(doc)

ietf/stats/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
urlpatterns = patterns('',
77
url("^$", ietf.stats.views.stats_index),
88
url("^document/(?:(?P<stats_type>authors|pages|words|format|formlang|author/documents|author/affiliation|author/country|author/continent|author/citation)/)?$", ietf.stats.views.document_stats),
9+
url("^knowncountries/$", ietf.stats.views.known_countries_list),
910
url("^review/(?:(?P<stats_type>completion|results|states|time)/)?(?:%(acronym)s/)?$" % settings.URL_REGEXPS, ietf.stats.views.review_stats),
1011
)

ietf/stats/utils.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,12 @@ def affiliation_sort_key(affiliation):
8484
return res
8585

8686

87-
88-
8987
def get_aliased_countries(countries):
9088
known_aliases = dict(CountryAlias.objects.values_list("alias", "country__name"))
9189

92-
iso_code_aliases = {}
93-
9490
# add aliases for known countries
9591
for slug, name in CountryName.objects.values_list("slug", "name"):
96-
if len(name) > 2:
97-
known_aliases[name.lower()] = name
98-
99-
if len(slug) == 2 and slug[0].isupper() and slug[1].isupper():
100-
iso_code_aliases[slug] = name # add ISO code
92+
known_aliases[name.lower()] = name
10193

10294
def lookup_alias(possible_alias):
10395
name = known_aliases.get(possible_alias)
@@ -185,14 +177,16 @@ def last_text_part_stripped(split):
185177
if found:
186178
continue
187179

188-
# if everything else has failed, try ISO code
189-
country = iso_code_aliases.get(country, country)
190-
if country in known_countries:
191-
res[original_country] = country
192-
continue
193-
194180
# unknown country
195181
res[original_country] = ""
196182

197183
return res
198184

185+
186+
def clean_country_name(country_name):
187+
if country_name:
188+
country_name = get_aliased_countries([country_name]).get(country_name, country_name)
189+
if country_name and CountryName.objects.filter(name=country_name).exists():
190+
return country_name
191+
192+
return ""

ietf/stats/views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@ def count_bins(bins):
526526
})
527527

528528

529+
def known_countries_list(request, stats_type=None, acronym=None):
530+
countries = CountryName.objects.prefetch_related("countryalias_set")
531+
for c in countries:
532+
# the sorting is a bit of a hack - it puts the ISO code first
533+
# since it was added in a migration
534+
c.aliases = sorted(c.countryalias_set.all(), key=lambda a: a.pk)
535+
536+
return render(request, "stats/known_countries_list.html", {
537+
"countries": countries
538+
})
539+
540+
529541
@login_required
530542
def review_stats(request, stats_type=None, acronym=None):
531543
# This view is a bit complex because we want to show a bunch of

ietf/submit/views.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
recently_approved_by_user, validate_submission, create_submission_event,
3232
docevent_from_submission, post_submission, cancel_submission, rename_submission_files,
3333
get_person_from_name_email )
34+
from ietf.stats.utils import clean_country_name
3435
from ietf.utils.accesstoken import generate_random_key, generate_access_token
3536
from ietf.utils.draft import Draft
3637
from ietf.utils.log import log
@@ -401,6 +402,9 @@ def submission_status(request, submission_id, access_token=None):
401402
# something went wrong, turn this into a GET and let the user deal with it
402403
return HttpResponseRedirect("")
403404

405+
for author in submission.authors:
406+
author["cleaned_country"] = clean_country_name(author.get("country"))
407+
404408
return render(request, 'submit/submission_status.html', {
405409
'selected': 'status',
406410
'submission': submission,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% extends "base.html" %}
2+
3+
{% load origin %}
4+
5+
{% load ietf_filters staticfiles bootstrap3 %}
6+
7+
{% block content %}
8+
{% origin %}
9+
10+
<h1>{% block title %}Countries known to the Datatracker{% endblock %}</h1>
11+
12+
<table class="table table-condensed table-striped">
13+
<thead>
14+
<tr>
15+
<th>Name</th>
16+
<th>Aliases (lowercase aliases are matched case-insensitive)</th>
17+
</tr>
18+
</thead>
19+
<tbody>
20+
{% for c in countries %}
21+
<tr>
22+
<td>{{ c.name }}</td>
23+
<td>
24+
{% for a in c.aliases %}
25+
{{ a.alias }}{% if not forloop.last %},{% endif %}
26+
{% endfor %}
27+
</td>
28+
</tr>
29+
{% endfor %}
30+
</tbody>
31+
</table>
32+
33+
{% endblock %}

ietf/templates/submit/submission_status.html

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ <h2>Meta-data from the submission</h2>
195195
<tr>
196196
<th>Authors</th>
197197
<td>
198-
{% with submission.authors as authors %}
199-
{{ authors|length }} author{{ authors|pluralize }}
200-
{% endwith %}
198+
{{ submission.authors|length }} author{{ submission.authors|pluralize }}
201199
{% if errors.authors %}<p class="text-danger"><b>{{ errors.authors|safe }}</b></p>{% endif %}
202200
</td>
203201
</tr>
@@ -207,8 +205,27 @@ <h2>Meta-data from the submission</h2>
207205
<th>Author {{ forloop.counter }}</th>
208206
<td>
209207
{{ author.name }} {% if author.email %}&lt;{{ author.email }}&gt;{% endif %}
210-
- {% if author.affiliation %}{{ author.affiliation }}{% else %}<i>unknown affiliation</i>{% endif %}
211-
- {% if author.country %}{{ author.country }}{% else %}<i>unknown country</i>{% endif %}
208+
-
209+
{% if author.affiliation %}
210+
{{ author.affiliation }}
211+
{% else %}
212+
<i>unknown affiliation</i>
213+
{% endif %}
214+
-
215+
{% if author.country %}
216+
{{ author.country }}
217+
{% if author.cleaned_country and author.country != author.cleaned_country %}
218+
(understood to be {{ author.cleaned_country }})
219+
{% endif %}
220+
{% else %}
221+
<i>unknown country</i>
222+
{% endif %}
223+
224+
{% if author.country and not author.cleaned_country %}
225+
<br>
226+
<b class="text-warning">Unrecognized country: "{{ author.country }}"</b>: Please use a <a href="{% url "ietf.stats.views.known_countries_list" %}">recognized country name</a>.
227+
{% endif %}
228+
212229
</td>
213230
</tr>
214231
{% endfor %}

0 commit comments

Comments
 (0)