Revision 07a1f977

b/snf-astakos-app/astakos/im/management/commands/group-details.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from collections import defaultdict
35

  
36
from django.core.management.base import BaseCommand, CommandError
37
from django.db import models
38

  
39
from astakos.im.models import AstakosGroup
40

  
41
class Command(BaseCommand):
42
    args = "<group name>"
43
    help = "Show group info"
44

  
45
    def handle(self, *args, **options):
46
        if len(args) != 1:
47
            raise CommandError("Please provide a group name")
48

  
49
        group = AstakosGroup.objects
50
        name_or_id = args[0].decode('utf8')
51
        try:
52
            if name_or_id.isdigit():
53
                group = group.get(id=int(name_or_id))
54
            else:
55
                group = group.get(name=name_or_id)
56
        except AstakosGroup.DoesNotExist:
57
            field = 'id' if name_or_id.isdigit() else 'name'
58
            msg = "Unknown user with %s '%s'" % (field, name_or_id)
59
            raise CommandError(msg)
60
        
61
        attrs = (
62
            'id',
63
            'name',
64
            'kind',
65
            'homepage', 
66
            'desc',
67
            'owners',
68
            'is_enabled',
69
            'max_participants',
70
            'moderation_enabled',
71
            'creation_date',
72
            'issue_date',
73
            'expiration_date',
74
            'approval_date',
75
            'members',
76
            'approved_members',
77
            'quota',
78
            'permissions'
79
        )
80

  
81
        for attr in attrs:
82
            val = getattr(group, attr)
83
            if isinstance(val, defaultdict):
84
                val = dict(val)
85
            if isinstance(val, models.Manager):
86
                val = val.all()
87
            line = '%s: %s\n' % (attr.rjust(22), val)
88
            self.stdout.write(line.encode('utf8'))
89
        self.stdout.write('\n')
b/snf-astakos-app/astakos/im/management/commands/group-permissions-remove.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from optparse import make_option
35

  
36
from django.core.management.base import BaseCommand, CommandError
37
from django.contrib.auth.models import Group
38
from django.core.exceptions import ValidationError
39

  
40
from astakos.im.models import AstakosUser
41
from ._common import remove_group_permission
42

  
43

  
44
class Command(BaseCommand):
45
    args = "<groupname> <permission> [<permissions> ...]"
46
    help = "Remove group permissions"
47

  
48
    def handle(self, *args, **options):
49
        if len(args) < 2:
50
            raise CommandError(
51
                "Please provide a group name and at least one permission")
52

  
53
        group = None
54
        try:
55
            if args[0].isdigit():
56
                group = Group.objects.get(id=args[0])
57
            else:
58
                group = Group.objects.get(name=args[0])
59
        except Group.DoesNotExist, e:
60
            raise CommandError("Invalid group")
61

  
62
        try:
63
            for pname in args[1:]:
64
                r = remove_group_permission(group, pname)
65
                if r < 0:
66
                    self.stdout.write(
67
                        'Invalid permission codename: %s\n' % pname)
68
                elif r == 0:
69
                    self.stdout.write('Group has not permission: %s\n' % pname)
70
                elif r > 0:
71
                    self.stdout.write(
72
                        'Permission: %s removed successfully\n' % pname)
73
        except Exception, e:
74
            raise CommandError(e)
/dev/null
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from optparse import make_option
35

  
36
from django.core.management.base import BaseCommand, CommandError
37
from django.contrib.auth.models import Group
38
from django.core.exceptions import ValidationError
39

  
40
from astakos.im.models import AstakosUser
41
from ._common import remove_group_permission
42

  
43

  
44
class Command(BaseCommand):
45
    args = "<groupname> <permission> [<permissions> ...]"
46
    help = "Remove group permissions"
47

  
48
    def handle(self, *args, **options):
49
        if len(args) < 2:
50
            raise CommandError(
51
                "Please provide a group name and at least one permission")
52

  
53
        group = None
54
        try:
55
            if args[0].isdigit():
56
                group = Group.objects.get(id=args[0])
57
            else:
58
                group = Group.objects.get(name=args[0])
59
        except Group.DoesNotExist, e:
60
            raise CommandError("Invalid group")
61

  
62
        try:
63
            for pname in args[1:]:
64
                r = remove_group_permission(group, pname)
65
                if r < 0:
66
                    self.stdout.write(
67
                        'Invalid permission codename: %s\n' % pname)
68
                elif r == 0:
69
                    self.stdout.write('Group has not permission: %s\n' % pname)
70
                elif r > 0:
71
                    self.stdout.write(
72
                        'Permission: %s removed successfully\n' % pname)
73
        except Exception, e:
74
            raise CommandError(e)
/dev/null
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from optparse import make_option
35

  
36
from django.core.management.base import BaseCommand, CommandError
37
from django.contrib.auth.models import Group
38
from django.core.exceptions import ValidationError
39

  
40
from astakos.im.models import AstakosUser
41
from ._common import remove_group_permission
42

  
43

  
44
class Command(BaseCommand):
45
    args = "<groupname> <permission> [<permissions> ...]"
46
    help = "Remove group permissions"
47

  
48
    def handle(self, *args, **options):
49
        if len(args) < 2:
50
            raise CommandError(
51
                "Please provide a group name and at least one permission")
52

  
53
        group = None
54
        try:
55
            if args[0].isdigit():
56
                group = Group.objects.get(id=args[0])
57
            else:
58
                group = Group.objects.get(name=args[0])
59
        except Group.DoesNotExist, e:
60
            raise CommandError("Invalid group")
61

  
62
        try:
63
            for pname in args[1:]:
64
                r = remove_group_permission(group, pname)
65
                if r < 0:
66
                    self.stdout.write(
67
                        'Invalid permission codename: %s\n' % pname)
68
                elif r == 0:
69
                    self.stdout.write('Group has not permission: %s\n' % pname)
70
                elif r > 0:
71
                    self.stdout.write(
72
                        'Permission: %s removed successfully\n' % pname)
73
        except Exception, e:
74
            raise CommandError(e)
b/snf-astakos-app/astakos/im/management/commands/service-add.py
52 52
        except Exception, e:
53 53
            raise CommandError(e)
54 54
        else:
55
	    if r.is_success:
55
	        if r.is_success:
56 56
                self.stdout.write(
57 57
                    'Service created successfully\n')
58 58
            else:
59
	    	raise CommandError(r.reason)
59
	    	    raise CommandError(r.reason)
b/snf-astakos-app/astakos/im/management/commands/user-add.py
49 49

  
50 50

  
51 51
class Command(BaseCommand):
52
    args = "<email>"
52
    args = "<email> <first name> <last name>"
53 53
    help = "Create a user"
54 54

  
55 55
    option_list = BaseCommand.option_list + (
56
        make_option('--first-name',
57
                    dest='first_name',
58
                    metavar='NAME',
59
                    help="Set user's first name"),
60
        make_option('--last-name',
61
                    dest='last_name',
62
                    metavar='NAME',
63
                    help="Set user's last name"),
64 56
        make_option('--affiliation',
65 57
                    dest='affiliation',
66 58
                    metavar='AFFILIATION',
......
90 82
    )
91 83

  
92 84
    def handle(self, *args, **options):
93
        if len(args) != 1:
85
        if len(args) != 3:
94 86
            raise CommandError("Invalid number of arguments")
95 87

  
96
        email = args[0].decode('utf8')
97

  
88
        email, first_name, last_name = (args[i].decode('utf8') for i in range(3))
89
        
98 90
        try:
99 91
            validate_email(email)
100 92
        except ValidationError:
101 93
            raise CommandError("Invalid email")
102 94

  
103
        u = {'email': email}
95
        u = {'email': email,
96
             'first_name':first_name,
97
             'last_name':last_name
98
        }
104 99
        u.update(filter_custom_options(options))
105 100
        if not u.get('password'):
106 101
            u['password'] = AstakosUser.objects.make_random_password()
107 102

  
108 103
        try:
109 104
            c = AstakosCallpoint()
110
            r = c.create_users((u,))
105
            r = c.create_users((u,)).next()
111 106
        except socket.error, e:
112 107
            raise CommandError(e)
113 108
        except ValidationError, e:
114 109
            raise CommandError(e)
115 110
        else:
116
            failed = (res for res in r if not res.is_success)
117
            for r in failed:
118
                if not r.is_success:
119
                    raise CommandError(r.reason)
120
            if not failed:
121
                self.stdout.write('User created successfully')
122
                if not u.get('password'):
123
                    self.stdout.write('with password: %s' % u['password'])
111
            if not r.is_success:
112
                raise CommandError(r.reason)
113
            else:
114
                self.stdout.write('User created successfully ')
115
                if not options.get('password'):
116
                    self.stdout.write('with password: %s\n' % u['password'])
117
                else:
118
                    self.stdout.write('\n')
b/snf-astakos-app/astakos/im/management/commands/user-remove.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
import socket
35

  
36
from optparse import make_option
37

  
38
from django.core.management.base import BaseCommand, CommandError
39
from django.core.validators import validate_email
40
from django.core.exceptions import ValidationError
41

  
42
from astakos.im.models import AstakosUser
43
from astakos.im.api.callpoint import AstakosCallpoint
44

  
45
def filter_custom_options(options):
46
    base_dests = list(
47
        getattr(o, 'dest', None) for o in BaseCommand.option_list)
48
    return dict((k, v) for k, v in options.iteritems() if k not in base_dests)
49

  
50

  
51
class Command(BaseCommand):
52
    args = "<user ID>"
53
    help = "Remove a user"
54

  
55
    def handle(self, *args, **options):
56
        if len(args) != 1:
57
            raise CommandError("Invalid number of arguments")
58

  
59
        id = args[0]
60
        if not id.isdigit():
61
            raise CommandError('ID must me an integer')
62
        
63
        try:
64
            user = AstakosUser.objects.get(id=int(id))
65
        except:
66
            msg = "Unknown user with id '%s'" % id
67
            raise CommandError(msg)
68
        else:
69
            user.delete()
70
            self.stdout.write('User deleted successfully\n')

Also available in: Unified diff