<s> """<STR_LIT>""" <EOL> from django . db . models import Q <EOL> from rest_framework import status <EOL> from rest_framework . views import APIView <EOL> from rest_framework . response import Response <EOL> from geokey . core . decorators import handle_exceptions_for_ajax <EOL> from geokey . users . models import User <EOL> from . . models import Location <EOL> from . . serializers import LocationSerializer <EOL> class LocationAbstractAPIView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> def get_user ( self , request ) : <EOL> """<STR_LIT>""" <EOL> user = request . user <EOL> if user . is_anonymous ( ) : <EOL> user = User . objects . get ( display_name = '<STR_LIT>' ) <EOL> return user <EOL> class LocationsAPIView ( LocationAbstractAPIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def get ( self , request , project_id ) : <EOL> """<STR_LIT>""" <EOL> query = request . GET . get ( '<STR_LIT>' ) <EOL> locations = Location . objects . get_list ( <EOL> self . get_user ( request ) , <EOL> project_id <EOL> ) <EOL> if query : <EOL> query = query . lower ( ) <EOL> locations = locations . filter ( <EOL> Q ( name__icontains = query ) | Q ( description__icontains = query ) <EOL> ) <EOL> serializer = LocationSerializer ( locations , many = True ) <EOL> return Response ( serializer . data , status = status . HTTP_200_OK ) <EOL> class SingleLocationAPIView ( LocationAbstractAPIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def patch ( self , request , project_id , location_id ) : <EOL> """<STR_LIT>""" <EOL> location = Location . objects . get_single ( <EOL> self . get_user ( request ) , <EOL> project_id , <EOL> location_id <EOL> ) <EOL> serializer = LocationSerializer ( <EOL> location , <EOL> data = request . data , <EOL> partial = True <EOL> ) <EOL> if serializer . is_valid ( ) : <EOL> serializer . save ( ) <EOL> return Response ( serializer . data ) <EOL> return Response ( serializer . errors , status = status . HTTP_400_BAD_REQUEST ) </s>
