<s> import json <EOL> from cornice import Service <EOL> from pyramid . security import Everyone <EOL> from daybed . backends . exceptions import RecordNotFound , ModelNotFound <EOL> from daybed . schemas . validators import ( RecordSchema , record_validator , <EOL> validate_against_schema ) <EOL> records = Service ( name = '<STR_LIT>' , <EOL> path = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) <EOL> record = Service ( name = '<STR_LIT>' , <EOL> path = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) <EOL> @ records . get ( permission = '<STR_LIT>' ) <EOL> @ records . get ( accept = '<STR_LIT>' , renderer = '<STR_LIT>' , <EOL> permission = '<STR_LIT>' ) <EOL> def get_records ( request ) : <EOL> """<STR_LIT>""" <EOL> model_id = request . matchdict [ '<STR_LIT>' ] <EOL> try : <EOL> request . db . get_model_definition ( model_id ) <EOL> except ModelNotFound : <EOL> request . errors . add ( '<STR_LIT:path>' , model_id , "<STR_LIT>" ) <EOL> request . errors . status = "<STR_LIT>" <EOL> return <EOL> if "<STR_LIT>" not in request . permissions : <EOL> results = request . db . get_records_with_authors ( model_id ) <EOL> results = [ r [ '<STR_LIT>' ] for r in results <EOL> if set ( request . principals ) . intersection ( r [ '<STR_LIT>' ] ) ] <EOL> else : <EOL> results = request . db . get_records ( model_id ) <EOL> return { '<STR_LIT>' : results } <EOL> @ records . post ( validators = record_validator , permission = '<STR_LIT>' ) <EOL> def post_record ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . headers . get ( '<STR_LIT>' , '<STR_LIT:false>' ) == '<STR_LIT:true>' : <EOL> return <EOL> model_id = request . matchdict [ '<STR_LIT>' ] <EOL> if request . credentials_id : <EOL> credentials_id = request . credentials_id <EOL> else : <EOL> credentials_id = Everyone <EOL> record_id = request . db . put_record ( model_id , request . data_clean , <EOL> [ credentials_id ] ) <EOL> request . notify ( '<STR_LIT>' , model_id , record_id ) <EOL> created = u'<STR_LIT>' % ( request . application_url , model_id , <EOL> record_id ) <EOL> request . response . status = "<STR_LIT>" <EOL> request . response . headers [ '<STR_LIT:location>' ] = str ( created ) <EOL> return { '<STR_LIT:id>' : record_id } <EOL> @ records . delete ( permission = '<STR_LIT>' ) <EOL> def delete_records ( request ) : <EOL> """<STR_LIT>""" <EOL> model_id = request . matchdict [ '<STR_LIT>' ] <EOL> try : <EOL> records = request . db . delete_records ( model_id ) <EOL> for record in records : <EOL> request . notify ( '<STR_LIT>' , model_id , record [ '<STR_LIT:id>' ] ) <EOL> except ModelNotFound : <EOL> request . errors . add ( '<STR_LIT:path>' , model_id , "<STR_LIT>" ) <EOL> request . errors . status = "<STR_LIT>" <EOL> return <EOL> return { "<STR_LIT>" : records } <EOL> @ record . get ( permission = '<STR_LIT>' ) <EOL> def get ( request ) : <EOL> """<STR_LIT>""" <EOL> model_id = request . matchdict [ '<STR_LIT>' ] <EOL> record_id = request . matchdict [ '<STR_LIT>' ] <EOL> try : <EOL> return request . db . get_record ( model_id , record_id ) <EOL> except RecordNotFound : <EOL> request . errors . add ( '<STR_LIT:path>' , record_id , "<STR_LIT>" ) <EOL> request . errors . status = "<STR_LIT>" <EOL> @ record . put ( validators = record_validator , permission = '<STR_LIT>' ) <EOL> def put ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . headers . get ( '<STR_LIT>' , '<STR_LIT:false>' ) == '<STR_LIT:true>' : <EOL> return <EOL> model_id = request . matchdict [ '<STR_LIT>' ] <EOL> record_id = request . matchdict [ '<STR_LIT>' ] <EOL> try : <EOL> request . db . get_record ( model_id , record_id ) <EOL> create = True <EOL> except RecordNotFound : <EOL> create = False <EOL> if request . credentials_id : <EOL> credentials_id = request . credentials_id <EOL> else : <EOL> credentials_id = Everyone <EOL> record_id = request . db . put_record ( model_id , request . data_clean , <EOL> [ credentials_id ] , record_id = record_id ) <EOL> event = '<STR_LIT>' if create else '<STR_LIT>' <EOL> request . notify ( event , model_id , record_id ) <EOL> return { '<STR_LIT:id>' : record_id } <EOL> @ record . patch ( permission = '<STR_LIT>' ) <EOL> def patch ( request ) : <EOL> """<STR_LIT>""" <EOL> model_id = request . matchdict [ '<STR_LIT>' ] <EOL> record_id = request . matchdict [ '<STR_LIT>' ] <EOL> if request . credentials_id : <EOL> credentials_id = request . credentials_id <EOL> else : <EOL> credentials_id = Everyone <EOL> try : <EOL> record = request . db . get_record ( model_id , record_id ) <EOL> except RecordNotFound : <EOL> request . errors . add ( '<STR_LIT:path>' , record_id , "<STR_LIT>" ) <EOL> request . errors . status = "<STR_LIT>" <EOL> return <EOL> record . update ( json . loads ( request . body . decode ( '<STR_LIT:utf-8>' ) ) ) <EOL> definition = request . db . get_model_definition ( model_id ) <EOL> validate_against_schema ( request , RecordSchema ( definition ) , record ) <EOL> if not request . errors : <EOL> request . db . put_record ( model_id , record , [ credentials_id ] , record_id ) <EOL> request . notify ( '<STR_LIT>' , model_id , record_id ) <EOL> return { '<STR_LIT:id>' : record_id } <EOL> @ record . delete ( permission = '<STR_LIT>' ) <EOL> def delete ( request ) : <EOL> """<STR_LIT>""" <EOL> model_id = request . matchdict [ '<STR_LIT>' ] <EOL> record_id = request . matchdict [ '<STR_LIT>' ] <EOL> try : <EOL> deleted = request . db . delete_record ( model_id , record_id ) <EOL> request . notify ( '<STR_LIT>' , model_id , record_id ) <EOL> except RecordNotFound : <EOL> request . errors . add ( '<STR_LIT:path>' , record_id , "<STR_LIT>" ) <EOL> request . errors . status = "<STR_LIT>" <EOL> return <EOL> return deleted </s>
