<commit_before>from .models import Room
from rest_framework import viewsets
from .serializers import RoomDetailSerializer, RoomListSerializer
from rest_framework.decorators import detail_route, list_route
from django.core.exceptions import PermissionDenied
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from django.utils import timezone
from hub.models import Hub
from django.contrib.gis.geos import Point


class RoomViewSet(viewsets.ModelViewSet):

    def get_serializer_class(self):
        if self.action == 'retrieve':
            return RoomDetailSerializer
        return RoomListSerializer

    def get_queryset(self):
        if self.action not in {'retrieve', 'list', 'favorites'}:
            return Room.objects.all()

        lat = self.request.QUERY_PARAMS.get('lat', None)
        lon = self.request.QUERY_PARAMS.get('lon', None)

        if lat is None or lon is None:
            raise ParseError(detail="lat and lon are required query params")

        try:
            lat = float(lat)
            lon = float(lon)
        except ValueError:
            raise ParseError(detail="lat or lon is not float")

        ref_location = Point(lon, lat)

        return Room.objects.all().distance(ref_location).order_by('distance')

    @detail_route(methods=['post'])
    def report_availability(self, request, pk=None):
        room = self.get_object()

        try:
            hub_token = request.DATA['hub_token']
        except:
            raise ParseError(detail='hub_token must be specified')

        try:
            hub = Hub.objects.get(token=hub_token)
        except Hub.DoesNotExist:
            raise PermissionDenied()

        if not hub.room_permissions.filter(id=room.id).exists():
            raise PermissionDenied()
<commit_msg>Filter rooms by availability<commit_after>from .models import Room
from rest_framework import viewsets
from .serializers import RoomDetailSerializer, RoomListSerializer
from rest_framework.decorators import detail_route, list_route
from django.core.exceptions import PermissionDenied
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from django.utils import timezone
from hub.models import Hub
from django.contrib.gis.geos import Point


class RoomViewSet(viewsets.ModelViewSet):

    def get_serializer_class(self):
        if self.action == 'retrieve':
            return RoomDetailSerializer
        return RoomListSerializer

    def get_queryset(self):
        queryset = Room.objects.all()
        if self.action not in {'retrieve', 'list', 'favorites'}:
            return queryset

        lat = self.request.QUERY_PARAMS.get('lat', None)
        lon = self.request.QUERY_PARAMS.get('lon', None)

        if lat is None or lon is None:
            raise ParseError(detail="lat and lon are required query params")

        try:
            lat = float(lat)
            lon = float(lon)
        except ValueError:
            raise ParseError(detail="lat or lon is not float")

        ref_location = Point(lon, lat)

        if self.action == 'list':
            queryset = queryset.filter(available_since__isnull=False)

        return queryset.distance(ref_location).order_by('distance')

    @detail_route(methods=['post'])
    def report_availability(self, request, pk=None):
        room = self.get_object()

        try:
            hub_token = request.DATA['hub_token']
        except:
            raise ParseError(detail='hub_token must be specified')

        try:
            hub = Hub.objects.get(token=hub_token)
        except Hub.DoesNotExist:
            raise PermissionDenied()

        if not hub.room_permissions.filter(id=room.id).exists():
            raise PermissionDenied()

{'fraction_non_alphanumeric': 0.08506113769271664, 'fraction_numerical': 0.0, 'mean_word_length': 2.851586489252815, 'pattern_counts': {'":': 0, '<': 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': '2169147', 'n_tokens_mistral': 1030, 'n_tokens_neox': 998, 'n_words': 276}