<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import progressbar <EOL> from django . db import connections , IntegrityError , router , transaction <EOL> from django . db . models import ForeignKey <EOL> from django . db . models . deletion import Collector <EOL> from django . db . models . signals import pre_delete , pre_save , post_save , post_delete <EOL> from sentry . utils import db <EOL> class InvalidQuerySetError ( ValueError ) : <EOL> pass <EOL> class RangeQuerySetWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , queryset , step = <NUM_LIT:1000> , limit = None , min_id = None , <EOL> order_by = '<STR_LIT>' , callbacks = ( ) ) : <EOL> if queryset . query . low_mark == <NUM_LIT:0> and not ( queryset . query . order_by or queryset . query . extra_order_by ) : <EOL> if limit is None : <EOL> limit = queryset . query . high_mark <EOL> queryset . query . clear_limits ( ) <EOL> else : <EOL> raise InvalidQuerySetError <EOL> self . limit = limit <EOL> if limit : <EOL> self . step = min ( limit , abs ( step ) ) <EOL> self . desc = step < <NUM_LIT:0> <EOL> else : <EOL> self . step = abs ( step ) <EOL> self . desc = step < <NUM_LIT:0> <EOL> self . queryset = queryset <EOL> self . min_value = min_id <EOL> self . order_by = order_by <EOL> self . callbacks = callbacks <EOL> def __iter__ ( self ) : <EOL> max_value = None <EOL> if self . min_value is not None : <EOL> cur_value = self . min_value <EOL> else : <EOL> cur_value = None <EOL> num = <NUM_LIT:0> <EOL> limit = self . limit <EOL> queryset = self . queryset <EOL> if self . desc : <EOL> queryset = queryset . order_by ( '<STR_LIT>' % self . order_by ) <EOL> else : <EOL> queryset = queryset . order_by ( self . order_by ) <EOL> last_object = None <EOL> has_results = True <EOL> while has_results : <EOL> if ( max_value and cur_value >= max_value ) or ( limit and num >= limit ) : <EOL> break <EOL> start = num <EOL> if cur_value is None : <EOL> results = queryset <EOL> elif self . desc : <EOL> results = queryset . filter ( ** { '<STR_LIT>' % self . order_by : cur_value } ) <EOL> elif not self . desc : <EOL> results = queryset . filter ( ** { '<STR_LIT>' % self . order_by : cur_value } ) <EOL> results = list ( results [ <NUM_LIT:0> : self . step ] ) <EOL> for cb in self . callbacks : <EOL> cb ( results ) <EOL> for result in results : <EOL> if result == last_object : <EOL> continue <EOL> yield result <EOL> num += <NUM_LIT:1> <EOL> cur_value = getattr ( result , self . order_by ) <EOL> last_object = result <EOL> if cur_value is None : <EOL> break <EOL> has_results = num > start <EOL> class RangeQuerySetWrapperWithProgressBar ( RangeQuerySetWrapper ) : <EOL> def __iter__ ( self ) : <EOL> total_count = self . queryset . count ( ) <EOL> if not total_count : <EOL> return iter ( [ ] ) <EOL> iterator = super ( RangeQuerySetWrapperWithProgressBar , self ) . __iter__ ( ) <EOL> label = self . queryset . model . _meta . verbose_name_plural . title ( ) <EOL> return iter ( WithProgressBar ( iterator , total_count , label ) ) <EOL> class WithProgressBar ( object ) : <EOL> def __init__ ( self , iterator , count = None , caption = None ) : <EOL> if count is None and hasattr ( iterator , '<STR_LIT>' ) : <EOL> count = len ( iterator ) <EOL> self . iterator = iterator <EOL> self . count = count <EOL> self . caption = unicode ( caption or u'<STR_LIT>' ) <EOL> def __iter__ ( self ) : <EOL> if self . count != <NUM_LIT:0> : <EOL> widgets = [ <EOL> '<STR_LIT>' % ( self . caption , ) , <EOL> progressbar . Percentage ( ) , <EOL> '<STR_LIT:U+0020>' , <EOL> progressbar . Bar ( ) , <EOL> '<STR_LIT:U+0020>' , <EOL> progressbar . ETA ( ) , <EOL> ] <EOL> pbar = progressbar . ProgressBar ( widgets = widgets , maxval = self . count ) <EOL> pbar . start ( ) <EOL> for idx , item in enumerate ( self . iterator ) : <EOL> yield item <EOL> pbar . update ( idx ) <EOL> pbar . finish ( ) <EOL> class EverythingCollector ( Collector ) : <EOL> """<STR_LIT>""" <EOL> def collect ( self , objs , source = None , nullable = False , collect_related = True , <EOL> source_attr = None , reverse_dependency = False ) : <EOL> new_objs = self . add ( objs ) <EOL> if not new_objs : <EOL> return <EOL> model = type ( new_objs [ <NUM_LIT:0> ] ) <EOL> concrete_model = model . _meta . concrete_model <EOL> for ptr in concrete_model . _meta . parents . iteritems ( ) : <EOL> if ptr : <EOL> parent_objs = [ getattr ( obj , ptr . name ) for obj in new_objs ] <EOL> self . collect ( parent_objs , source = model , <EOL> source_attr = ptr . rel . related_name , <EOL> collect_related = False , <EOL> reverse_dependency = True ) <EOL> if collect_related : <EOL> for related in model . _meta . get_all_related_objects ( <EOL> include_hidden = True , include_proxy_eq = True ) : <EOL> sub_objs = self . related_objects ( related , new_objs ) <EOL> self . add ( sub_objs ) <EOL> for relation in model . _meta . many_to_many : <EOL> if not relation . rel . through : <EOL> sub_objs = relation . bulk_related_objects ( new_objs , self . using ) <EOL> self . collect ( sub_objs , <EOL> source = model , <EOL> source_attr = relation . rel . related_name , <EOL> nullable = True ) <EOL> def merge_into ( self , other , callback = lambda x : x , using = '<STR_LIT:default>' ) : <EOL> """<STR_LIT>""" <EOL> s_model = type ( self ) <EOL> collector = EverythingCollector ( using = using ) <EOL> collector . collect ( [ self ] ) <EOL> for model , objects in collector . data . iteritems ( ) : <EOL> fields = set ( <EOL> f . name for f in model . _meta . fields <EOL> if isinstance ( f , ForeignKey ) <EOL> and f . rel . to == s_model <EOL> if f . rel . to <EOL> ) <EOL> if not fields : <EOL> if model == s_model : <EOL> continue <EOL> raise TypeError ( '<STR_LIT>' % model ) <EOL> for obj in objects : <EOL> send_signals = not model . _meta . auto_created <EOL> update_kwargs = { } <EOL> for f_name in fields : <EOL> if getattr ( obj , f_name ) == self : <EOL> update_kwargs [ f_name ] = other <EOL> if not update_kwargs : <EOL> if obj == self : <EOL> continue <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> signal_kwargs = { <EOL> '<STR_LIT>' : model , <EOL> '<STR_LIT>' : obj , <EOL> '<STR_LIT>' : using , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> if send_signals : <EOL> pre_delete . send ( ** signal_kwargs ) <EOL> post_delete . send ( ** signal_kwargs ) <EOL> for k , v in update_kwargs . iteritems ( ) : <EOL> setattr ( obj , k , v ) <EOL> if send_signals : <EOL> pre_save . send ( created = True , ** signal_kwargs ) <EOL> try : <EOL> with transaction . atomic ( using = using ) : <EOL> model . objects . using ( using ) . filter ( pk = obj . pk ) . update ( ** update_kwargs ) <EOL> except IntegrityError : <EOL> model . objects . using ( using ) . filter ( pk = obj . pk ) . delete ( ) <EOL> if send_signals : <EOL> post_save . send ( created = True , ** signal_kwargs ) <EOL> def bulk_delete_objects ( model , limit = <NUM_LIT> , logger = None , ** filters ) : <EOL> connection = connections [ router . db_for_write ( model ) ] <EOL> quote_name = connection . ops . quote_name <EOL> query = [ ] <EOL> params = [ ] <EOL> for column , value in filters . items ( ) : <EOL> query . append ( '<STR_LIT>' % ( quote_name ( column ) , ) ) <EOL> params . append ( value ) <EOL> if logger is not None : <EOL> logger . info ( '<STR_LIT>' , model , column , value ) <EOL> if db . is_postgres ( ) : <EOL> query = """<STR_LIT>""" % dict ( <EOL> query = '<STR_LIT>' . join ( query ) , <EOL> table = model . _meta . db_table , <EOL> limit = limit , <EOL> ) <EOL> elif db . is_mysql ( ) : <EOL> query = """<STR_LIT>""" % dict ( <EOL> query = '<STR_LIT>' . join ( query ) , <EOL> table = model . _meta . db_table , <EOL> limit = limit , <EOL> ) <EOL> else : <EOL> if logger is not None : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> has_more = False <EOL> for obj in model . objects . filter ( ** filters ) [ : limit ] : <EOL> obj . delete ( ) <EOL> has_more = True <EOL> return has_more <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( query , params ) <EOL> return cursor . rowcount > <NUM_LIT:0> </s>
