<commit_before>import json
from django.shortcuts import render
from django.http import HttpResponse
from publisher.models import Measurement
from publisher.models import Bus
from django.http import JsonResponse
from django.core.serializers.json import DjangoJSONEncoder
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from publisher.serializers import MeasurementSerializer
from publisher.serializers import MeasurementBatchList

import datetime

from .forms import VisualizeForm

def index(request):
    context = {}
    return render(request, 'publisher/index.html', context)

def about(request):
    context = {}
    return render(request, 'publisher/about.html', context)

def docs(request):
    context = {}
    return render(request, 'publisher/docs.html', context)

def visualize(request):
    """Renders the visualization webpage on GET and returns map data on POST"""
    form = VisualizeForm()
    context = {'form': form}

    if request.method == 'GET':
        return render(request, 'publisher/visualize.html', context)
    if request.method == 'POST':
        bus_name = request.POST.get("id_bus_name", "")
        min_lat = request.POST.get("id_min_lat", "")
        max_lat = request.POST.get("id_max_lat", "")
        min_lng = request.POST.get("id_min_lng", "")
        max_lng = request.POST.get("id_max_lng", "")
        start_time = request.POST.get("id_start_time", "")
        end_time = request.POST.get("id_end_time", "")
        sensor_name = request.POST.get("id_sensor_name_0", "")

        q = Measurement.objects.all()

        if bus_name:
            bus_key = Bus.objects.filter(name__iexact=bus_name).first().pk
            q = q.filter(bus=bus_key)
        if min_lat:
            q = q.filter(lat__gte=min_lat)
        if max_lat:
            q = q.filter(lat__lte=max_lat)
        if min_lng:
            q = q.filter(lat__gte=min_lng)
        if max_lng:
            q = q.filter(lat__lte=max_lng)
        if start_time:
            q = q.filter(time__gte=start_time)
        if end_time:
            q = q.filter(time__lte=end_time)

        data = {'data' : []}
        for o in q:
            d = {}
            d['lat'] = float(o.lat)
            d['lng'] = float(o.lng)
            if sensor_name == 'TEMPERATURE':
                d['value'] = float(o.temperature)
            if sensor_name == 'HUMIDITY':
                d['value'] = float(o.humidity)
            if sensor_name == 'LIGHT':
                d['value'] = float(o.light)
            if sensor_name == 'RAIN':
                d['value'] = float(o.rain)
            data['data'].append(d)

        data['max'] = 30
        now = datetime.datetime.now()
        html = "Data = %s" % data
        return JsonResponse(data)

    else:
        html = "<html><body>This is not nice, lek</body></html>"
        return HttpResponse(html)

class JSONResponse(HttpResponse):
    """
    An HttpResponse that renders its content into JSON.
    """
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

@csrf_exempt
def measurement_list(request):
    """
    get:
    List all code measurements

    post:
    create a new measurement
    """
    if request.method == 'GET':
        measurements = Measurement.objects.all()
        serializer = MeasurementSerializer(measurements, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = MeasurementSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)<commit_msg>Last API made<commit_after>import json
from django.shortcuts import render
from django.http import HttpResponse
from publisher.models import Measurement
from publisher.models import Bus
from django.http import JsonResponse
from django.core.serializers.json import DjangoJSONEncoder
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from publisher.serializers import MeasurementSerializer
from publisher.serializers import MeasurementBatchList

import datetime

from .forms import VisualizeForm

def index(request):
    context = {}
    return render(request, 'publisher/index.html', context)

def about(request):
    context = {}
    return render(request, 'publisher/about.html', context)

def docs(request):
    context = {}
    return render(request, 'publisher/docs.html', context)

def visualize(request):
    """Renders the visualization webpage on GET and returns map data on POST"""
    form = VisualizeForm()
    context = {'form': form}

    if request.method == 'GET':
        return render(request, 'publisher/visualize.html', context)
    if request.method == 'POST':
        bus_name = request.POST.get("id_bus_name", "")
        min_lat = request.POST.get("id_min_lat", "")
        max_lat = request.POST.get("id_max_lat", "")
        min_lng = request.POST.get("id_min_lng", "")
        max_lng = request.POST.get("id_max_lng", "")
        start_time = request.POST.get("id_start_time", "")
        end_time = request.POST.get("id_end_time", "")
        sensor_name = request.POST.get("id_sensor_name_0", "")

        q = Measurement.objects.all()

        if bus_name:
            bus_key = Bus.objects.filter(name__iexact=bus_name).first().pk
            q = q.filter(bus=bus_key)
        if min_lat:
            q = q.filter(lat__gte=min_lat)
        if max_lat:
            q = q.filter(lat__lte=max_lat)
        if min_lng:
            q = q.filter(lat__gte=min_lng)
        if max_lng:
            q = q.filter(lat__lte=max_lng)
        if start_time:
            q = q.filter(time__gte=start_time)
        if end_time:
            q = q.filter(time__lte=end_time)

        data = {'data' : []}
        for o in q:
            d = {}
            d['lat'] = float(o.lat)
            d['lng'] = float(o.lng)
            if sensor_name == 'TEMPERATURE':
                d['value'] = float(o.temperature)
            if sensor_name == 'HUMIDITY':
                d['value'] = float(o.humidity)
            if sensor_name == 'LIGHT':
                d['value'] = float(o.light)
            if sensor_name == 'RAIN':
                d['value'] = float(o.rain)
            data['data'].append(d)

        data['max'] = 30
        now = datetime.datetime.now()
        html = "Data = %s" % data
        return JsonResponse(data)

    else:
        html = "<html><body>This is not nice, lek</body></html>"
        return HttpResponse(html)

class JSONResponse(HttpResponse):
    """
    An HttpResponse that renders its content into JSON.
    """
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

@csrf_exempt
def measurement_list(request):
    """
    get:
    List all code measurements

    post:
    create a new measurement
    """
    if request.method == 'GET':
        bus_name = request.GET.get("bus_name", "")
        min_lat = request.GET.get("min_lat", "")
        max_lat = request.GET.get("max_lat", "")
        min_lng = request.GET.get("min_lng", "")
        max_lng = request.GET.get("max_lng", "")
        start_time = request.GET.get("start_time", "")
        end_time = request.GET.get("end_time", "")
        sensor_name = request.GET.get("sensor_name", "")

        q = Measurement.objects.all()

        if bus_name:
            bus_key = Bus.objects.filter(name__iexact=bus_name).first().pk
            q = q.filter(bus=bus_key)
        if min_lat:
            q = q.filter(lat__gte=min_lat)
        if max_lat:
            q = q.filter(lat__lte=max_lat)
        if min_lng:
            q = q.filter(lat__gte=min_lng)
        if max_lng:
            q = q.filter(lat__lte=max_lng)
        if start_time:
            q = q.filter(time__gte=start_time)
        if end_time:
            q = q.filter(time__lte=end_time)
        serializer = MeasurementSerializer(q, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = MeasurementSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
{'fraction_non_alphanumeric': 0.1214302298583701, 'fraction_numerical': 0.0013930810308799629, 'mean_word_length': 2.510594947025265, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1202303', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2591, 'n_words': 563}