<s> """<STR_LIT>""" <EOL> import datetime <EOL> from django . conf import settings <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> from django . utils import datetime_safe <EOL> from django . utils import simplejson <EOL> import requests <EOL> class SolrJSONEncoder ( DjangoJSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def default ( self , o ) : <EOL> if isinstance ( o , datetime . datetime ) : <EOL> d = datetime_safe . new_datetime ( o ) <EOL> return d . strftime ( '<STR_LIT>' ) <EOL> else : <EOL> return super ( SolrJSONEncoder , self ) . default ( o ) <EOL> def dumps ( data ) : <EOL> return simplejson . dumps ( data , cls = SolrJSONEncoder ) <EOL> def loads ( data ) : <EOL> return simplejson . loads ( data ) <EOL> class SolrError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response , * args , ** kwargs ) : <EOL> self . status_code = response . status_code <EOL> self . response_body = response . content <EOL> super ( SolrError , self ) . __init__ ( * args , ** kwargs ) <EOL> def __unicode__ ( self ) : <EOL> return self . response_body <EOL> def add ( core , documents , commit = False ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . join ( [ settings . SOLR_ENDPOINT , '<STR_LIT:/>' , core , '<STR_LIT>' ] ) <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } if commit else { } <EOL> response = requests . post ( url , dumps ( documents ) , params = params , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> raise SolrError ( response ) <EOL> return loads ( response . content ) <EOL> def commit ( core ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . join ( [ settings . SOLR_ENDPOINT , '<STR_LIT:/>' , core , '<STR_LIT>' ] ) <EOL> response = requests . post ( url , '<STR_LIT>' , params = { '<STR_LIT>' : '<STR_LIT:true>' } , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> raise SolrError ( response ) <EOL> return loads ( response . content ) <EOL> def delete ( core , q , commit = True ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . join ( [ settings . SOLR_ENDPOINT , '<STR_LIT:/>' , core , '<STR_LIT>' ] ) <EOL> params = { '<STR_LIT>' : '<STR_LIT:true>' } if commit else { } <EOL> response = requests . post ( url , dumps ( { '<STR_LIT>' : { '<STR_LIT>' : q } } ) , params = params , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> raise SolrError ( response ) <EOL> return loads ( response . content ) <EOL> def query ( core , q , limit = <NUM_LIT:10> , offset = <NUM_LIT:0> , sort = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . join ( [ settings . SOLR_ENDPOINT , '<STR_LIT:/>' , core , '<STR_LIT>' ] ) <EOL> response = requests . get ( url , params = { '<STR_LIT:q>' : q , '<STR_LIT>' : '<STR_LIT:1>' , '<STR_LIT:start>' : offset , '<STR_LIT>' : limit , '<STR_LIT>' : sort } , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> raise SolrError ( response ) <EOL> return loads ( response . content ) <EOL> def query_grouped ( core , q , group_field , limit = <NUM_LIT:10> , offset = <NUM_LIT:0> , sort = '<STR_LIT>' , group_limit = settings . PANDA_DEFAULT_SEARCH_ROWS_PER_GROUP , group_offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . join ( [ settings . SOLR_ENDPOINT , '<STR_LIT:/>' , core , '<STR_LIT>' ] ) <EOL> response = requests . get ( url , params = { '<STR_LIT:q>' : q , '<STR_LIT>' : '<STR_LIT:1>' , '<STR_LIT:start>' : offset , '<STR_LIT>' : limit , '<STR_LIT>' : sort , '<STR_LIT>' : '<STR_LIT:true>' , '<STR_LIT>' : group_field , '<STR_LIT>' : group_limit , '<STR_LIT>' : group_offset , '<STR_LIT>' : '<STR_LIT:true>' } , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> raise SolrError ( response ) <EOL> return loads ( response . content ) </s>
