<commit_before>import json
from collections import OrderedDict
from urllib.parse import urljoin
from io import BytesIO

import mimetypes
from PIL import Image

from django.http import HttpResponse
from django.views.generic import View

from tmstiler.django import DjangoRasterTileLayerManager

from .models import Measurement

SAFECAST_TILELAYER_PREFIX = "/tiles/"  # needs to match urls.py

class Legend:

    def get_color_str(self, value):
        """
        :param value:
        :return:  rgb or hsl color string in the format:

        rgb(255,0,0)
        rgb(100%,0%,0%)

        hsl(hue, saturation%, lightness%)
        where:
            hue is the color given as an angle between 0 and 360 (red=0, green=120, blue=240)
            saturation is a value between 0% and 100% (gray=0%, full color=100%)
            lightness is a value between 0% and 100% (black=0%, normal=50%, white=100%).

        For example, hsl(0,100%,50%) is pure red.
        """
        return "hsl(0,100%,50%)"  # pure red for now...


class SafecastMeasurementsTileView(View):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # create per month layers
        months = Measurement.objects.order_by("date").values_list('date', flat=True).distinct()
        layers = OrderedDict()
        legend = Legend()
        for m in months:
            qs = Measurement.objects.filter(date=m)
            month_layername = m.strftime("%Y%m")
            layers[month_layername] = {
                        "pixel_size": 250,  # currently hard-coded in measurements.management.commands.load_safecast_csv
                        "point_position": "upperleft",
                        "model_queryset": qs,
                        "model_point_fieldname": "location",
                        "model_value_fieldname": "value",
                        "legend_instance": legend,  # returns a hslcolor_str
                        }
        self.tilemgr = DjangoRasterTileLayerManager(layers)

    # def get(self, request):
    #     layername, zoom, x, y, image_format = self.tilemgr.parse_url(request.path)
    #     mimetype, tile_pil_img_object = self.tilemgr.get_tile(layername, zoom, x, y)
    #     image_encoding = image_format.replace(".", "")
    #     image_fileio = BytesIO()
    #     tile_pil_img_object.save(image_fileio, image_encoding)
    #     image_fileio.seek(0)
    #     return HttpResponse(image_fileio, content_type=mimetype)

    def get(self, request):
        # debug for tile image display
        image_fileio = BytesIO()
        tile_pil_img_object = Image.new("RGBA", (256, 256), (255,255,255, 0))
        tile_pil_img_object.save(image_fileio, "png")
        image_fileio.seek(0)
        extension = ".png"
        return HttpResponse(image_fileio, content_type=mimetypes.types_map.get(extension))


def get_month_layers(request):
    months = Measurement.objects.order_by("date").values_list('date', flat=True).distinct()
    layers = []
    for m in months:
        month_layername = m.strftime("%Y%m")
        layer_url = urljoin(SAFECAST_TILELAYER_PREFIX, month_layername)
        layers.append(layer_url)
    return HttpResponse(json.dumps(layers), content_type="application/json")<commit_msg>changing debug tile color to red<commit_after>import json
from collections import OrderedDict
from urllib.parse import urljoin
from io import BytesIO

import mimetypes
from PIL import Image

from django.http import HttpResponse
from django.views.generic import View

from tmstiler.django import DjangoRasterTileLayerManager

from .models import Measurement

SAFECAST_TILELAYER_PREFIX = "/tiles/"  # needs to match urls.py

class Legend:

    def get_color_str(self, value):
        """
        :param value:
        :return:  rgb or hsl color string in the format:

        rgb(255,0,0)
        rgb(100%,0%,0%)

        hsl(hue, saturation%, lightness%)
        where:
            hue is the color given as an angle between 0 and 360 (red=0, green=120, blue=240)
            saturation is a value between 0% and 100% (gray=0%, full color=100%)
            lightness is a value between 0% and 100% (black=0%, normal=50%, white=100%).

        For example, hsl(0,100%,50%) is pure red.
        """
        return "hsl(0,100%,50%)"  # pure red for now...


class SafecastMeasurementsTileView(View):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # create per month layers
        months = Measurement.objects.order_by("date").values_list('date', flat=True).distinct()
        layers = OrderedDict()
        legend = Legend()
        for m in months:
            qs = Measurement.objects.filter(date=m)
            month_layername = m.strftime("%Y%m")
            layers[month_layername] = {
                        "pixel_size": 250,  # currently hard-coded in measurements.management.commands.load_safecast_csv
                        "point_position": "upperleft",
                        "model_queryset": qs,
                        "model_point_fieldname": "location",
                        "model_value_fieldname": "value",
                        "legend_instance": legend,  # returns a hslcolor_str
                        }
        self.tilemgr = DjangoRasterTileLayerManager(layers)

    # def get(self, request):
    #     layername, zoom, x, y, image_format = self.tilemgr.parse_url(request.path)
    #     mimetype, tile_pil_img_object = self.tilemgr.get_tile(layername, zoom, x, y)
    #     image_encoding = image_format.replace(".", "")
    #     image_fileio = BytesIO()
    #     tile_pil_img_object.save(image_fileio, image_encoding)
    #     image_fileio.seek(0)
    #     return HttpResponse(image_fileio, content_type=mimetype)

    def get(self, request):
        # debug for tile image display
        image_fileio = BytesIO()
        tile_pil_img_object = Image.new("RGBA", (256, 256), (255,0,0, 0))
        tile_pil_img_object.save(image_fileio, "png")
        image_fileio.seek(0)
        extension = ".png"
        return HttpResponse(image_fileio, content_type=mimetypes.types_map.get(extension))


def get_month_layers(request):
    months = Measurement.objects.order_by("date").values_list('date', flat=True).distinct()
    layers = []
    for m in months:
        month_layername = m.strftime("%Y%m")
        layer_url = urljoin(SAFECAST_TILELAYER_PREFIX, month_layername)
        layers.append(layer_url)
    return HttpResponse(json.dumps(layers), content_type="application/json")
{'fraction_non_alphanumeric': 0.10749034749034748, 'fraction_numerical': 0.021621621621621623, 'mean_word_length': 2.6587570621468926, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, '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': '512542', 'n_tokens_mistral': 2098, 'n_tokens_neox': 1941, 'n_words': 460}