<s> """<STR_LIT>""" <EOL> from django . core . exceptions import ValidationError <EOL> import floppyforms . __future__ as forms <EOL> from ... . import model <EOL> from model_utils import Choices <EOL> from ... utils import mtforms <EOL> from ... lists import filters <EOL> class TagForm ( mtforms . NonFieldErrorsClassFormMixin , mtforms . MTModelForm ) : <EOL> """<STR_LIT>""" <EOL> caseversions = mtforms . MTMultipleChoiceField ( <EOL> required = False , <EOL> widget = mtforms . FilteredSelectMultiple ( <EOL> listordering_template = ( <EOL> "<STR_LIT>" ) , <EOL> filters = [ <EOL> filters . KeywordFilter ( "<STR_LIT:name>" ) , <EOL> filters . ModelFilter ( <EOL> "<STR_LIT>" , <EOL> key = "<STR_LIT>" , <EOL> lookup = "<STR_LIT>" , <EOL> queryset = model . ProductVersion . objects . all ( ) , <EOL> ) , <EOL> filters . ModelFilter ( <EOL> "<STR_LIT>" , lookup = "<STR_LIT>" , queryset = model . Tag . objects . all ( ) ) , <EOL> filters . ModelFilter ( <EOL> "<STR_LIT>" , queryset = model . User . objects . all ( ) ) , <EOL> filters . ChoicesFilter ( <EOL> "<STR_LIT>" , <EOL> choices = Choices ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> ) <EOL> ] , <EOL> ) <EOL> ) <EOL> product = mtforms . MTModelChoiceField ( <EOL> queryset = model . Product . objects . all ( ) , <EOL> choice_attrs = lambda p : { "<STR_LIT>" : p . id } , <EOL> required = False ) <EOL> class Meta : <EOL> model = model . Tag <EOL> fields = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT:description>" ] <EOL> widgets = { <EOL> "<STR_LIT:name>" : forms . TextInput , <EOL> "<STR_LIT>" : forms . Select , <EOL> "<STR_LIT:description>" : mtforms . BareTextarea , <EOL> } <EOL> def clean_caseversions ( self ) : <EOL> """<STR_LIT>""" <EOL> caseversions = dict ( ( unicode ( x . id ) , x ) for x in <EOL> model . CaseVersion . objects . filter ( <EOL> pk__in = self . cleaned_data [ "<STR_LIT>" ] ) ) <EOL> try : <EOL> return [ caseversions [ x ] for x in self . cleaned_data [ "<STR_LIT>" ] ] <EOL> except KeyError as e : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> def save ( self , user = None ) : <EOL> """<STR_LIT>""" <EOL> user = user or self . user <EOL> tag = super ( TagForm , self ) . save ( user = user ) <EOL> if "<STR_LIT>" in self . changed_data : <EOL> TagCV = tag . caseversions . through <EOL> tag . caseversions . clear ( ) <EOL> tcv_list = [ TagCV ( tag = tag , caseversion = cv ) <EOL> for cv in self . cleaned_data [ "<STR_LIT>" ] ] <EOL> TagCV . objects . bulk_create ( tcv_list ) <EOL> return tag <EOL> class EditTagForm ( TagForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( EditTagForm , self ) . __init__ ( * args , ** kwargs ) <EOL> products_tagged = model . Product . objects . filter ( <EOL> cases__versions__tags = self . instance ) . distinct ( ) <EOL> count = products_tagged . count ( ) <EOL> pf = self . fields [ "<STR_LIT>" ] <EOL> self . initial [ "<STR_LIT>" ] = list ( <EOL> self . instance . caseversions . values_list ( <EOL> "<STR_LIT:name>" , flat = True ) ) <EOL> if count > <NUM_LIT:1> : <EOL> pf . queryset = model . Product . objects . none ( ) <EOL> pf . readonly = True <EOL> elif count == <NUM_LIT:1> : <EOL> pf . queryset = products_tagged <EOL> class AddTagForm ( TagForm ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
