<s> import json <EOL> import logging <EOL> from functools import wraps <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django_api . json_helpers import JsonResponse <EOL> from django_api . json_helpers import JsonResponseBadRequest <EOL> from django_api . json_helpers import JsonResponseNotFound <EOL> logger = logging . getLogger ( __name__ ) <EOL> class ValidatedRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , orig_request , form ) : <EOL> self . _orig_request = orig_request <EOL> self . _form = form <EOL> def __getattr__ ( self , name ) : <EOL> if name in [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] : <EOL> return self . _form . cleaned_data <EOL> else : <EOL> return object . __getattribute__ ( self . _orig_request , name ) <EOL> def api_accepts ( fields ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapped_func ( request , * args , ** kwargs ) : <EOL> if request . method not in [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] : <EOL> return func ( request , * args , ** kwargs ) <EOL> form_class = type ( '<STR_LIT>' , ( forms . Form , ) , fields . copy ( ) ) <EOL> form = form_class ( getattr ( request , request . method ) ) <EOL> if not form . is_valid ( ) : <EOL> if settings . DEBUG : <EOL> return JsonResponseBadRequest ( <EOL> '<STR_LIT>' % dict ( form . errors ) <EOL> ) <EOL> else : <EOL> logger . warn ( <EOL> '<STR_LIT>' , <EOL> request . path , <EOL> dict ( form . errors ) <EOL> ) <EOL> return func ( request , * args , ** kwargs ) <EOL> for ( field_name , field_instance ) in fields . items ( ) : <EOL> if isinstance ( field_instance , models . Model ) : <EOL> field_type = type ( field_instance ) <EOL> field_id = '<STR_LIT>' % field_name <EOL> if field_id not in request . REQUEST : <EOL> return JsonResponseBadRequest ( <EOL> '<STR_LIT>' % field_name <EOL> ) <EOL> field_pk = int ( request . REQUEST [ field_id ] ) <EOL> try : <EOL> field_value = field_type . objects . get ( pk = field_pk ) <EOL> except field_type . DoesNotExist : <EOL> return JsonResponseNotFound ( <EOL> '<STR_LIT>' % ( <EOL> field_type , field_pk <EOL> ) <EOL> ) <EOL> form . cleaned_data [ field_name ] = field_value <EOL> validated_request = ValidatedRequest ( request , form ) <EOL> return func ( validated_request , * args , ** kwargs ) <EOL> return wrapped_func <EOL> return decorator <EOL> def api_returns ( return_values ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapped_func ( request , * args , ** kwargs ) : <EOL> return_value = func ( request , * args , ** kwargs ) <EOL> if not isinstance ( return_value , JsonResponse ) : <EOL> if settings . DEBUG : <EOL> return JsonResponseBadRequest ( '<STR_LIT>' ) <EOL> else : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> accepted_return_codes = return_values . keys ( ) <EOL> accepted_return_codes . append ( <NUM_LIT> ) <EOL> if return_value . status_code not in accepted_return_codes : <EOL> if settings . DEBUG : <EOL> return JsonResponseBadRequest ( <EOL> '<STR_LIT>' % <EOL> ( return_value . status_code , accepted_return_codes ) <EOL> ) <EOL> else : <EOL> logger . warn ( <EOL> '<STR_LIT>' , <EOL> return_value . status_code , <EOL> accepted_return_codes , <EOL> ) <EOL> return return_value <EOL> return wrapped_func <EOL> return decorator <EOL> def api ( accept_return_dict ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapped_func ( request , * args , ** kwargs ) : <EOL> @ api_accepts ( accept_return_dict [ '<STR_LIT>' ] ) <EOL> @ api_returns ( accept_return_dict [ '<STR_LIT>' ] ) <EOL> def apid_fnc ( request , * args , ** kwargs ) : <EOL> return func ( request , * args , ** kwargs ) <EOL> return apid_fnc ( request , * args , ** kwargs ) <EOL> return wrapped_func <EOL> return decorator <EOL> def validate_json_request ( required_fields ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapped_func ( request , * args , ** kwargs ) : <EOL> try : <EOL> request_dict = json . loads ( request . raw_post_data ) <EOL> except ValueError as e : <EOL> return JsonResponseBadRequest ( '<STR_LIT>' % e ) <EOL> for k in required_fields : <EOL> if k not in request_dict : <EOL> return JsonResponseBadRequest ( <EOL> '<STR_LIT>' % k ) <EOL> return func ( request , request_dict , * args , ** kwargs ) <EOL> return wrapped_func <EOL> return decorator </s>
