@@ -0,0 +1,2699 @@
+'''%0ACreated on Feb 19, 2013%0A%0A@author: raybesiga%0A'''%0A%0Afrom django.core.management.base import BaseCommand%0Afrom education.models import reschedule_termly_script%0Afrom optparse import OptionParser, make_option%0Afrom education.utils import _next_term_question_date%0Afrom rapidsms.models import Connection%0Afrom unregister.models import Blacklist%0Afrom script.models import Script, ScriptProgress%0A%0Aclass Command(BaseCommand):%0A%0A    option_list = BaseCommand.option_list + (%0A        make_option(%22-d%22, %22--date%22, dest=%22date%22),%0A        make_option(%22-g%22, %22--group%22, dest=%22group%22),%0A        make_option(%22-s%22, %22--script%22, dest=%22script%22),%0A    )%0A    def handle(self, **options):%0A%0A        if not options%5B'date'%5D:%0A            date = raw_input('Date when questions should be sent out -- YYYY-MM-DD:')%0A        else:%0A            date = options%5B'date'%5D%0A        if not options%5B'group'%5D:%0A            group = raw_input('Group -- SMC or Head Teachers:')%0A        else:%0A            group = 'all'%0A        if not options%5B'script'%5D:%0A            script = raw_input('Slug of script you wish to reschedule -- edtrac_p3_enrollment_headteacher_termly')%0A        else:%0A            script = options%5B'script'%5D%0A        %0A        connections = Connection.objects.filter(contact__emisreporter__groups__name__in=%5Bgroup%5D)%0A        script = Script.objects.get(slug=script)%0A        for connection in connections:%0A            try:%0A                Blacklist.objects.get(connection=connection)%0A                print '%25s blacklisted! ' %25 connection.identity%0A            except Blacklist.DoesNotExist:%0A                try:%0A                    ScriptProgress.objects.filter(connection=connection, script=script).exclude(step=None)%0A                except ScriptProgress.DoesNotExist:%0A                    if date:%0A                        d = _next_term_question_date(date=date)%0A                    else:%0A                        if group == 'SMC':%0A                            d = _next_term_question_date(True)%0A                        else:%0A                            d = _next_term_question_date(False)%0A                            %0A                    sp = ScriptProgress.objects.create(script=script, connection=connection)%0A                    sp.set_time(d)%0A                    print 'ScriptProgress for %25s created!!' %25 connection%0A                except ScriptProgress.MultipleObjectsReturned:%0A                    sps = ScriptProgress.objects.filter(connection=connection, script=script)%0A                    i = 0%0A                    for i in range(1, len(sps)):%0A                        sps%5Bi%5D.delete()%0A                        print 'Duplicate progress for %25s deleted!!' %25 connection%0A        %0A        self.stdout.write('%5Cn')%0A        self.stdout.write('Done')

Add clean progress command