<s> """<STR_LIT>""" <EOL> from dal_contenttypes . fields import ( <EOL> ContentTypeModelMultipleFieldMixin , <EOL> GenericModelMixin , <EOL> ) <EOL> from django import forms <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from queryset_sequence import QuerySetSequence <EOL> class QuerySetSequenceFieldMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset_for_content_type ( self , content_type_id ) : <EOL> """<STR_LIT>""" <EOL> content_type = ContentType . objects . get_for_id ( content_type_id ) <EOL> for queryset in self . queryset . query . _querysets : <EOL> if queryset . model == content_type . model_class ( ) : <EOL> return queryset <EOL> def raise_invalid_choice ( self , params = None ) : <EOL> """<STR_LIT>""" <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> params = params , <EOL> ) <EOL> def get_content_type_id_object_id ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value . split ( '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> class QuerySetSequenceModelField ( GenericModelMixin , <EOL> QuerySetSequenceFieldMixin , <EOL> forms . ModelChoiceField ) : <EOL> """<STR_LIT>""" <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not value : <EOL> return value <EOL> content_type_id , object_id = self . get_content_type_id_object_id ( value ) <EOL> queryset = self . get_queryset_for_content_type ( content_type_id ) <EOL> if queryset is None : <EOL> self . raise_invalid_choice ( ) <EOL> try : <EOL> return queryset . get ( pk = object_id ) <EOL> except queryset . model . DoesNotExist : <EOL> self . raise_invalid_choice ( ) <EOL> class QuerySetSequenceModelMultipleField ( ContentTypeModelMultipleFieldMixin , <EOL> QuerySetSequenceFieldMixin , <EOL> forms . ModelMultipleChoiceField ) : <EOL> """<STR_LIT>""" <EOL> def _deduplicate_values ( self , value ) : <EOL> try : <EOL> return frozenset ( value ) <EOL> except TypeError : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT:list>' ] , <EOL> code = '<STR_LIT:list>' , <EOL> ) <EOL> def _get_ctype_objects ( self , values ) : <EOL> pks = { } <EOL> for val in values : <EOL> content_type_id , object_id = self . get_content_type_id_object_id ( <EOL> val ) <EOL> pks . setdefault ( content_type_id , [ ] ) <EOL> pks [ content_type_id ] . append ( object_id ) <EOL> return pks <EOL> def _get_queryset_for_pks ( self , pks ) : <EOL> querysets = [ ] <EOL> for content_type_id , object_ids in pks . items ( ) : <EOL> queryset = self . get_queryset_for_content_type ( content_type_id ) <EOL> if queryset is None : <EOL> self . raise_invalid_choice ( <EOL> params = dict ( <EOL> value = '<STR_LIT>' % ( content_type_id , object_ids [ <NUM_LIT:0> ] ) <EOL> ) <EOL> ) <EOL> querysets . append ( queryset . filter ( pk__in = object_ids ) ) <EOL> return QuerySetSequence ( * querysets ) <EOL> def _check_values ( self , value ) : <EOL> values = self . _deduplicate_values ( value ) <EOL> pks = self . _get_ctype_objects ( values ) <EOL> queryset = self . _get_queryset_for_pks ( pks ) <EOL> fetched_values = [ <EOL> '<STR_LIT>' % ( ContentType . objects . get_for_model ( o ) . pk , o . pk ) <EOL> for o in queryset <EOL> ] <EOL> for val in value : <EOL> if val not in fetched_values : <EOL> self . raise_invalid_choice ( params = { '<STR_LIT:value>' : val } ) <EOL> return queryset </s>
