from __builtin__ import dict
from django.http import Http404
from django.shortcuts import render, get_object_or_404
from django.views.decorators.csrf import ensure_csrf_cookie
from dplace_app.models import Society, Language
from forms import GeoForm
from models import ISOCode
# Create your views here.

def search_geo(request):
    if request.method == 'POST':
        # handle search
        form = GeoForm(request.POST)
        results = []
        region = None
        if form.is_valid():
            cd = form.cleaned_data
            region = cd['region'] # Polygon
            isocodes = ISOCode.objects.filter(location__intersects=region)
            # get the environmentals
            for isocode in isocodes:
                result = {
                'isocode': isocode.iso_code,
                'environmentals': isocode.environmentals.all(),
                'societies':isocode.societies.all(),}
                results.append(result)
        return render(request, 'search_geo.html', {'form': form, 'region': region, 'results': results})
    else:
        form = GeoForm()
        return render(request, 'search_geo.html', {'form': form})

def view_society(request, society_id):
    society = get_object_or_404(Society, pk=society_id)
    environmentals = society.environmentals.all()
    cultural_traits = society.get_cultural_trait_data()
    return render(request,'society.html', {'society': society,
                                           'environmentals': environmentals,
                                           'cultural_traits': cultural_traits})

def view_language(request, language_id):
    language = get_object_or_404(Language, pk=language_id)
    if language.society is None:
        raise Http404
    environmentals = language.society.environmentals.all()
    ea_values = language.society.get_ethnographic_atlas_data()
    return render(request,'society.html', {'language': language,
                                           'society': language.society,
                                           'environmentals': environmentals,
                                           'ea_values': ea_values})

@ensure_csrf_cookie
def angular(request):
    return render(request, 'angular.html', dict())

from __builtin__ import dict
from django.http import Http404
from django.shortcuts import render, get_object_or_404
from dplace_app.models import Society, Language
from forms import GeoForm
from models import ISOCode
# Create your views here.

def search_geo(request):
    if request.method == 'POST':
        # handle search
        form = GeoForm(request.POST)
        results = []
        region = None
        if form.is_valid():
            cd = form.cleaned_data
            region = cd['region'] # Polygon
            isocodes = ISOCode.objects.filter(location__intersects=region)
            # get the environmentals
            for isocode in isocodes:
                result = {
                'isocode': isocode.iso_code,
                'environmentals': isocode.environmentals.all(),
                'societies':isocode.societies.all(),}
                results.append(result)
        return render(request, 'search_geo.html', {'form': form, 'region': region, 'results': results})
    else:
        form = GeoForm()
        return render(request, 'search_geo.html', {'form': form})

def view_society(request, society_id):
    society = get_object_or_404(Society, pk=society_id)
    environmentals = society.environmentals.all()
    cultural_traits = society.get_cultural_trait_data()
    return render(request,'society.html', {'society': society,
                                           'environmentals': environmentals,
                                           'cultural_traits': cultural_traits})

def view_language(request, language_id):
    language = get_object_or_404(Language, pk=language_id)
    if language.society is None:
        raise Http404
    environmentals = language.society.environmentals.all()
    ea_values = language.society.get_ethnographic_atlas_data()
    return render(request,'society.html', {'language': language,
                                           'society': language.society,
                                           'environmentals': environmentals,
                                           'ea_values': ea_values})

def angular(request):
    return render(request, 'angular.html', dict())

Remove ensure_csrf_cookie from views