Commit 5ebd5989 authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

Extend/Update management commands

parent f30ed7e6
# 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 collections import defaultdict
from django.core.management.base import BaseCommand, CommandError
from django.db import models
from astakos.im.models import AstakosGroup
class Command(BaseCommand):
args = "<group name>"
help = "Show group info"
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Please provide a group name")
group = AstakosGroup.objects
name_or_id = args[0].decode('utf8')
try:
if name_or_id.isdigit():
group = group.get(id=int(name_or_id))
else:
group = group.get(name=name_or_id)
except AstakosGroup.DoesNotExist:
field = 'id' if name_or_id.isdigit() else 'name'
msg = "Unknown user with %s '%s'" % (field, name_or_id)
raise CommandError(msg)
attrs = (
'id',
'name',
'kind',
'homepage',
'desc',
'owners',
'is_enabled',
'max_participants',
'moderation_enabled',
'creation_date',
'issue_date',
'expiration_date',
'approval_date',
'members',
'approved_members',
'quota',
'permissions'
)
for attr in attrs:
val = getattr(group, attr)
if isinstance(val, defaultdict):
val = dict(val)
if isinstance(val, models.Manager):
val = val.all()
line = '%s: %s\n' % (attr.rjust(22), val)
self.stdout.write(line.encode('utf8'))
self.stdout.write('\n')
\ No newline at end of file
......@@ -52,8 +52,8 @@ class Command(BaseCommand):
except Exception, e:
raise CommandError(e)
else:
if r.is_success:
if r.is_success:
self.stdout.write(
'Service created successfully\n')
else:
raise CommandError(r.reason)
raise CommandError(r.reason)
......@@ -49,18 +49,10 @@ def filter_custom_options(options):
class Command(BaseCommand):
args = "<email>"
args = "<email> <first name> <last name>"
help = "Create a user"
option_list = BaseCommand.option_list + (
make_option('--first-name',
dest='first_name',
metavar='NAME',
help="Set user's first name"),
make_option('--last-name',
dest='last_name',
metavar='NAME',
help="Set user's last name"),
make_option('--affiliation',
dest='affiliation',
metavar='AFFILIATION',
......@@ -90,34 +82,37 @@ class Command(BaseCommand):
)
def handle(self, *args, **options):
if len(args) != 1:
if len(args) != 3:
raise CommandError("Invalid number of arguments")
email = args[0].decode('utf8')
email, first_name, last_name = (args[i].decode('utf8') for i in range(3))
try:
validate_email(email)
except ValidationError:
raise CommandError("Invalid email")
u = {'email': email}
u = {'email': email,
'first_name':first_name,
'last_name':last_name
}
u.update(filter_custom_options(options))
if not u.get('password'):
u['password'] = AstakosUser.objects.make_random_password()
try:
c = AstakosCallpoint()
r = c.create_users((u,))
r = c.create_users((u,)).next()
except socket.error, e:
raise CommandError(e)
except ValidationError, e:
raise CommandError(e)
else:
failed = (res for res in r if not res.is_success)
for r in failed:
if not r.is_success:
raise CommandError(r.reason)
if not failed:
self.stdout.write('User created successfully')
if not u.get('password'):
self.stdout.write('with password: %s' % u['password'])
if not r.is_success:
raise CommandError(r.reason)
else:
self.stdout.write('User created successfully ')
if not options.get('password'):
self.stdout.write('with password: %s\n' % u['password'])
else:
self.stdout.write('\n')
\ No newline at end of file
......@@ -31,44 +31,40 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import socket
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import Group
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from astakos.im.models import AstakosUser
from ._common import remove_group_permission
from astakos.im.api.callpoint import AstakosCallpoint
def filter_custom_options(options):
base_dests = list(
getattr(o, 'dest', None) for o in BaseCommand.option_list)
return dict((k, v) for k, v in options.iteritems() if k not in base_dests)
class Command(BaseCommand):
args = "<groupname> <permission> [<permissions> ...]"
help = "Remove group permissions"
args = "<user ID>"
help = "Remove a user"
def handle(self, *args, **options):
if len(args) < 2:
raise CommandError(
"Please provide a group name and at least one permission")
group = None
try:
if args[0].isdigit():
group = Group.objects.get(id=args[0])
else:
group = Group.objects.get(name=args[0])
except Group.DoesNotExist, e:
raise CommandError("Invalid group")
if len(args) != 1:
raise CommandError("Invalid number of arguments")
id = args[0]
if not id.isdigit():
raise CommandError('ID must me an integer')
try:
for pname in args[1:]:
r = remove_group_permission(group, pname)
if r < 0:
self.stdout.write(
'Invalid permission codename: %s\n' % pname)
elif r == 0:
self.stdout.write('Group has not permission: %s\n' % pname)
elif r > 0:
self.stdout.write(
'Permission: %s removed successfully\n' % pname)
except Exception, e:
raise CommandError(e)
user = AstakosUser.objects.get(id=int(id))
except:
msg = "Unknown user with id '%s'" % id
raise CommandError(msg)
else:
user.delete()
self.stdout.write('User deleted successfully\n')
\ No newline at end of file
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