<s> import json <EOL> from psycopg2 . extras import Json <EOL> from . forms import JSONField as JSONFormField <EOL> from . lookups import ( DataContains , ContainedBy , HasKey , HasKeys , HasAnyKeys ) <EOL> from django . core import exceptions <EOL> from django . db . models import Field , Transform <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class JSONField ( Field ) : <EOL> empty_strings_allowed = False <EOL> description = _ ( '<STR_LIT>' ) <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> def db_type ( self , connection ) : <EOL> return '<STR_LIT>' <EOL> def get_transform ( self , name ) : <EOL> transform = super ( JSONField , self ) . get_transform ( name ) <EOL> if transform : <EOL> return transform <EOL> return KeyTransformFactory ( name ) <EOL> def get_prep_value ( self , value ) : <EOL> if value is not None : <EOL> return Json ( value ) <EOL> return value <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if lookup_type in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return value <EOL> if isinstance ( value , ( dict , list ) ) : <EOL> return Json ( value ) <EOL> return super ( JSONField , self ) . get_prep_lookup ( lookup_type , value ) <EOL> def validate ( self , value , model_instance ) : <EOL> super ( JSONField , self ) . validate ( value , model_instance ) <EOL> try : <EOL> json . dumps ( value ) <EOL> except TypeError : <EOL> raise exceptions . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> params = { '<STR_LIT:value>' : value } , <EOL> ) <EOL> def value_to_string ( self , obj ) : <EOL> value = self . value_from_object ( obj ) <EOL> return value <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : JSONFormField } <EOL> defaults . update ( kwargs ) <EOL> return super ( JSONField , self ) . formfield ( ** defaults ) <EOL> JSONField . register_lookup ( DataContains ) <EOL> JSONField . register_lookup ( ContainedBy ) <EOL> JSONField . register_lookup ( HasKey ) <EOL> JSONField . register_lookup ( HasKeys ) <EOL> JSONField . register_lookup ( HasAnyKeys ) <EOL> class KeyTransform ( Transform ) : <EOL> def __init__ ( self , key_name , * args , ** kwargs ) : <EOL> super ( KeyTransform , self ) . __init__ ( * args , ** kwargs ) <EOL> self . key_name = key_name <EOL> def as_sql ( self , compiler , connection ) : <EOL> key_transforms = [ self . key_name ] <EOL> previous = self . lhs <EOL> while isinstance ( previous , KeyTransform ) : <EOL> key_transforms . insert ( <NUM_LIT:0> , previous . key_name ) <EOL> previous = previous . lhs <EOL> lhs , params = compiler . compile ( previous ) <EOL> if len ( key_transforms ) > <NUM_LIT:1> : <EOL> return "<STR_LIT>" . format ( lhs ) , [ key_transforms ] + params <EOL> try : <EOL> int ( self . key_name ) <EOL> except ValueError : <EOL> lookup = "<STR_LIT>" % self . key_name <EOL> else : <EOL> lookup = "<STR_LIT:%s>" % self . key_name <EOL> return "<STR_LIT>" % ( lhs , lookup ) , params <EOL> class KeyTransformFactory ( object ) : <EOL> def __init__ ( self , key_name ) : <EOL> self . key_name = key_name <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return KeyTransform ( self . key_name , * args , ** kwargs ) </s>
