<s> from collections import Iterable , Mapping <EOL> import six <EOL> from sqlalchemy import Column <EOL> from sqlalchemy . orm . attributes import InstrumentedAttribute <EOL> from wtforms import ValidationError <EOL> class Unique ( object ) : <EOL> """<STR_LIT>""" <EOL> field_flags = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , column , get_session = None , message = None ) : <EOL> self . column = column <EOL> self . message = message <EOL> self . get_session = get_session <EOL> @ property <EOL> def query ( self ) : <EOL> self . _check_for_session ( self . model ) <EOL> if self . get_session : <EOL> return self . get_session ( ) . query ( self . model ) <EOL> elif hasattr ( self . model , '<STR_LIT>' ) : <EOL> return getattr ( self . model , '<STR_LIT>' ) <EOL> else : <EOL> raise Exception ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def _check_for_session ( self , model ) : <EOL> if not hasattr ( model , '<STR_LIT>' ) and not self . get_session : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def _syntaxes_as_tuples ( self , form , field , column ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( column , six . string_types ) : <EOL> return ( ( column , getattr ( form . Meta . model , column ) ) , ) <EOL> elif isinstance ( column , Mapping ) : <EOL> return tuple ( <EOL> ( x [ <NUM_LIT:0> ] , self . _syntaxes_as_tuples ( form , field , x [ <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> for x in column . items ( ) <EOL> ) <EOL> elif isinstance ( column , Iterable ) : <EOL> return tuple ( <EOL> self . _syntaxes_as_tuples ( form , field , x ) [ <NUM_LIT:0> ] <EOL> for x in column <EOL> ) <EOL> elif isinstance ( column , ( Column , InstrumentedAttribute ) ) : <EOL> return ( ( column . key , column ) , ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def __call__ ( self , form , field ) : <EOL> columns = self . _syntaxes_as_tuples ( form , field , self . column ) <EOL> self . model = columns [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] . class_ <EOL> query = self . query <EOL> for field_name , column in columns : <EOL> query = query . filter ( column == form [ field_name ] . data ) <EOL> obj = query . first ( ) <EOL> if not hasattr ( form , '<STR_LIT>' ) : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> if obj and not form . _obj == obj : <EOL> if self . message is None : <EOL> self . message = field . gettext ( u'<STR_LIT>' ) <EOL> raise ValidationError ( self . message ) </s>
