--- /dev/null
+# 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)
# 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):
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:
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')
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):
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,))
from astakos.im.models import Invitation
+from ._common import format_bool
+
class Command(BaseCommand):
help = "List users"
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)
from astakos.im.models import AstakosUser
+from ._common import format_bool
+
class Command(BaseCommand):
help = "List users"
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)
from django.core.management.base import BaseCommand, CommandError
-from astakos.im.models import AstakosUser
+from ._common import get_user
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
# 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):
# 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):