<s> from django . core . management . base import BaseCommand <EOL> import reversion <EOL> from nodeconductor . quotas . models import Quota <EOL> class Command ( BaseCommand ) : <EOL> help = "<STR_LIT>" <EOL> def handle ( self , * args , ** options ) : <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> duplicates = sum ( [ self . get_quota_duplicate_versions ( quota ) for quota in Quota . objects . all ( ) ] , [ ] ) <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> if not duplicates : <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> else : <EOL> self . stdout . write ( '<STR_LIT>' % len ( duplicates ) ) <EOL> while True : <EOL> delete = raw_input ( '<STR_LIT>' ) or '<STR_LIT:y>' <EOL> if delete . lower ( ) not in ( '<STR_LIT:y>' , '<STR_LIT:n>' ) : <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> else : <EOL> delete = delete . lower ( ) == '<STR_LIT:y>' <EOL> break <EOL> if delete : <EOL> for duplicate in duplicates : <EOL> duplicate . delete ( ) <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> else : <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> def get_quota_duplicate_versions ( self , quota ) : <EOL> versions = reversion . get_for_object ( quota ) . order_by ( '<STR_LIT>' ) <EOL> if not versions : <EOL> return [ ] <EOL> duplicates = [ ] <EOL> last_version = versions [ <NUM_LIT:0> ] <EOL> for version in versions [ <NUM_LIT:1> : ] : <EOL> next_version = version <EOL> if self . are_versions_equal ( quota , last_version , next_version ) : <EOL> duplicates . append ( next_version ) <EOL> else : <EOL> last_version = next_version <EOL> return duplicates <EOL> def are_versions_equal ( self , quota , v1 , v2 ) : <EOL> o1 = v1 . object_version . object <EOL> o2 = v2 . object_version . object <EOL> return all ( [ getattr ( o1 , f ) == getattr ( o2 , f ) for f in quota . get_version_fields ( ) ] ) </s>
