<s> from __future__ import with_statement <EOL> from contextlib import closing <EOL> from copy import copy <EOL> from datetime import date <EOL> from fcntl import LOCK_EX <EOL> from itertools import chain <EOL> from optparse import ( OptionParser , OptionValueError , <EOL> Option as BaseOption ) <EOL> from time import strptime <EOL> from django . contrib . auth . models import User <EOL> from django . core . management . base import CommandError , LabelCommand <EOL> from django_lean . lean_segments . utils import get_segments <EOL> from django_lean . lockfile import lockfile <EOL> from django_lean . utils import get_current_site <EOL> LOCKFILE = '<STR_LIT>' <EOL> def check_date ( option , opt , value ) : <EOL> try : <EOL> t = strptime ( value , '<STR_LIT>' ) <EOL> return date ( * t [ : <NUM_LIT:3> ] ) <EOL> except ValueError : <EOL> raise OptionValueError ( <EOL> '<STR_LIT>' % ( opt , value ) <EOL> ) <EOL> class Option ( BaseOption ) : <EOL> TYPES = BaseOption . TYPES + ( '<STR_LIT:date>' , ) <EOL> TYPE_CHECKER = copy ( BaseOption . TYPE_CHECKER ) <EOL> TYPE_CHECKER [ '<STR_LIT:date>' ] = check_date <EOL> make_option = Option <EOL> class Command ( LabelCommand ) : <EOL> """<STR_LIT>""" <EOL> ACTIONS = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> option_list = list ( LabelCommand . option_list ) <EOL> option_list . extend ( ( <EOL> make_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' <EOL> ) , <EOL> make_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) , <EOL> make_option ( <EOL> '<STR_LIT>' , type = '<STR_LIT:date>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) , <EOL> make_option ( <EOL> '<STR_LIT>' , type = '<STR_LIT:date>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) , <EOL> make_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> ) ) <EOL> help = """<STR_LIT>""" <EOL> args = '<STR_LIT:action>' <EOL> label = '<STR_LIT:action>' <EOL> def info ( self , message , ** options ) : <EOL> self . log ( message , <NUM_LIT:1> , options ) <EOL> def log ( self , message , level , options ) : <EOL> if int ( options . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) >= level : <EOL> print message <EOL> def handle ( self , * labels , ** options ) : <EOL> if len ( labels ) != <NUM_LIT:1> : <EOL> raise CommandError ( '<STR_LIT>' % self . label ) <EOL> action = labels [ <NUM_LIT:0> ] <EOL> if action not in self . ACTIONS : <EOL> raise CommandError ( '<STR_LIT>' % <EOL> ( self . label . capitalize ( ) , <EOL> action , <EOL> "<STR_LIT:U+002CU+0020>" . join ( self . ACTIONS ) ) ) <EOL> return super ( Command , self ) . handle ( * labels , ** options ) <EOL> def handle_label ( self , label , ** options ) : <EOL> return getattr ( self , '<STR_LIT>' + label ) ( <EOL> segments = get_segments ( ) , <EOL> usernames = options . get ( '<STR_LIT>' ) , <EOL> start_date = options . get ( '<STR_LIT:start>' ) , <EOL> end_date = options . get ( '<STR_LIT:end>' ) , <EOL> ** options <EOL> ) <EOL> def handle_clear ( self , segments , usernames , start_date , end_date , <EOL> ** options ) : <EOL> """<STR_LIT>""" <EOL> with lockfile ( LOCKFILE , LOCK_EX , wait = True ) : <EOL> user_segments = [ get_user_segments ( s , usernames , <EOL> start_date = start_date , <EOL> end_date = end_date ) <EOL> for s in segments ] <EOL> for user_segment in chain ( * user_segments ) : <EOL> self . info ( repr ( user_segment ) , ** options ) <EOL> total = sum ( len ( l ) for l in user_segments ) <EOL> self . info ( '<STR_LIT>' % total , ** options ) <EOL> if not total : <EOL> return <EOL> if options . get ( '<STR_LIT>' ) : <EOL> confirm = raw_input ( '<STR_LIT>' ) <EOL> else : <EOL> confirm = '<STR_LIT:y>' <EOL> if confirm and confirm in '<STR_LIT>' : <EOL> for p in chain ( * user_segments ) : <EOL> p . delete ( ) <EOL> self . info ( '<STR_LIT>' % total , ** options ) <EOL> else : <EOL> self . info ( '<STR_LIT>' , ** options ) <EOL> def handle_update ( self , segments , usernames , start_date , end_date , <EOL> ** options ) : <EOL> """<STR_LIT>""" <EOL> with lockfile ( LOCKFILE , LOCK_EX , wait = options [ '<STR_LIT>' ] ) : <EOL> total = <NUM_LIT:0> <EOL> users = get_users ( usernames = usernames ) <EOL> for user in users : <EOL> for segment in segments : <EOL> assigned = segment . objects . assign ( user = user , <EOL> start_date = start_date , <EOL> end_date = end_date ) <EOL> if assigned : <EOL> self . info ( '<STR_LIT>' % ( user , len ( assigned ) ) , <EOL> ** options ) <EOL> total += len ( assigned ) <EOL> self . info ( '<STR_LIT>' % total , ** options ) <EOL> def get_user_segments ( segment , usernames = None , start_date = None , end_date = None ) : <EOL> """<STR_LIT>""" <EOL> result = segment . objects . filter ( site = get_current_site ( ) ) <EOL> if start_date : <EOL> pass <EOL> if end_date : <EOL> pass <EOL> if usernames : <EOL> result = result . filter ( user__in = get_users ( usernames = usernames ) ) <EOL> return result <EOL> def get_users ( usernames = None ) : <EOL> result = User . objects . all ( ) <EOL> if usernames : <EOL> result = result . filter ( username__in = usernames ) <EOL> return result </s>
