<s> import logging <EOL> from bson . errors import InvalidId <EOL> from django . http import JsonResponse , HttpResponseNotFound , HttpResponseBadRequest , HttpResponse <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from api . models import db_model <EOL> from bson import ObjectId <EOL> from api . models . auth import RequireLogin <EOL> from api . models import rule_model <EOL> import json <EOL> from datetime import datetime <EOL> logger = logging . getLogger ( __name__ ) <EOL> @ csrf_exempt <EOL> def rest ( request , * pargs , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> rest_function = get <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> rest_function = post <EOL> elif request . method == '<STR_LIT>' : <EOL> rest_function = put <EOL> elif request . method == '<STR_LIT>' : <EOL> rest_function = delete <EOL> else : <EOL> return JsonResponse ( { "<STR_LIT:error>" : "<STR_LIT>" } ) <EOL> try : <EOL> return rest_function ( request , * pargs , ** kwargs ) <EOL> except TypeError : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> @ RequireLogin ( ) <EOL> def get ( request , rule_id = None ) : <EOL> """<STR_LIT>""" <EOL> if rule_id is None : <EOL> return get_all_rules ( ) <EOL> dbc = db_model . connect ( ) <EOL> try : <EOL> rule = dbc . rule . find_one ( { "<STR_LIT>" : ObjectId ( rule_id ) } ) <EOL> except InvalidId : <EOL> return HttpResponseNotFound ( ) <EOL> if rule is None : <EOL> return HttpResponseNotFound ( ) <EOL> else : <EOL> rule [ '<STR_LIT:id>' ] = str ( rule . pop ( '<STR_LIT>' ) ) <EOL> return JsonResponse ( rule ) <EOL> def get_all_rules ( ) : <EOL> """<STR_LIT>""" <EOL> dbc = db_model . connect ( ) <EOL> rules = [ r for r in dbc . rule . find ( ) ] <EOL> for rule in rules : <EOL> rule [ '<STR_LIT:id>' ] = str ( rule . pop ( '<STR_LIT>' ) ) <EOL> return JsonResponse ( { "<STR_LIT>" : rules } ) <EOL> @ RequireLogin ( ) <EOL> def post ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> new = json . loads ( request . body ) <EOL> except ValueError : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> rule = rule_model . validate ( new ) <EOL> if rule is None : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> else : <EOL> dbc = db_model . connect ( ) <EOL> rule [ '<STR_LIT>' ] = datetime . isoformat ( datetime . now ( ) ) <EOL> rule [ '<STR_LIT>' ] = datetime . isoformat ( datetime . now ( ) ) <EOL> rule_id = str ( dbc . rule . save ( rule ) ) <EOL> r = JsonResponse ( { "<STR_LIT:id>" : rule_id } ) <EOL> r [ '<STR_LIT:location>' ] = "<STR_LIT>" % rule_id <EOL> logger . info ( "<STR_LIT>" % ( rule_id , request . user [ '<STR_LIT:username>' ] ) ) <EOL> return r <EOL> @ RequireLogin ( ) <EOL> def put ( request , rule_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> in_json = json . loads ( request . body ) <EOL> except ValueError : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> dbc = db_model . connect ( ) <EOL> try : <EOL> rule = dbc . rule . find_one ( { "<STR_LIT>" : ObjectId ( rule_id ) } ) <EOL> except InvalidId : <EOL> return HttpResponseNotFound ( ) <EOL> if rule is None : <EOL> return HttpResponseNotFound ( ) <EOL> else : <EOL> in_json [ '<STR_LIT>' ] = rule [ '<STR_LIT>' ] <EOL> rule = rule_model . validate ( in_json ) <EOL> if rule is None : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> else : <EOL> rule [ '<STR_LIT>' ] = ObjectId ( rule_id ) <EOL> rule [ '<STR_LIT>' ] = datetime . isoformat ( datetime . now ( ) ) <EOL> dbc . rule . save ( rule ) <EOL> r = JsonResponse ( { "<STR_LIT:id>" : rule_id } ) <EOL> r [ '<STR_LIT:location>' ] = "<STR_LIT>" % rule_id <EOL> logger . info ( "<STR_LIT>" % ( rule_id , request . user [ '<STR_LIT:username>' ] ) ) <EOL> return r <EOL> @ RequireLogin ( ) <EOL> def delete ( request , rule_id ) : <EOL> """<STR_LIT>""" <EOL> dbc = db_model . connect ( ) <EOL> try : <EOL> rule = dbc . rule . find_one ( { '<STR_LIT>' : ObjectId ( rule_id ) } ) <EOL> except InvalidId : <EOL> return HttpResponseNotFound ( ) <EOL> if rule is None : <EOL> return HttpResponseNotFound ( ) <EOL> else : <EOL> dbc . rule . remove ( { "<STR_LIT>" : ObjectId ( rule_id ) } ) <EOL> logger . info ( "<STR_LIT>" % ( rule_id , request . user [ '<STR_LIT:username>' ] ) ) <EOL> return HttpResponse ( ) </s>
