<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from rest_framework . compat import unicode_to_repr <EOL> from rest_framework . exceptions import ValidationError <EOL> from rest_framework . utils . representation import smart_repr <EOL> class UniqueValidator ( object ) : <EOL> """<STR_LIT>""" <EOL> message = _ ( '<STR_LIT>' ) <EOL> def __init__ ( self , queryset , message = None ) : <EOL> self . queryset = queryset <EOL> self . serializer_field = None <EOL> self . message = message or self . message <EOL> def set_context ( self , serializer_field ) : <EOL> """<STR_LIT>""" <EOL> self . field_name = serializer_field . source_attrs [ <NUM_LIT:0> ] <EOL> self . instance = getattr ( serializer_field . parent , '<STR_LIT>' , None ) <EOL> def filter_queryset ( self , value , queryset ) : <EOL> """<STR_LIT>""" <EOL> filter_kwargs = { self . field_name : value } <EOL> return queryset . filter ( ** filter_kwargs ) <EOL> def exclude_current_instance ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> if self . instance is not None : <EOL> return queryset . exclude ( pk = self . instance . pk ) <EOL> return queryset <EOL> def __call__ ( self , value ) : <EOL> queryset = self . queryset <EOL> queryset = self . filter_queryset ( value , queryset ) <EOL> queryset = self . exclude_current_instance ( queryset ) <EOL> if queryset . exists ( ) : <EOL> raise ValidationError ( self . message ) <EOL> def __repr__ ( self ) : <EOL> return unicode_to_repr ( '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> smart_repr ( self . queryset ) <EOL> ) ) <EOL> class UniqueTogetherValidator ( object ) : <EOL> """<STR_LIT>""" <EOL> message = _ ( '<STR_LIT>' ) <EOL> missing_message = _ ( '<STR_LIT>' ) <EOL> def __init__ ( self , queryset , fields , message = None ) : <EOL> self . queryset = queryset <EOL> self . fields = fields <EOL> self . serializer_field = None <EOL> self . message = message or self . message <EOL> def set_context ( self , serializer ) : <EOL> """<STR_LIT>""" <EOL> self . instance = getattr ( serializer , '<STR_LIT>' , None ) <EOL> def enforce_required_fields ( self , attrs ) : <EOL> """<STR_LIT>""" <EOL> if self . instance is not None : <EOL> return <EOL> missing = { <EOL> field_name : self . missing_message <EOL> for field_name in self . fields <EOL> if field_name not in attrs <EOL> } <EOL> if missing : <EOL> raise ValidationError ( missing ) <EOL> def filter_queryset ( self , attrs , queryset ) : <EOL> """<STR_LIT>""" <EOL> if self . instance is not None : <EOL> for field_name in self . fields : <EOL> if field_name not in attrs : <EOL> attrs [ field_name ] = getattr ( self . instance , field_name ) <EOL> filter_kwargs = { <EOL> field_name : attrs [ field_name ] <EOL> for field_name in self . fields <EOL> } <EOL> return queryset . filter ( ** filter_kwargs ) <EOL> def exclude_current_instance ( self , attrs , queryset ) : <EOL> """<STR_LIT>""" <EOL> if self . instance is not None : <EOL> return queryset . exclude ( pk = self . instance . pk ) <EOL> return queryset <EOL> def __call__ ( self , attrs ) : <EOL> self . enforce_required_fields ( attrs ) <EOL> queryset = self . queryset <EOL> queryset = self . filter_queryset ( attrs , queryset ) <EOL> queryset = self . exclude_current_instance ( attrs , queryset ) <EOL> checked_values = [ <EOL> value for field , value in attrs . items ( ) if field in self . fields <EOL> ] <EOL> if None not in checked_values and queryset . exists ( ) : <EOL> field_names = '<STR_LIT:U+002CU+0020>' . join ( self . fields ) <EOL> raise ValidationError ( self . message . format ( field_names = field_names ) ) <EOL> def __repr__ ( self ) : <EOL> return unicode_to_repr ( '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> smart_repr ( self . queryset ) , <EOL> smart_repr ( self . fields ) <EOL> ) ) <EOL> class BaseUniqueForValidator ( object ) : <EOL> message = None <EOL> missing_message = _ ( '<STR_LIT>' ) <EOL> def __init__ ( self , queryset , field , date_field , message = None ) : <EOL> self . queryset = queryset <EOL> self . field = field <EOL> self . date_field = date_field <EOL> self . message = message or self . message <EOL> def set_context ( self , serializer ) : <EOL> """<STR_LIT>""" <EOL> self . field_name = serializer . fields [ self . field ] . source_attrs [ <NUM_LIT:0> ] <EOL> self . date_field_name = serializer . fields [ self . date_field ] . source_attrs [ <NUM_LIT:0> ] <EOL> self . instance = getattr ( serializer , '<STR_LIT>' , None ) <EOL> def enforce_required_fields ( self , attrs ) : <EOL> """<STR_LIT>""" <EOL> missing = { <EOL> field_name : self . missing_message <EOL> for field_name in [ self . field , self . date_field ] <EOL> if field_name not in attrs <EOL> } <EOL> if missing : <EOL> raise ValidationError ( missing ) <EOL> def filter_queryset ( self , attrs , queryset ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def exclude_current_instance ( self , attrs , queryset ) : <EOL> """<STR_LIT>""" <EOL> if self . instance is not None : <EOL> return queryset . exclude ( pk = self . instance . pk ) <EOL> return queryset <EOL> def __call__ ( self , attrs ) : <EOL> self . enforce_required_fields ( attrs ) <EOL> queryset = self . queryset <EOL> queryset = self . filter_queryset ( attrs , queryset ) <EOL> queryset = self . exclude_current_instance ( attrs , queryset ) <EOL> if queryset . exists ( ) : <EOL> message = self . message . format ( date_field = self . date_field ) <EOL> raise ValidationError ( { self . field : message } ) <EOL> def __repr__ ( self ) : <EOL> return unicode_to_repr ( '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> smart_repr ( self . queryset ) , <EOL> smart_repr ( self . field ) , <EOL> smart_repr ( self . date_field ) <EOL> ) ) <EOL> class UniqueForDateValidator ( BaseUniqueForValidator ) : <EOL> message = _ ( '<STR_LIT>' ) <EOL> def filter_queryset ( self , attrs , queryset ) : <EOL> value = attrs [ self . field ] <EOL> date = attrs [ self . date_field ] <EOL> filter_kwargs = { } <EOL> filter_kwargs [ self . field_name ] = value <EOL> filter_kwargs [ '<STR_LIT>' % self . date_field_name ] = date . day <EOL> filter_kwargs [ '<STR_LIT>' % self . date_field_name ] = date . month <EOL> filter_kwargs [ '<STR_LIT>' % self . date_field_name ] = date . year <EOL> return queryset . filter ( ** filter_kwargs ) <EOL> class UniqueForMonthValidator ( BaseUniqueForValidator ) : <EOL> message = _ ( '<STR_LIT>' ) <EOL> def filter_queryset ( self , attrs , queryset ) : <EOL> value = attrs [ self . field ] <EOL> date = attrs [ self . date_field ] <EOL> filter_kwargs = { } <EOL> filter_kwargs [ self . field_name ] = value <EOL> filter_kwargs [ '<STR_LIT>' % self . date_field_name ] = date . month <EOL> return queryset . filter ( ** filter_kwargs ) <EOL> class UniqueForYearValidator ( BaseUniqueForValidator ) : <EOL> message = _ ( '<STR_LIT>' ) <EOL> def filter_queryset ( self , attrs , queryset ) : <EOL> value = attrs [ self . field ] <EOL> date = attrs [ self . date_field ] <EOL> filter_kwargs = { } <EOL> filter_kwargs [ self . field_name ] = value <EOL> filter_kwargs [ '<STR_LIT>' % self . date_field_name ] = date . year <EOL> return queryset . filter ( ** filter_kwargs ) </s>
