<s> from __future__ import division <EOL> from django . forms import ValidationError , ModelForm <EOL> from django . forms . models import inlineformset_factory <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from aristotle_mdr import models as MDR <EOL> from aristotle_mdr . contrib . slots . models import Slot , SlotDefinition <EOL> def slot_inlineformset_factory ( model ) : <EOL> class SlotForm ( ModelForm ) : <EOL> class Meta : <EOL> model = Slot <EOL> fields = ( '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT:value>' ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SlotForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT:type>' ] . queryset = SlotDefinition . objects . filter ( <EOL> app_label = model . _meta . app_label , concept_type = model . _meta . model_name <EOL> ) <EOL> base_formset = inlineformset_factory ( <EOL> MDR . _concept , Slot , <EOL> can_delete = True , <EOL> fields = ( '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT:value>' ) , <EOL> extra = <NUM_LIT:1> , <EOL> form = SlotForm <EOL> ) <EOL> class SlotFormset ( base_formset ) : <EOL> def clean ( self ) : <EOL> slot_types_seen = set ( ) <EOL> for form in self . forms : <EOL> if '<STR_LIT:type>' in form . cleaned_data . keys ( ) : <EOL> item = form . cleaned_data [ '<STR_LIT>' ] . item <EOL> slot_type = form . cleaned_data [ '<STR_LIT:type>' ] <EOL> slot_type_in_form = slot_type in slot_types_seen <EOL> slot_types_seen . add ( slot_type ) <EOL> model = item . __class__ <EOL> if not ( slot_type . app_label == model . _meta . app_label and slot_type . concept_type == model . _meta . model_name ) : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) % { '<STR_LIT>' : slot_type . slot_name } ) <EOL> if slot_type . cardinality == SlotDefinition . CARDINALITY . singleton and slot_type_in_form : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) % { '<STR_LIT>' : slot_type . slot_name } ) <EOL> return SlotFormset </s>
