"""
Views for the rdap_explorer project, query app.
"""

import ipwhois

from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views.decorators.cache import cache_page
from django.views.decorators.csrf import csrf_protect
from json import dumps
from pycountry import countries

from .forms import QueryForm


def index(request):
    if request.method == 'POST':
        form = QueryForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect(reverse(
                'query:results',
                args=(form['query'].value(),)
            ))
    else:
        form = QueryForm()

    return render(request, 'query/index.html', {
        'title': 'Query',
        'form': form
    })


@cache_page(86400)
@csrf_protect
def results(request, query):
    error = None
    result = {}
    form = QueryForm(initial={"query": query})

    try:
        ip = ipwhois.IPWhois(query)
        result = ip.lookup_rdap(retry_count=1, depth=2)
    except (ValueError, ipwhois.exceptions.IPDefinedError) as e:
        error = e

    if result['asn_country_code']:
        result['asn_country'] = countries.get(
            alpha_2=result['asn_country_code']
        ).name

    roles = {}
    for object_name in result['objects']:
        contact = result['objects'][object_name]['contact']
        for role in result['objects'][object_name]['roles']:
            if role not in roles:
                roles[role] = {}
                if contact['name'] is not None:
                    roles[role]['name'] = contact['name']
                if contact['email'] is not None:
                    roles[role]['email'] = contact['email'][0]['value']

    return render(request, 'query/index.html', {
        'title': query,
        'error': error,
        'form': form,
        'roles': roles,
        'result': dumps(result)
    })

@@ -1107,16 +1107,35 @@
 or = e%0A%0A
+    country = None%0A
     if r
@@ -1173,29 +1173,15 @@
     
-result%5B'asn_
 country
-'%5D
  = c
@@ -1193,29 +1193,16 @@
 ies.get(
-%0A            
 alpha_2=
@@ -1227,25 +1227,16 @@
 y_code'%5D
-%0A        
 ).name%0A%0A
@@ -1770,19 +1770,23 @@
    '
-title': que
+country': count
 ry,%0A
@@ -1886,16 +1886,41 @@
 (result)
+,%0A        'title': query,
 %0A    %7D)%0A

Move country from the result set to a template variable for simplier access.