from os.path import abspath
from django.core.management.base import BaseCommand, CommandError
-
from django.contrib.auth.models import Group
+from ._common import add_group_permission
+
class Command(BaseCommand):
- args = "<name>"
+ args = "<groupname> [<permission> ...]"
help = "Insert group"
def handle(self, *args, **options):
- if len(args) != 1:
+ if len(args) < 1:
raise CommandError("Invalid number of arguments")
name = args[0].decode('utf8')
except Group.DoesNotExist, e:
group = Group(name=name)
group.save()
-
- msg = "Created group id %d" % (group.id,)
- self.stdout.write(msg + '\n')
+ msg = "Created group id %d" % (group.id,)
+ self.stdout.write(msg + '\n')
+ try:
+ for pname in args[1:]:
+ r, created = add_group_permission(group, pname)
+ if created:
+ self.stdout.write('Permission: %s created successfully\n' % pname)
+ if r == 0:
+ self.stdout.write('Group has already permission: %s\n' % pname)
+ else:
+ self.stdout.write('Permission: %s added successfully\n' % pname)
+ except Exception, e:
+ raise CommandError(e)
\ No newline at end of file