from optparse import make_option

from django.core.management.base import BaseCommand

from go.base.utils import vumi_api_for_user
from go.base.command_utils import get_user_by_email
from go.vumitools.opt_out import OptOutStore


class Command(BaseCommand):
    help = "List opt-outs from a particular account"

    LOCAL_OPTIONS = (
        make_option('--email-address',
                    dest='email-address',
                    help='Email address for the Vumi Go user'),
    )
    option_list = BaseCommand.option_list + LOCAL_OPTIONS

    def handle(self, *args, **options):
        options = options.copy()
        self.handle_validated(*args, **options)

    def handle_validated(self, *args, **options):
        email_address = options['email-address']

        user = get_user_by_email(email_address)
        user_api = vumi_api_for_user(user)

        self.show_opt_outs(user_api, email_address)

    def show_opt_outs(self, user_api, email_address):
        opt_out_store = OptOutStore(user_api.manager,
                                    user_api.user_account_key)
        opt_outs = opt_out_store.list_opt_outs()

        print "Address Type, Address, Message ID, Timestamp"
        print "============================================"
        for key in opt_outs:
            addr_type, _colon, addr = key.partition(":")
            opt_out = opt_out_store.get_opt_out(addr_type, addr)
            print "%s, %s, %s, %s" % (addr_type, addr, opt_out.message,
                                      opt_out.created_at)

@@ -36,151 +36,69 @@
 rom 
-django.core.management.base import BaseCommand%0A%0Afrom go.base.utils import vumi_api_for_user%0Afrom go.base.command_utils import get_user_by_email
+go.base.command_utils import BaseGoCommand, make_email_option
 %0Afro
@@ -155,24 +155,26 @@
 Command(Base
+Go
 Command):%0A  
@@ -245,17 +245,17 @@
 TIONS = 
-(
+%5B
 %0A       
@@ -264,145 +264,29 @@
 ake_
-option('--email-address',%0A                    dest='email-address',%0A                    help='Email address for the Vumi Go user'),
+email_option()
 %0A    
-)
+%5D%0A
 %0A   
@@ -304,16 +304,18 @@
 t = Base
+Go
 Command.
@@ -328,16 +328,22 @@
 _list + 
+tuple(
 LOCAL_OP
@@ -347,16 +347,17 @@
 _OPTIONS
+)
 %0A%0A    de
@@ -534,139 +534,63 @@
     
-email_address = options%5B'email-address'%5D%0A%0A        user = get_user_by_email(
+_, user_api = self.mk_user_api(options%5B'
 email
-_
+-
 address
-)%0A        user_api = vumi_api_for_user(user
+'%5D
 )%0A%0A 

Make go_list_opt_outs extend BaseGoCommand