# Path: dplace_app/models.py
# class Language(models.Model):
#     name = models.CharField(max_length=200, db_index=True)
#     glotto_code = models.CharField(max_length=8, null=False, unique=True)
# 
#     # needs to be null=True because some glottolog languages do not have isocodes
#     iso_code = models.CharField(max_length=3, null=True)
#     family = models.ForeignKey('LanguageFamily', null=True)
# 
#     def __unicode__(self):
#         return "Language: %s, ISO Code %s, Glotto Code %s" % (
#             self.name, self.iso_code, self.glotto_code)
#     
#     def get_absolute_url(self):
#         return reverse("view_language", args=[self.glotto_code])
#     
#     class Meta(object):
#         verbose_name = "Language"
#         ordering = ('name', )


import json
import zipfile
import datetime
from django.http import Http404, HttpResponse
from django.shortcuts import render, get_object_or_404
from dplace_app.models import Language
from django.views.decorators.csrf import ensure_csrf_cookie
from six import StringIO



def view_language(request, glottocode):
    language = get_object_or_404(Language, glottocode=glottocode)
    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 download_file(request):
    if request.method =='POST':
        s = StringIO()
        zf = zipfile.ZipFile(s, "w")
        try:
            if 'values' in request.POST:
                result_set = json.loads(request.POST.get('values'))
            else:
                result_set = {}
            name = 'tree.svg'
            tree = ''
            if 'n' in result_set:
                name = result_set['n'][0]

# Path: dplace_app/models.py
# class Language(models.Model):
#     name = models.CharField(max_length=200, db_index=True)
#     glotto_code = models.CharField(max_length=8, null=False, unique=True)
# 
#     # needs to be null=True because some glottolog languages do not have isocodes
#     iso_code = models.CharField(max_length=3, null=True)
#     family = models.ForeignKey('LanguageFamily', null=True)
# 
#     def __unicode__(self):
#         return "Language: %s, ISO Code %s, Glotto Code %s" % (
#             self.name, self.iso_code, self.glotto_code)
#     
#     def get_absolute_url(self):
#         return reverse("view_language", args=[self.glotto_code])
#     
#     class Meta(object):
#         verbose_name = "Language"
#         ordering = ('name', )


# Path: dplace_app/views.py
import json
import zipfile
import datetime
from django.http import Http404, HttpResponse
from django.shortcuts import render, get_object_or_404
from dplace_app.models import Language
from django.views.decorators.csrf import ensure_csrf_cookie
from six import StringIO




def view_language(request, glottocode):
    language = get_object_or_404(Language, glottocode=glottocode)
    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 download_file(request):
    if request.method =='POST':
        s = StringIO()
        zf = zipfile.ZipFile(s, "w")
        try:
            if 'values' in request.POST:
                result_set = json.loads(request.POST.get('values'))
            else:
                result_set = {}
            name = 'tree.svg'
            tree = ''
            if 'n' in result_set:
                name = result_set['n'][0]

            if 't' in result_set: