Commit 397d5cbe authored by Giorgos Verigakis's avatar Giorgos Verigakis
Browse files

Move common code to _common.py

parent add3d318
# 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)
......@@ -31,12 +31,11 @@
# 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:
......
......@@ -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')
......
......@@ -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,))
......
......@@ -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)
......
......@@ -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)
......
......@@ -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
......
......@@ -31,26 +31,11 @@
# 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):
......
......@@ -31,23 +31,11 @@
# 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):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment