<s> from django . contrib . contenttypes . models import ContentType <EOL> from lfs . core . utils import import_symbol <EOL> class Criteria ( object ) : <EOL> """<STR_LIT>""" <EOL> def is_valid ( self , request , product = None ) : <EOL> """<STR_LIT>""" <EOL> for criterion in self . get_criteria ( ) : <EOL> criterion . request = request <EOL> criterion . product = product <EOL> if not criterion . is_valid ( ) : <EOL> return False <EOL> return True <EOL> def get_criteria ( self ) : <EOL> """<STR_LIT>""" <EOL> content_type = ContentType . objects . get_for_model ( self ) <EOL> criteria = [ ] <EOL> from lfs . criteria . models import Criterion <EOL> for criterion in Criterion . objects . filter ( content_id = self . id , content_type = content_type ) : <EOL> criteria . append ( criterion . get_content_object ( ) ) <EOL> return criteria <EOL> def save_criteria ( self , request ) : <EOL> """<STR_LIT>""" <EOL> for co in self . get_criteria ( ) : <EOL> co . delete ( ) <EOL> for key , model in request . POST . items ( ) : <EOL> if key . startswith ( "<STR_LIT:type>" ) : <EOL> try : <EOL> id = key . split ( "<STR_LIT:->" ) [ <NUM_LIT:1> ] <EOL> except KeyError : <EOL> continue <EOL> operator = request . POST . get ( "<STR_LIT>" % id ) <EOL> position = request . POST . get ( "<STR_LIT>" % id ) <EOL> criterion_class = import_symbol ( model ) <EOL> criterion = criterion_class . objects . create ( content = self , operator = operator , position = position ) <EOL> if criterion . get_value_type ( ) == criterion . MULTIPLE_SELECT : <EOL> value = request . POST . getlist ( "<STR_LIT>" % id ) <EOL> else : <EOL> value = request . POST . get ( "<STR_LIT>" % id ) <EOL> criterion . update ( value ) </s>
