<s> from django . http import Http404 <EOL> from rest_framework . generics import GenericAPIView <EOL> from rest_framework . mixins import ListModelMixin <EOL> from rest_framework . response import Response <EOL> from rest_framework import status <EOL> from wq . db . rest . views import SimpleView <EOL> from . util import search <EOL> from . serializers import SearchResultSerializer <EOL> SEARCH_PARAMETER = '<STR_LIT:q>' <EOL> class SearchView ( SimpleView , GenericAPIView , ListModelMixin ) : <EOL> auto = False <EOL> search = None <EOL> content_type = None <EOL> authority_id = None <EOL> serializer_class = SearchResultSerializer <EOL> def get_queryset ( self ) : <EOL> if not self . search : <EOL> return [ ] <EOL> return search ( <EOL> self . search , <EOL> self . auto , <EOL> self . content_type , <EOL> self . authority_id <EOL> ) <EOL> def filter_queryset ( self , queryset ) : <EOL> return queryset <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . search = request . GET . get ( SEARCH_PARAMETER , None ) <EOL> self . auto = request . GET . get ( '<STR_LIT>' , self . auto ) <EOL> self . content_type = request . GET . get ( '<STR_LIT:type>' , self . content_type ) <EOL> self . authority_id = request . GET . get ( '<STR_LIT>' , self . authority_id ) <EOL> response = self . list ( request , args , kwargs ) <EOL> if response . data [ '<STR_LIT:count>' ] == <NUM_LIT:1> and self . auto : <EOL> return self . auto_redirect ( response ) <EOL> else : <EOL> return response <EOL> def auto_redirect ( self , response ) : <EOL> item = response . data [ '<STR_LIT:list>' ] [ <NUM_LIT:0> ] <EOL> return Response ( <EOL> status = status . HTTP_302_FOUND , <EOL> data = { '<STR_LIT:message>' : '<STR_LIT>' , '<STR_LIT>' : self . search } , <EOL> headers = { '<STR_LIT>' : '<STR_LIT:/>' + item [ '<STR_LIT:url>' ] } <EOL> ) <EOL> class DisambiguateView ( SearchView ) : <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . search = kwargs [ '<STR_LIT>' ] <EOL> response = self . list ( request , * args , ** kwargs ) <EOL> if response . data [ '<STR_LIT:count>' ] == <NUM_LIT:0> : <EOL> raise Http404 ( "<STR_LIT>" , self . search ) <EOL> elif response . data [ '<STR_LIT:count>' ] == <NUM_LIT:1> : <EOL> return self . auto_redirect ( response ) <EOL> else : <EOL> response . data [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> return response </s>
