<s> from __future__ import absolute_import , unicode_literals <EOL> from django . core . management . color import no_style <EOL> from django . db import models <EOL> try : <EOL> from django_pg import lookups <EOL> except ImportError : <EOL> lookups = None <EOL> from django_pg . utils . datatypes import CoerciveList <EOL> from django_pg . utils . south import south_installed <EOL> import six <EOL> @ six . add_metaclass ( models . SubfieldBase ) <EOL> class ArrayField ( models . Field ) : <EOL> """<STR_LIT>""" <EOL> if lookups : <EOL> class_lookups = { '<STR_LIT>' : lookups . ArrayLength } <EOL> description = '<STR_LIT>' <EOL> def __init__ ( self , of = models . IntegerField , ** kwargs ) : <EOL> if isinstance ( of , tuple ) and south_installed : <EOL> from south . utils import ask_for_it_by_name as gf <EOL> of = gf ( of [ <NUM_LIT:0> ] ) ( * of [ <NUM_LIT:1> ] , ** of [ <NUM_LIT:2> ] ) <EOL> self . of = of <EOL> if isinstance ( self . of , type ) : <EOL> self . of = self . of ( ) <EOL> kwargs [ '<STR_LIT:null>' ] = True <EOL> super ( ArrayField , self ) . __init__ ( ** kwargs ) <EOL> def create_type ( self , connection ) : <EOL> if hasattr ( self . of , '<STR_LIT>' ) : <EOL> return self . of . create_type ( connection ) <EOL> return <EOL> def create_type_sql ( self , connection , style = no_style ( ) , <EOL> only_if_not_exists = False ) : <EOL> if hasattr ( self . of , '<STR_LIT>' ) : <EOL> return self . of . create_type_sql ( connection , style , <EOL> only_if_not_exists = only_if_not_exists ) <EOL> return '<STR_LIT>' <EOL> def db_type ( self , connection ) : <EOL> """<STR_LIT>""" <EOL> db_subfield = self . of . db_type ( connection ) <EOL> return '<STR_LIT>' % db_subfield <EOL> def get_db_lookup_expression ( self , lookup_type , value , connection ) : <EOL> if lookup_type == '<STR_LIT>' : <EOL> return '<STR_LIT>' % self . db_type ( connection ) <EOL> if lookup_type == '<STR_LIT>' : <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> return '<STR_LIT>' % self . db_type ( connection ) <EOL> return '<STR_LIT>' <EOL> if lookup_type == '<STR_LIT>' : <EOL> if value : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def get_db_prep_lookup ( self , lookup_type , value , connection , <EOL> prepared = False ) : <EOL> if lookup_type == '<STR_LIT>' : <EOL> return [ value ] <EOL> return super ( ArrayField , self ) . get_db_prep_lookup ( <EOL> lookup_type , value , connection , <EOL> prepared = prepared , <EOL> ) <EOL> def get_lookup ( self , lookup_name ) : <EOL> """<STR_LIT>""" <EOL> if lookup_name == '<STR_LIT>' : <EOL> class ArrayContains ( lookups . ArrayContains ) : <EOL> field = self <EOL> return ArrayContains <EOL> if lookup_name == '<STR_LIT>' : <EOL> class ArrayExact ( lookups . ArrayExact ) : <EOL> field = self <EOL> return ArrayExact <EOL> return super ( ArrayField , self ) . get_lookup ( lookup_name ) <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if lookup_type == '<STR_LIT>' : <EOL> try : <EOL> return int ( value ) <EOL> except ValueError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if lookup_type not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' % lookup_type ) <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> value = [ self . of . get_prep_lookup ( '<STR_LIT>' , i ) for i in value ] <EOL> return super ( ArrayField , self ) . get_prep_lookup ( lookup_type , value ) <EOL> def get_prep_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not value : <EOL> return [ ] <EOL> answer = [ ] <EOL> for item in list ( value ) : <EOL> answer . append ( self . of . get_prep_value ( item ) ) <EOL> return answer <EOL> def register_composite ( self , connection , globally = True ) : <EOL> if hasattr ( self . of , '<STR_LIT>' ) : <EOL> return self . of . register_composite ( connection , globally = globally ) <EOL> return <EOL> def south_field_triple ( self ) : <EOL> """<STR_LIT>""" <EOL> from south . modelsinspector import introspector <EOL> double = introspector ( self . of ) <EOL> return ( <EOL> '<STR_LIT>' % ( self . __class__ . __module__ , self . __class__ . __name__ ) , <EOL> [ ] , <EOL> { <EOL> '<STR_LIT>' : ( <EOL> '<STR_LIT>' . format ( <EOL> module = self . of . __class__ . __module__ , <EOL> class_name = self . of . __class__ . __name__ , <EOL> ) , <EOL> double [ <NUM_LIT:0> ] , <EOL> double [ <NUM_LIT:1> ] , <EOL> ) , <EOL> } , <EOL> ) <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return CoerciveList ( self . of . to_python , value ) </s>
