from __future__ import absolute_import

from django.core.management.base import BaseCommand
from confirmation.models import Confirmation
from zerver.models import UserProfile, PreregistrationUser, \
    get_user_profile_by_email

class Command(BaseCommand):
    help = "Generate activation links for users and print them to stdout."

    def handle(self, *args, **options):
        duplicates = False
        for email in args:
            try:
                get_user_profile_by_email(email)
                print email + ": There is already a user registered with that address."
                duplicates = True
                continue
            except UserProfile.DoesNotExist:
                pass

        if duplicates:
            return

        for email in args:
            prereg_user = PreregistrationUser(email=email)
            prereg_user.save()
            print email + ": " + Confirmation.objects.get_link_for_object(prereg_user)


@@ -33,16 +33,50 @@
 import%0A%0A
+from optparse import make_option%0A%0A
 from dja
@@ -255,16 +255,27 @@
 by_email
+, get_realm
 %0A%0Aclass 
@@ -380,448 +380,1778 @@
     
-def handle(self, *args, **options):%0A        duplicates = False%0A        for email in args:%0A            try:%0A                get_user_profile_by_email(email)%0A                print email + %22: There is already a user registered with that address.%22%0A                duplicates = True%0A                continue%0A            except UserProfile.DoesNotExist:%0A                pass%0A%0A        if duplicates:%0A            return%0A%0A        for email in args:%0A
+option_list = BaseCommand.option_list + (%0A        make_option('--domain',%0A                    dest='domain',%0A                    type='str',%0A                    help='The realm in which to generate the invites (use for open realms).'),%0A        make_option('--force',%0A                    dest='force',%0A                    action=%22store_true%22,%0A                    default=False,%0A                    help='Override that the domain is restricted to external users.'),%0A        )%0A%0A    def handle(self, *args, **options):%0A        duplicates = False%0A        for email in args:%0A            try:%0A                get_user_profile_by_email(email)%0A                print email + %22: There is already a user registered with that address.%22%0A                duplicates = True%0A                continue%0A            except UserProfile.DoesNotExist:%0A                pass%0A%0A        if duplicates:%0A            return%0A%0A        realm = None%0A        domain = options%5B%22domain%22%5D%0A        if domain:%0A            realm = get_realm(domain)%0A        if not realm:%0A            print %22The realm %25s doesn't exist yet, please create it first.%22 %25 (domain,)%0A            print %22Don't forget default streams!%22%0A            exit(1)%0A%0A        for email in args:%0A            if realm:%0A                if realm.restricted_to_domain and %5C%0A                        domain.lower() != email.split(%22@%22, 1)%5B-1%5D.lower() and %5C%0A                        not options%5B%22force%22%5D:%0A                    print %22You've asked to add an external user (%25s) to a closed realm (%25s).%22 %25 (%0A                        email, domain)%0A                    print %22Are you sure? To do this, pass --force.%22%0A                    exit(1)%0A                else:%0A                    prereg_user = PreregistrationUser(email=email, realm=realm)%0A            else:%0A    
     

work for open realms too.