Bug won't allow render page having None country
http 500
code gets generated due to null
/None
in cached data.
redis /var/run/redis/redis-server.sock> get sview_cached;top_countries_by_attackers_list_long;1w
"[{\"country\": \"CN\", \"count\": 24153}, {\"country\": \"US\", \"count\": 13836}, {\"country\": \"MX\", \"count\": 12480}, {\"country\": \"IN\", \"count\": 10346}, {\"country\": \"RU\", \"count\": 8023}, {\"country\": \"BR\", \"count\": 6578}, {\"country\": \"EG\", \"count\": 5388}, {\"country\": \"IR\", \"count\": 4036}, {\"country\": \"DE\", \"count\": 3235}, {\"country\": \"KR\", \"count\": 2886}, {\"country\": \"TW\", \"count\": 2147}, {\"country\": \"ES\", \"count\": 1963}, {\"country\": \"TR\", \"count\": 1864}, {\"country\": \"IT\", \"count\": 1838}, {\"country\": \"TH\", \"count\": 1774}, {\"country\": \"ID\", \"count\": 1718}, {\"country\": \"GB\", \"count\": 1703}, {\"country\": \"UA\", \"count\": 1369}, {\"country\": \"AR\", \"count\": 1367}, {\"country\": \"PK\", \"count\": 1331}, {\"country\": \"VN\", \"count\": 1088}, {\"country\": \"CA\", \"count\": 1066}, {\"country\": \"HU\", \"count\": 1053}, {\"country\": \"FR\", \"count\": 1050}, {\"country\": \"PL\", \"count\": 1043}, {\"country\": \"NL\", \"count\": 968}, {\"country\": \"SG\", \"count\": 929}, {\"country\": \"JP\", \"count\": 895}, {\"country\": \"HK\", \"count\": 859}, {\"country\": null, \"count\": 676}]"
Generates following error:
Traceback (most recent call last):
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "./sview/statistics/views.py", line 92, in attackers
return render_template(
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/flask/templating.py", line 147, in render_template
return _render(
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/flask/templating.py", line 128, in _render
rv = template.render(context)
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
self.environment.handle_exception()
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "/var/www/uwsgi_sview/sview/templates/statistics/attackers.html", line 8, in top-level template code
{% extends "layout.html" %}
File "/var/www/uwsgi_sview/sview/templates/layout.html", line 105, in top-level template code
{%- block body %}{% endblock %}
File "/var/www/uwsgi_sview/sview/templates/statistics/attackers.html", line 30, in block 'body'
{% call(data_item) data_box("top_countries_by_attackers_list_long", "Top countries by unique attackers", resources["top_countries_by_attackers_list_long"], "country") -%}
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/jinja2/runtime.py", line 828, in _invoke
rv = self._func(*arguments)
File "/var/www/uwsgi_sview/sview/templates/_snippets.html", line 70, in template
{% if caller %}{{ caller(item[data_attr]) }}{% else %}{{ data_item(item[data_attr]) }}{% endif %}
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/jinja2/runtime.py", line 828, in _invoke
rv = self._func(*arguments)
File "/var/www/uwsgi_sview/sview/templates/statistics/attackers.html", line 31, in template
{{ country_code_item(data_item) }}
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/jinja2/runtime.py", line 828, in _invoke
rv = self._func(*arguments)
File "/var/www/uwsgi_sview/sview/templates/_snippets.html", line 46, in template
{{ country_code_flag(data) }} <abbr title="{{data|uncc}}">{{ data }}</abbr>
File "./sview/filters.py", line 25, in country_code_to_name
c = pycountry.countries.get(alpha_2=cc)
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/pycountry/db.py", line 39, in load_if_needed
return f(self, *args, **kw)
File "/opt/virtualenv/uwsgi_sview/lib/python3.9/site-packages/pycountry/db.py", line 111, in get
raise LookupError(
Probable solution is to handle formatting function like
def country_code_to_name(cc):
try:
c = pycountry.countries.get(alpha_2=cc)
return c.common_name if hasattr(c, "common_name") else c.name
except LookupError:
return 'N/A'