<commit_before>#from django.db.models import Count
from django.contrib.gis.db.models import Count
from django.shortcuts import render
#from django.contrib.auth.models import User, Group
from rest_framework import views, viewsets, permissions, response, pagination
from django.contrib.gis.geos import Polygon, Point

from gis_csdt.models import Dataset, MapPoint, Tag, MapPolygon, TagIndiv, DataField, DataElement
from gis_csdt.serializers import TagCountSerializer, DatasetSerializer, MapPointSerializer, NewTagSerializer, TagSerializer, NewTagSerializer, MapPolygonSerializer #, CountPointsInPolygonSerializer
from django.core.paginator import Paginator
from django.contrib.gis.measure import Distance, Area


class TagViewSet(viewsets.ModelViewSet):
    queryset = TagIndiv.objects.filter(tag__approved=True).distinct('tag')
    serializer_class = TagSerializer

    #http://www.django-rest-framework.org/api-guide/permissions
    permission_classes = (permissions.AllowAny,)#(permissions.IsAuthenticatedOrReadOnly)

class NewTagViewSet(viewsets.ModelViewSet):
    queryset = TagIndiv.objects.filter(tag__approved=True).distinct('tag')
    serializer_class = NewTagSerializer

    #http://www.django-rest-framework.org/api-guide/permissions
    permission_classes = (permissions.AllowAny,)#(permissions.IsAuthenticatedOrReadOnly)

class TagCountViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = TagCountSerializer
    model = Tag

    def get_queryset(self):
        return Tag.objects.filter(approved = True).values('dataset','tag').annotate(num_tags = Count('id'))

class DatasetViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Dataset.objects.all()
    serializer_class = DatasetSerializer

class MapPointViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = MapPointSerializer
    model = MapPoint

    def get_queryset(self):
        t = False
        matchall = False
        queryset = MapPoint.objects.none()

        for (param, result) in self.request.QUERY_PARAMS.items():
            if param in ['tag','tags']:
                t = result
            elif param == 'match' and result == 'all':
                matchall = True
        if t:
            t = t.split(',')
            if type(t) is not list:
                t = [t]
            if matchall:
                queryset = MapPoint.objects
                for tag in t:
                    try:
                        num = int(tag)
                        queryset = queryset.filter(tagindiv__tag=num)
                    except:
                        queryset = queryset.filter(tagindiv__tag__tag=tag)
            else:
                for tag in t:
                    try:
                        num = int(tag)
                        queryset = queryset | MapPoint.objects.filter(tagindiv__tag=num)
                    except:
                        queryset = queryset | MapPoint.objects.filter(tagindiv__tag__tag=tag)
            queryset = queryset.filter(tagindiv__tag__approved=True)
        else:
            queryset = MapPoint.objects
        bb = {}
        for param, result in self.request.QUERY_PARAMS.items():
            p = param.lower()
            if p == 'dataset':
                try:
                    r = int(result)
                    queryset = queryset.filter(dataset__id__exact = r)
                except:
                    queryset = queryset.filter(dataset__name__icontains = result.strip())
            elif p in ['max_lat','min_lat','max_lon','min_lon']:
                try:
                    r = float(result)
                    #for tolerance
                    minr = r - 0.0000005
                    maxr = r + 0.0000005 
                except:
                    continue
                if p == 'max_lat' or p == 'lat':
                    queryset = queryset.filter(lat__lte = maxr)
                    bb['max_lat'] = maxr
                if p == 'min_lat' or p == 'lat':
                    queryset = queryset.filter(lat__gte = minr)
                    bb['min_lat'] = minr
                    continue
                if p == 'max_lon' or p == 'lon':
                    queryset = queryset.filter(lon__lte = maxr)
                    bb['max_lon'] = maxr
                if p == 'min_lon' or p == 'lon':
                    queryset = queryset.filter(lon__gte = minr)
                    bb['min_lon'] = minr
            elif p == 'street':
                queryset = queryset.filter(street__iexact = result)
            elif p == 'city':
                print result
                queryset = queryset.filter(city__iexact = result)
            elif p == 'state':
                queryset = queryset.filter(state__iexact = result)
            elif p == 'county':
                queryset = queryset.filter(county__iexact = result)
            elif p in ['zipcode','zip','zip_code']:
                queryset = queryset.filter(zipcode__iexact = result)
            elif param == 'radius':
                try:
                    radius = int(result)
                except:
                    return HttpResponseBadRequest('Invalid radius. Only integers accepted.' )
            elif param == 'center':
                temp = result.split(',')
                try:
                    if len(temp) != 2:
                        raise 
                    temp[0] = float(temp[0])
                    temp[1] = float(temp[1])
                    center = Point(temp[0],temp[1])
                except:
                    return HttpResponseBadRequest('Invalid center. Format is: center=lon,lat' )


        if 'max_lat' in bb and 'min_lat' in bb and 'max_lon' in bb and 'min_lon' in bb:
            #mid_lat = (bb['max_lat'] + bb['min_lat']) / 2
            #mid_lon = (bb['max_lon'] + bb['min_lon']) / 2
            geom = Polygon.from_bbox((bb['min_lon'],bb['min_lat'],bb['max_lon'],bb['max_lat']))#Point(mid_lat, mid_lon)
            #print geom
            queryset = queryset.filter(point__within=geom)
            #print queryset.query
        if radius and center:<commit_msg>fixed api-mp undefined variable error<commit_after>#from django.db.models import Count
from django.contrib.gis.db.models import Count
from django.shortcuts import render
#from django.contrib.auth.models import User, Group
from rest_framework import views, viewsets, permissions, response, pagination
from django.contrib.gis.geos import Polygon, Point

from gis_csdt.models import Dataset, MapPoint, Tag, MapPolygon, TagIndiv, DataField, DataElement
from gis_csdt.serializers import TagCountSerializer, DatasetSerializer, MapPointSerializer, NewTagSerializer, TagSerializer, NewTagSerializer, MapPolygonSerializer #, CountPointsInPolygonSerializer
from django.core.paginator import Paginator
from django.contrib.gis.measure import Distance, Area


class TagViewSet(viewsets.ModelViewSet):
    queryset = TagIndiv.objects.filter(tag__approved=True).distinct('tag')
    serializer_class = TagSerializer

    #http://www.django-rest-framework.org/api-guide/permissions
    permission_classes = (permissions.AllowAny,)#(permissions.IsAuthenticatedOrReadOnly)

class NewTagViewSet(viewsets.ModelViewSet):
    queryset = TagIndiv.objects.filter(tag__approved=True).distinct('tag')
    serializer_class = NewTagSerializer

    #http://www.django-rest-framework.org/api-guide/permissions
    permission_classes = (permissions.AllowAny,)#(permissions.IsAuthenticatedOrReadOnly)

class TagCountViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = TagCountSerializer
    model = Tag

    def get_queryset(self):
        return Tag.objects.filter(approved = True).values('dataset','tag').annotate(num_tags = Count('id'))

class DatasetViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Dataset.objects.all()
    serializer_class = DatasetSerializer

class MapPointViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = MapPointSerializer
    model = MapPoint

    def get_queryset(self):
        t = False
        radius = False
        center = False
        matchall = False
        queryset = MapPoint.objects.none()

        for (param, result) in self.request.QUERY_PARAMS.items():
            if param in ['tag','tags']:
                t = result
            elif param == 'match' and result == 'all':
                matchall = True
        if t:
            t = t.split(',')
            if type(t) is not list:
                t = [t]
            if matchall:
                queryset = MapPoint.objects
                for tag in t:
                    try:
                        num = int(tag)
                        queryset = queryset.filter(tagindiv__tag=num)
                    except:
                        queryset = queryset.filter(tagindiv__tag__tag=tag)
            else:
                for tag in t:
                    try:
                        num = int(tag)
                        queryset = queryset | MapPoint.objects.filter(tagindiv__tag=num)
                    except:
                        queryset = queryset | MapPoint.objects.filter(tagindiv__tag__tag=tag)
            queryset = queryset.filter(tagindiv__tag__approved=True)
        else:
            queryset = MapPoint.objects
        bb = {}
        for param, result in self.request.QUERY_PARAMS.items():
            p = param.lower()
            if p == 'dataset':
                try:
                    r = int(result)
                    queryset = queryset.filter(dataset__id__exact = r)
                except:
                    queryset = queryset.filter(dataset__name__icontains = result.strip())
            elif p in ['max_lat','min_lat','max_lon','min_lon']:
                try:
                    r = float(result)
                    #for tolerance
                    minr = r - 0.0000005
                    maxr = r + 0.0000005 
                except:
                    continue
                if p == 'max_lat' or p == 'lat':
                    queryset = queryset.filter(lat__lte = maxr)
                    bb['max_lat'] = maxr
                if p == 'min_lat' or p == 'lat':
                    queryset = queryset.filter(lat__gte = minr)
                    bb['min_lat'] = minr
                    continue
                if p == 'max_lon' or p == 'lon':
                    queryset = queryset.filter(lon__lte = maxr)
                    bb['max_lon'] = maxr
                if p == 'min_lon' or p == 'lon':
                    queryset = queryset.filter(lon__gte = minr)
                    bb['min_lon'] = minr
            elif p == 'street':
                queryset = queryset.filter(street__iexact = result)
            elif p == 'city':
                print result
                queryset = queryset.filter(city__iexact = result)
            elif p == 'state':
                queryset = queryset.filter(state__iexact = result)
            elif p == 'county':
                queryset = queryset.filter(county__iexact = result)
            elif p in ['zipcode','zip','zip_code']:
                queryset = queryset.filter(zipcode__iexact = result)
            elif param == 'radius':
                try:
                    radius = int(result)
                except:
                    return HttpResponseBadRequest('Invalid radius. Only integers accepted.' )
            elif param == 'center':
                temp = result.split(',')
                try:
                    if len(temp) != 2:
                        raise 
                    temp[0] = float(temp[0])
                    temp[1] = float(temp[1])
                    center = Point(temp[0],temp[1])
                except:
                    return HttpResponseBadRequest('Invalid center. Format is: center=lon,lat' )


        if 'max_lat' in bb and 'min_lat' in bb and 'max_lon' in bb and 'min_lon' in bb:
            geom = Polygon.from_bbox((bb['min_lon'],bb['min_lat'],bb['max_lon'],bb['max_lat']))
            queryset = queryset.filter(point__within=geom)

        if radius and center:
{'fraction_non_alphanumeric': 0.09736951983298539, 'fraction_numerical': 0.0040083507306889355, 'mean_word_length': 1.810607838535555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5585285', 'n_tokens_mistral': 3413, 'n_tokens_neox': 3204, 'n_words': 724}