Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / group_add.py @ 304acb60

History | View | Annotate | Download (3 kB)

1 18ffbee1 Sofia Papagiannaki
# Copyright 2012 GRNET S.A. All rights reserved.
2 18ffbee1 Sofia Papagiannaki
#
3 18ffbee1 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 18ffbee1 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 18ffbee1 Sofia Papagiannaki
# conditions are met:
6 18ffbee1 Sofia Papagiannaki
#
7 18ffbee1 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 18ffbee1 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 18ffbee1 Sofia Papagiannaki
#      disclaimer.
10 18ffbee1 Sofia Papagiannaki
#
11 18ffbee1 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 18ffbee1 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 18ffbee1 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 18ffbee1 Sofia Papagiannaki
#      provided with the distribution.
15 18ffbee1 Sofia Papagiannaki
#
16 18ffbee1 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 18ffbee1 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18ffbee1 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 18ffbee1 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 18ffbee1 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 18ffbee1 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 18ffbee1 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 18ffbee1 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 18ffbee1 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 18ffbee1 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 18ffbee1 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 18ffbee1 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 18ffbee1 Sofia Papagiannaki
#
29 18ffbee1 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 18ffbee1 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 18ffbee1 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 18ffbee1 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 18ffbee1 Sofia Papagiannaki
34 18ffbee1 Sofia Papagiannaki
from optparse import make_option
35 18ffbee1 Sofia Papagiannaki
from random import choice
36 18ffbee1 Sofia Papagiannaki
from string import digits, lowercase, uppercase
37 18ffbee1 Sofia Papagiannaki
from uuid import uuid4
38 18ffbee1 Sofia Papagiannaki
from time import time
39 18ffbee1 Sofia Papagiannaki
from os.path import abspath
40 18ffbee1 Sofia Papagiannaki
41 18ffbee1 Sofia Papagiannaki
from django.core.management.base import BaseCommand, CommandError
42 ffb1e7a8 Sofia Papagiannaki
43 ffb1e7a8 Sofia Papagiannaki
from astakos.im.models import AstakosGroup
44 18ffbee1 Sofia Papagiannaki
45 30dc8c1a Sofia Papagiannaki
from ._common import add_group_permission
46 30dc8c1a Sofia Papagiannaki
47 18ffbee1 Sofia Papagiannaki
class Command(BaseCommand):
48 30dc8c1a Sofia Papagiannaki
    args = "<groupname> [<permission> ...]"
49 18ffbee1 Sofia Papagiannaki
    help = "Insert group"
50 18ffbee1 Sofia Papagiannaki
    
51 18ffbee1 Sofia Papagiannaki
    def handle(self, *args, **options):
52 30dc8c1a Sofia Papagiannaki
        if len(args) < 1:
53 18ffbee1 Sofia Papagiannaki
            raise CommandError("Invalid number of arguments")
54 18ffbee1 Sofia Papagiannaki
        
55 18ffbee1 Sofia Papagiannaki
        name = args[0].decode('utf8')
56 18ffbee1 Sofia Papagiannaki
        
57 18ffbee1 Sofia Papagiannaki
        try:
58 ffb1e7a8 Sofia Papagiannaki
            AstakosGroup.objects.get(name=name)
59 18ffbee1 Sofia Papagiannaki
            raise CommandError("A group with this name already exists")
60 ffb1e7a8 Sofia Papagiannaki
        except AstakosGroup.DoesNotExist, e:
61 ffb1e7a8 Sofia Papagiannaki
            group = AstakosGroup(name=name)
62 18ffbee1 Sofia Papagiannaki
            group.save()
63 30dc8c1a Sofia Papagiannaki
            msg = "Created group id %d" % (group.id,)
64 30dc8c1a Sofia Papagiannaki
            self.stdout.write(msg + '\n')
65 30dc8c1a Sofia Papagiannaki
            try:
66 30dc8c1a Sofia Papagiannaki
                for pname in args[1:]:
67 30dc8c1a Sofia Papagiannaki
                    r, created = add_group_permission(group, pname)
68 30dc8c1a Sofia Papagiannaki
                    if created:
69 30dc8c1a Sofia Papagiannaki
                        self.stdout.write('Permission: %s created successfully\n' % pname)
70 30dc8c1a Sofia Papagiannaki
                    if r == 0:
71 30dc8c1a Sofia Papagiannaki
                        self.stdout.write('Group has already permission: %s\n' % pname)
72 30dc8c1a Sofia Papagiannaki
                    else:
73 30dc8c1a Sofia Papagiannaki
                        self.stdout.write('Permission: %s added successfully\n' % pname)
74 30dc8c1a Sofia Papagiannaki
            except Exception, e:
75 30dc8c1a Sofia Papagiannaki
                raise CommandError(e)