<commit_before>import json
from django.contrib.gis.geos import Polygon
from django.utils.translation import ugettext as _
from django.utils.html import mark_safe
from django.views.generic import TemplateView, DetailView
from django.views.decorators import cache
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.views.decorators.clickjacking import xframe_options_exempt
from rest_framework.generics import ListAPIView
from rest_framework.exceptions import ValidationError

from events.models import Event
from groups.models import SupportGroup, SupportGroupSubtype

from . import serializers


class BBoxFilterBackend(object):
    error_message = _("le paramètre bbox devrait être un tableau de 4 flottants")

    def filter_queryset(self, request, queryset, view):
        if not 'bbox' in request.query_params:
            return queryset

        try:
            bbox = json.loads(request.query_params['bbox'])
        except json.JSONDecodeError:
            raise ValidationError(self.error_message)

        if not len(bbox) == 4:
            raise ValidationError(self.error_message)

        try:
            bbox = [float(c) for c in bbox]
        except ValueError:
            raise ValidationError(self.error_message)

        if not (-180. <= bbox[0] < bbox[2] <= 180.) or not (-90. <= bbox[1] < bbox[3] <= 90):
            raise ValidationError(self.error_message)

        bbox = Polygon.from_bbox(bbox)

        return queryset.filter(coordinates__intersects=bbox)


class ZonedView(ListAPIView):
    filter_backends = (BBoxFilterBackend,)


class EventsView(ZonedView):
    serializer_class = serializers.MapEventSerializer
    filter_backends = (BBoxFilterBackend,)
    authentication_classes = []

    def get_queryset(self):
        return Event.objects.upcoming().filter(coordinates__isnull=False).select_related('subtype')

    @cache.cache_control(max_age=60, public=True)
    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)


class GroupsView(ZonedView):
    serializer_class = serializers.MapGroupSerializer
    queryset = SupportGroup.active.filter(coordinates__isnull=False).prefetch_related('subtypes')
    authentication_classes = []

    @cache.cache_control(max_age=60, public=True)
    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)


def get_subtype_information(subtype):
    res = {
        'id': subtype.id,
        'label': subtype.label,
        'color': subtype.color,
        'type': subtype.type,
    }
<commit_msg>Cache groups and events for the map for 5 minutes instead of one<commit_after>import json
from django.contrib.gis.geos import Polygon
from django.utils.translation import ugettext as _
from django.utils.html import mark_safe
from django.views.generic import TemplateView, DetailView
from django.views.decorators import cache
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.views.decorators.clickjacking import xframe_options_exempt
from rest_framework.generics import ListAPIView
from rest_framework.exceptions import ValidationError

from events.models import Event
from groups.models import SupportGroup, SupportGroupSubtype

from . import serializers


class BBoxFilterBackend(object):
    error_message = _("le paramètre bbox devrait être un tableau de 4 flottants")

    def filter_queryset(self, request, queryset, view):
        if not 'bbox' in request.query_params:
            return queryset

        try:
            bbox = json.loads(request.query_params['bbox'])
        except json.JSONDecodeError:
            raise ValidationError(self.error_message)

        if not len(bbox) == 4:
            raise ValidationError(self.error_message)

        try:
            bbox = [float(c) for c in bbox]
        except ValueError:
            raise ValidationError(self.error_message)

        if not (-180. <= bbox[0] < bbox[2] <= 180.) or not (-90. <= bbox[1] < bbox[3] <= 90):
            raise ValidationError(self.error_message)

        bbox = Polygon.from_bbox(bbox)

        return queryset.filter(coordinates__intersects=bbox)


class ZonedView(ListAPIView):
    filter_backends = (BBoxFilterBackend,)


class EventsView(ZonedView):
    serializer_class = serializers.MapEventSerializer
    filter_backends = (BBoxFilterBackend,)
    authentication_classes = []

    def get_queryset(self):
        return Event.objects.upcoming().filter(coordinates__isnull=False).select_related('subtype')

    @cache.cache_control(max_age=300, public=True)
    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)


class GroupsView(ZonedView):
    serializer_class = serializers.MapGroupSerializer
    queryset = SupportGroup.active.filter(coordinates__isnull=False).prefetch_related('subtypes')
    authentication_classes = []

    @cache.cache_control(max_age=300, public=True)
    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)


def get_subtype_information(subtype):
    res = {
        'id': subtype.id,
        'label': subtype.label,
        'color': subtype.color,
        'type': subtype.type,
    }

{'fraction_non_alphanumeric': 0.09580379383023568, 'fraction_numerical': 0.008239126269400269, 'mean_word_length': 3.6732318710832588, 'pattern_counts': {'":': 0, '<': 15, '<?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': '2085242', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1514, 'n_words': 360}