Move common code to _common.py admin
authorGiorgos Verigakis <verigak@gmail.com>
Tue, 21 Feb 2012 13:15:06 +0000 (15:15 +0200)
committerGiorgos Verigakis <verigak@gmail.com>
Tue, 21 Feb 2012 13:15:06 +0000 (15:15 +0200)
astakos/im/management/commands/_common.py [new file with mode: 0644]
astakos/im/management/commands/activateuser.py
astakos/im/management/commands/createuser.py
astakos/im/management/commands/inviteuser.py
astakos/im/management/commands/listinvitations.py
astakos/im/management/commands/listusers.py
astakos/im/management/commands/modifyuser.py
astakos/im/management/commands/showinvitation.py
astakos/im/management/commands/showuser.py

diff --git a/astakos/im/management/commands/_common.py b/astakos/im/management/commands/_common.py
new file mode 100644 (file)
index 0000000..e068a9f
--- /dev/null
@@ -0,0 +1,62 @@
+# Copyright 2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+from datetime import datetime
+
+from django.utils.timesince import timesince, timeuntil
+
+from astakos.im.models import AstakosUser
+
+
+def get_user(email_or_id):
+    try:
+        if email_or_id.isdigit():
+            return AstakosUser.objects.get(id=int(email_or_id))
+        else:
+            return AstakosUser.objects.get(email=email_or_id)
+    except AstakosUser.DoesNotExist:
+        return None
+
+
+def format_bool(b):
+    return 'YES' if b else 'NO'
+
+
+def format_date(d):
+    if not d:
+        return ''
+
+    if d < datetime.now():
+        return timesince(d) + ' ago'
+    else:
+        return 'in ' + timeuntil(d)
index 4d8cd01..5b5bf8b 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from optparse import make_option
-
 from django.core.management.base import BaseCommand, CommandError
 
 from astakos.im.admin.functions import activate
-from astakos.im.models import AstakosUser
+
+from ._common import get_user
     
 
 class Command(BaseCommand):
@@ -48,15 +47,9 @@ class Command(BaseCommand):
             raise CommandError("No user was given")
         
         for email_or_id in args:
-            try:
-                if email_or_id.isdigit():
-                    user = AstakosUser.objects.get(id=int(email_or_id))
-                else:
-                    user = AstakosUser.objects.get(email=email_or_id)
-            except AstakosUser.DoesNotExist:
-                field = 'id' if email_or_id.isdigit() else 'email'
-                msg = "Unknown user with %s '%s'" % (field, email_or_id)
-                self.stderr.write(msg + '\n')
+            user = get_user(email_or_id)
+            if not user:
+                self.stderr.write("Unknown user '%s'\n" % (email_or_id,))
                 continue
             
             if user.is_active:
index 50bbfb1..528d41c 100644 (file)
@@ -79,6 +79,12 @@ class Command(BaseCommand):
         if password is None:
             password = generate_password()
         
+        try:
+            AstakosUser.objects.get(email=email)
+            raise CommandError("A user with this email already exists")
+        except AstakosUser.DoesNotExist:
+            pass
+        
         user = AstakosUser(username=username, first_name=first, last_name=last,
                            email=email, affiliation=affiliation,
                            provider='local')
index c3b8cd2..c9b2f91 100644 (file)
@@ -38,7 +38,8 @@ from smtplib import SMTPException
 from django.core.management.base import BaseCommand, CommandError
 
 from astakos.im.admin.functions import invite
-from astakos.im.models import AstakosUser
+
+from ._common import get_user
 
 
 class Command(BaseCommand):
@@ -49,21 +50,14 @@ class Command(BaseCommand):
         if len(args) != 3:
             raise CommandError("Invalid number of arguments")
         
-        email_or_id = args[0]
-        email = args[1]
-        realname = args[2]
-        
-        try:
-            if email_or_id.isdigit():
-                inviter = AstakosUser.objects.get(id=int(email_or_id))
-            else:
-                inviter = AstakosUser.objects.get(email=email_or_id)
-        except AstakosUser.DoesNotExist:
-            field = 'id' if email_or_id.isdigit() else 'email'
-            msg = "Unknown inviter with %s '%s'" % (field, email_or_id)
-            raise CommandError(msg + '\n')
+        user = get_user(args[0])
+        if not user:
+            raise CommandError("Unknown inviter")
         
         if inviter.invitations > 0:
+            email = args[1]
+            realname = args[2]
+            
             try:
                 invite(inviter, email, realname)
                 self.stdout.write("Invitation sent to '%s'\n" % (email,))
index e28480a..eb1b1c1 100644 (file)
@@ -37,6 +37,8 @@ from django.core.management.base import BaseCommand, CommandError
 
 from astakos.im.models import Invitation
 
+from ._common import format_bool
+
 
 class Command(BaseCommand):
     help = "List users"
@@ -67,8 +69,8 @@ class Command(BaseCommand):
         for invitation in invitations:
             id = str(invitation.id)
             code = str(invitation.code)
-            used = 'YES' if invitation.is_accepted else 'NO'
-            consumed = 'YES' if invitation.is_consumed else 'NO'
+            used = format_bool(invitation.is_accepted)
+            consumed = format_bool(invitation.is_consumed)
             fields = (id, invitation.username, invitation.realname,
                       code, used, consumed)
             
index 34bc9b1..1325c61 100644 (file)
@@ -37,6 +37,8 @@ from django.core.management.base import BaseCommand, CommandError
 
 from astakos.im.models import AstakosUser
 
+from ._common import format_bool
+
 
 class Command(BaseCommand):
     help = "List users"
@@ -73,8 +75,8 @@ class Command(BaseCommand):
         
         for user in users:
             id = str(user.id)
-            active = 'YES' if user.is_active else 'NO'
-            admin = 'YES' if user.is_superuser else 'NO'
+            active = format_bool(user.is_active)
+            admin = format_bool(user.is_superuser)
             fields = (id, user.email, user.realname, user.affiliation, active,
                       admin)
             
index 452ffcf..852b549 100644 (file)
@@ -35,7 +35,7 @@ from optparse import make_option
 
 from django.core.management.base import BaseCommand, CommandError
 
-from astakos.im.models import AstakosUser
+from ._common import get_user
 
 
 class Command(BaseCommand):
@@ -72,16 +72,9 @@ class Command(BaseCommand):
         if len(args) != 1:
             raise CommandError("Please provide a user_id or email")
         
-        email_or_id = args[0]
-        try:
-            if email_or_id.isdigit():
-                user = AstakosUser.objects.get(id=int(email_or_id))
-            else:
-                user = AstakosUser.objects.get(email=email_or_id)
-        except AstakosUser.DoesNotExist:
-            field = 'id' if email_or_id.isdigit() else 'email'
-            msg = "Unknown user with %s '%s'" % (field, email_or_id)
-            raise CommandError(msg)
+        user = get_user(args[0])
+        if not user:
+            raise CommandError("Unknown user")
         
         if options.get('admin'):
             user.is_superuser = True
index 758288f..be951e3 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from datetime import datetime
-from optparse import make_option
-
 from django.core.management.base import BaseCommand, CommandError
-from django.utils.timesince import timesince, timeuntil
 
 from astakos.im.models import Invitation
 
-
-def format_bool(b):
-    return 'YES' if b else 'NO'
-
-def format_date(d):
-    if not d:
-        return ''
-    
-    if d < datetime.now():
-        return timesince(d) + ' ago'
-    else:
-        return 'in ' + timeuntil(d)
+from ._common import format_bool, format_date
 
 
 class Command(BaseCommand):
index dd714b4..bf2e9bd 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from datetime import datetime
-from optparse import make_option
-
 from django.core.management.base import BaseCommand, CommandError
-from django.utils.timesince import timesince, timeuntil
 
 from astakos.im.models import AstakosUser
 
-
-def format_bool(b):
-    return 'YES' if b else 'NO'
-
-def format_date(d):
-    if d < datetime.now():
-        return timesince(d) + ' ago'
-    else:
-        return 'in ' + timeuntil(d)
+from ._common import format_bool, format_date
 
 
 class Command(BaseCommand):