<s> """<STR_LIT>""" <EOL> import itertools <EOL> from django . db import connections , models <EOL> from django . db . models . query import QuerySet <EOL> def _get_db_type ( field , connection ) : <EOL> if isinstance ( field , ( models . PositiveSmallIntegerField , <EOL> models . PositiveIntegerField ) ) : <EOL> return field . db_type ( connection ) . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> res = field . db_type ( connection ) <EOL> return res <EOL> def grouper ( iterable , size ) : <EOL> it = iter ( iterable ) <EOL> if size <= <NUM_LIT:0> : <EOL> yield it <EOL> return <EOL> while True : <EOL> chunk = tuple ( itertools . islice ( it , size ) ) <EOL> if not chunk : <EOL> return <EOL> yield chunk <EOL> def validate_fields ( meta , fields ) : <EOL> meta_fields = [ f . attname for f in meta . fields ] <EOL> meta_fields += [ f [ : - <NUM_LIT:3> ] for f in meta_fields if f . endswith ( '<STR_LIT>' ) ] <EOL> invalid_fields = [ ] <EOL> for field in fields or [ ] : <EOL> if field not in meta_fields : <EOL> invalid_fields . append ( field ) <EOL> if invalid_fields : <EOL> raise TypeError ( u'<STR_LIT>' <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( invalid_fields ) ) ) <EOL> def bulk_update ( objs , meta = None , update_fields = None , exclude_fields = None , <EOL> using = '<STR_LIT:default>' , batch_size = None , pk_field = '<STR_LIT>' ) : <EOL> assert batch_size is None or batch_size > <NUM_LIT:0> <EOL> if isinstance ( objs , QuerySet ) : <EOL> if not objs . exists ( ) : <EOL> return <EOL> batch_size = batch_size or objs . count ( ) <EOL> else : <EOL> if not objs : <EOL> return <EOL> batch_size = batch_size or len ( objs ) <EOL> connection = connections [ using ] <EOL> if meta is None : <EOL> meta = objs [ <NUM_LIT:0> ] . _meta <EOL> if pk_field == '<STR_LIT>' : <EOL> pk_field = meta . pk . name <EOL> exclude_fields = exclude_fields or [ ] <EOL> update_fields = update_fields or [ f . attname for f in meta . fields ] <EOL> validate_fields ( meta , update_fields + exclude_fields ) <EOL> fields = [ <EOL> f for f in meta . fields <EOL> if ( ( not isinstance ( f , models . AutoField ) ) <EOL> and ( ( f . attname in update_fields ) or <EOL> ( f . attname . endswith ( '<STR_LIT>' ) and <EOL> f . attname [ : - <NUM_LIT:3> ] in update_fields ) ) ) ] <EOL> fields = [ <EOL> f for f in fields <EOL> if ( ( f . attname not in exclude_fields ) or <EOL> ( f . attname . endswith ( '<STR_LIT>' ) and <EOL> f . attname [ : - <NUM_LIT:3> ] not in exclude_fields ) ) ] <EOL> vendor = connection . vendor <EOL> use_cast = '<STR_LIT>' not in vendor and '<STR_LIT>' not in vendor <EOL> if use_cast : <EOL> case_clause_template = '<STR_LIT>' <EOL> tail_end_template = '<STR_LIT>' <EOL> else : <EOL> case_clause_template = '<STR_LIT>' <EOL> tail_end_template = '<STR_LIT>' <EOL> quote_mark = '<STR_LIT:">' if '<STR_LIT>' not in vendor else '<STR_LIT>' <EOL> case_clause_template = case_clause_template . replace ( '<STR_LIT:">' , quote_mark ) <EOL> lenpks = <NUM_LIT:0> <EOL> for objs_batch in grouper ( objs , batch_size ) : <EOL> pks = [ ] <EOL> case_clauses = { } <EOL> for obj in objs_batch : <EOL> pks . append ( getattr ( obj , pk_field ) ) <EOL> for field in fields : <EOL> column = field . column <EOL> try : <EOL> case_clause = case_clauses [ column ] <EOL> except KeyError : <EOL> case_clause = { <EOL> '<STR_LIT>' : case_clause_template . format ( <EOL> column = column , <EOL> pkcolumn = meta . get_field ( pk_field ) . column ) , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:type>' : _get_db_type ( field , connection = connection ) , <EOL> } <EOL> case_clauses [ column ] = case_clause <EOL> case_clause [ '<STR_LIT>' ] = ( <EOL> case_clause [ '<STR_LIT>' ] . format ( when = "<STR_LIT>" ) <EOL> ) <EOL> case_clause [ '<STR_LIT>' ] . extend ( <EOL> [ getattr ( obj , pk_field ) , <EOL> field . get_db_prep_value ( <EOL> getattr ( obj , field . attname ) , connection ) ] ) <EOL> if pks : <EOL> values = '<STR_LIT:U+002CU+0020>' . join ( <EOL> v [ '<STR_LIT>' ] . format ( when = tail_end_template . format ( type = v [ '<STR_LIT:type>' ] ) ) <EOL> for v in case_clauses . values ( ) ) <EOL> parameters = [ <EOL> param <EOL> for clause in case_clauses . values ( ) <EOL> for param in clause [ '<STR_LIT>' ] ] <EOL> del case_clauses <EOL> pkcolumn = meta . get_field ( pk_field ) . column <EOL> dbtable = '<STR_LIT>' . format ( quote_mark , meta . db_table ) <EOL> in_clause_sql = '<STR_LIT>' . format ( <EOL> '<STR_LIT:U+002CU+0020>' . join ( itertools . repeat ( '<STR_LIT:%s>' , len ( pks ) ) ) ) <EOL> parameters . extend ( pks ) <EOL> sql = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( <EOL> dbtable = dbtable , values = values , pkcolumn = pkcolumn , <EOL> in_clause_sql = in_clause_sql ) ) <EOL> lenpks += len ( pks ) <EOL> del values , pks <EOL> connection . cursor ( ) . execute ( sql , parameters ) <EOL> return lenpks </s>
