# Path: dplace_app/serializers.py
# SEARCH_LANGUAGE = 'l'
# SEARCH_ENVIRONMENTAL = 'e'
# SEARCH_VARIABLES = 'v'
# SEARCH_GEOGRAPHIC = 'g'
# class SourceSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class CodeDescriptionSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class VariableSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class CategorySerializer(serializers.ModelSerializer):
#     class Meta(object):
# class VariableDetailSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class CategoryDetailSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class ValueSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageFamilySerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageSerializerWithSocieties(serializers.ModelSerializer):
#     class Meta(object):
# class SocietySerializer(serializers.ModelSerializer):
#     class Meta(object):
# class GeographicRegionSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class SocietyWithRegionSerializer(SocietySerializer):
# class TreeSocietySerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageTreeLabelsSequenceSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
# class LanguageTreeLabelsSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageTreeSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class SocietyResult(object):
# class VariableCode(object):
# class SocietyResultSet(object):
# class VariableCodeSerializer(serializers.Serializer):
# class SocietyResultSerializer(serializers.Serializer):
# class SocietyResultSetSerializer(serializers.Serializer):
# class Legend(object):
# class LegendSerializer(serializers.Serializer):
#     def __init__(self):
#     def __init__(self, society):
#     def __eq__(self, other):
#     def __init__(self, codes, variable):
#     def __eq__(self, other):
#     def __init__(self):
#     def __init__(self, name, svg):
# 
# Path: dplace_app/models.py
# class Society(models.Model):
#     class Meta(object):
# class SocietyRelation(models.Model):
# class Category(models.Model):
#     class Meta(object):
# class Variable(models.Model):
#     class Meta(object):
# class CodeDescription(models.Model):
#     class Meta(object):
# class Value(models.Model):
#     class Meta(object):
# class Source(models.Model):
#     class Meta(object):
# class LanguageFamily(models.Model):
#     class Meta(object):
# class Language(models.Model):
#     class Meta(object):
# class GeographicRegion(models.Model):
#     class Meta(object):
# class LanguageTree(models.Model):
#     class Meta(object):
# class LanguageTreeLabels(models.Model):
#     class Meta:
# class LanguageTreeLabelsSequence(models.Model):
#     class Meta:
#     def related(self):
#     def location(self):
#     def original_location(self):
#     def get_environmental_data(self):
#     def get_cultural_trait_data(self):
#     def get_data_references(self):
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def __unicode__(self):
#     def coded_societies(self):
#     def __unicode__(self):
#     def save(self, *args, **kwargs):
#     def read_code_number(self):
#     def coded_societies(self):
#     def __unicode__(self):
#     def get_description(self):
#     def __unicode__(self):
#     def __unicode__(self):
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def __unicode__(self):


import json
import re
import datetime
import logging
from itertools import groupby
from django.db import connection
from django.db.models import Prefetch, Q, Count
from django.shortcuts import get_object_or_404
from django.http import Http404
from rest_framework import viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.decorators import api_view, permission_classes, renderer_classes
from rest_framework.permissions import AllowAny
from rest_framework.views import Response
from rest_framework.renderers import JSONRenderer
from dplace_app.renderers import DPLACECSVRenderer
from dplace_app import serializers
from dplace_app import models
from dplace_app.tree import update_newick
    from time import time
    from time import time
    from django.db import connection
            .distinct():
        if update_newick(t, labels):
            language_trees.append(t)

    return Response(serializers.LanguageTreeSerializer(language_trees, many=True).data)


@api_view(['GET'])
@permission_classes((AllowAny,))
def find_societies(request):
    """
    View to find the societies that match an input request.  Currently expects
    { language_filters: [{language_ids: [1,2,3]}], variable_codes: [4,5,6...],
    environmental_filters: [{id: 1, operator: 'gt', params: [0.0]},
    {id:3, operator 'inrange', params: [10.0,20.0] }] }

    Returns serialized collection of SocietyResult objects
    """
    s = time()
    log.info('%s find_societies 1: %s queries' % (time() - s, len(connection.queries)))
    query = {}
    if 'name' in request.query_params:
        result_set = serializers.SocietyResultSet()
        q = request.query_params['name']
        if q:
            soc = models.Society.objects.filter(
                Q(name__icontains=q) | Q(alternate_names__unaccent__icontains=q))
            for s in soc:
                if s.value_set.count():
                    result_set.societies.add(serializers.SocietyResult(s))

# Path: dplace_app/serializers.py
# SEARCH_LANGUAGE = 'l'
# SEARCH_ENVIRONMENTAL = 'e'
# SEARCH_VARIABLES = 'v'
# SEARCH_GEOGRAPHIC = 'g'
# class SourceSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class CodeDescriptionSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class VariableSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class CategorySerializer(serializers.ModelSerializer):
#     class Meta(object):
# class VariableDetailSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class CategoryDetailSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class ValueSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageFamilySerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageSerializerWithSocieties(serializers.ModelSerializer):
#     class Meta(object):
# class SocietySerializer(serializers.ModelSerializer):
#     class Meta(object):
# class GeographicRegionSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class SocietyWithRegionSerializer(SocietySerializer):
# class TreeSocietySerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageTreeLabelsSequenceSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
# class LanguageTreeLabelsSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class LanguageTreeSerializer(serializers.ModelSerializer):
#     class Meta(object):
# class SocietyResult(object):
# class VariableCode(object):
# class SocietyResultSet(object):
# class VariableCodeSerializer(serializers.Serializer):
# class SocietyResultSerializer(serializers.Serializer):
# class SocietyResultSetSerializer(serializers.Serializer):
# class Legend(object):
# class LegendSerializer(serializers.Serializer):
#     def __init__(self):
#     def __init__(self, society):
#     def __eq__(self, other):
#     def __init__(self, codes, variable):
#     def __eq__(self, other):
#     def __init__(self):
#     def __init__(self, name, svg):
# 
# Path: dplace_app/models.py
# class Society(models.Model):
#     class Meta(object):
# class SocietyRelation(models.Model):
# class Category(models.Model):
#     class Meta(object):
# class Variable(models.Model):
#     class Meta(object):
# class CodeDescription(models.Model):
#     class Meta(object):
# class Value(models.Model):
#     class Meta(object):
# class Source(models.Model):
#     class Meta(object):
# class LanguageFamily(models.Model):
#     class Meta(object):
# class Language(models.Model):
#     class Meta(object):
# class GeographicRegion(models.Model):
#     class Meta(object):
# class LanguageTree(models.Model):
#     class Meta(object):
# class LanguageTreeLabels(models.Model):
#     class Meta:
# class LanguageTreeLabelsSequence(models.Model):
#     class Meta:
#     def related(self):
#     def location(self):
#     def original_location(self):
#     def get_environmental_data(self):
#     def get_cultural_trait_data(self):
#     def get_data_references(self):
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def __unicode__(self):
#     def coded_societies(self):
#     def __unicode__(self):
#     def save(self, *args, **kwargs):
#     def read_code_number(self):
#     def coded_societies(self):
#     def __unicode__(self):
#     def get_description(self):
#     def __unicode__(self):
#     def __unicode__(self):
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def __unicode__(self):


# Path: dplace_app/api_views.py
import json
import re
import datetime
import logging
from itertools import groupby
from django.db import connection
from django.db.models import Prefetch, Q, Count
from django.shortcuts import get_object_or_404
from django.http import Http404
from rest_framework import viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.decorators import api_view, permission_classes, renderer_classes
from rest_framework.permissions import AllowAny
from rest_framework.views import Response
from rest_framework.renderers import JSONRenderer
from dplace_app.renderers import DPLACECSVRenderer
from dplace_app import serializers
from dplace_app import models
from dplace_app.tree import update_newick
    from time import time
    from time import time
    from django.db import connection

            .distinct():
        if update_newick(t, labels):
            language_trees.append(t)

    return Response(serializers.LanguageTreeSerializer(language_trees, many=True).data)


@api_view(['GET'])
@permission_classes((AllowAny,))
def find_societies(request):
    """
    View to find the societies that match an input request.  Currently expects
    { language_filters: [{language_ids: [1,2,3]}], variable_codes: [4,5,6...],
    environmental_filters: [{id: 1, operator: 'gt', params: [0.0]},
    {id:3, operator 'inrange', params: [10.0,20.0] }] }

    Returns serialized collection of SocietyResult objects
    """
    s = time()
    log.info('%s find_societies 1: %s queries' % (time() - s, len(connection.queries)))
    query = {}
    if 'name' in request.query_params:
        result_set = serializers.SocietyResultSet()
        q = request.query_params['name']
        if q:
            soc = models.Society.objects.filter(
                Q(name__icontains=q) | Q(alternate_names__unaccent__icontains=q))
            for s in soc:
                if s.value_set.count():
                    result_set.societies.add(serializers.SocietyResult(s))

        return Response(serializers.SocietyResultSetSerializer(result_set).data)