Revision ccab6eb5 snf-astakos-app/astakos/im/management/commands/project-update.py
b/snf-astakos-app/astakos/im/management/commands/project-update.py | ||
---|---|---|
34 | 34 |
from optparse import make_option |
35 | 35 |
|
36 | 36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from django.db import transaction |
|
37 | 38 |
|
38 |
from astakos.im.models import approve_application,
|
|
39 |
from astakos.im.models import _lookup_object, ProjectApplication, Project
|
|
39 | 40 |
|
41 |
@transaction.commit_on_success |
|
40 | 42 |
class Command(BaseCommand): |
41 |
args = "<project application serial>"
|
|
43 |
args = "<project application id>"
|
|
42 | 44 |
help = "Update project state" |
43 | 45 |
|
44 | 46 |
option_list = BaseCommand.option_list + ( |
47 |
make_option('--approve', |
|
48 |
action='store_true', |
|
49 |
dest='approve', |
|
50 |
default=False, |
|
51 |
help="Approve group"), |
|
45 | 52 |
make_option('--terminate', |
46 | 53 |
action='store_true', |
47 | 54 |
dest='terminate', |
48 | 55 |
default=False, |
49 |
help="Enable group"),
|
|
56 |
help="Terminate group"),
|
|
50 | 57 |
make_option('--suspend', |
51 | 58 |
action='store_true', |
52 | 59 |
dest='suspend', |
53 | 60 |
default=False, |
54 |
help="Disable group"), |
|
55 |
make_option('--activate', |
|
56 |
action='store_true', |
|
57 |
dest='activate', |
|
58 |
default=False, |
|
59 |
help="Disable group"), |
|
61 |
help="Suspend group") |
|
60 | 62 |
) |
61 | 63 |
|
62 | 64 |
def handle(self, *args, **options): |
63 | 65 |
if len(args) < 1: |
64 | 66 |
raise CommandError("Please provide a group identifier") |
65 |
|
|
66 |
group = None |
|
67 |
try: |
|
68 |
if args[0].isdigit(): |
|
69 |
group = AstakosGroup.objects.get(id=args[0]) |
|
70 |
else: |
|
71 |
group = AstakosGroup.objects.get(name=args[0]) |
|
72 |
except AstakosGroup.DoesNotExist, e: |
|
73 |
raise CommandError("Invalid group") |
|
74 |
|
|
67 |
|
|
68 |
app = None |
|
69 |
p = None |
|
75 | 70 |
try: |
76 |
pname = options.get('add-permission') |
|
77 |
if pname: |
|
78 |
r, created = add_group_permission(group, pname) |
|
79 |
if created: |
|
80 |
self.stdout.write( |
|
81 |
'Permission: %s created successfully\n' % pname) |
|
82 |
if r == 0: |
|
83 |
self.stdout.write( |
|
84 |
'Group has already permission: %s\n' % pname) |
|
85 |
else: |
|
86 |
self.stdout.write( |
|
87 |
'Permission: %s added successfully\n' % pname) |
|
88 |
|
|
89 |
pname = options.get('delete-permission') |
|
90 |
if pname: |
|
91 |
r = remove_group_permission(group, pname) |
|
92 |
if r < 0: |
|
93 |
self.stdout.write( |
|
94 |
'Invalid permission codename: %s\n' % pname) |
|
95 |
elif r == 0: |
|
96 |
self.stdout.write('Group has not permission: %s\n' % pname) |
|
97 |
elif r > 0: |
|
98 |
self.stdout.write( |
|
99 |
'Permission: %s removed successfully\n' % pname) |
|
71 |
id = int(args[0]) |
|
72 |
except ValueError: |
|
73 |
raise CommandError('Invalid id') |
|
74 |
else: |
|
75 |
try: |
|
76 |
# Is it a project application id? |
|
77 |
app = _lookup_object(ProjectApplication, id=id) |
|
78 |
except ProjectApplication.DoesNotExist: |
|
79 |
try: |
|
80 |
# Is it a project id? |
|
81 |
p = _lookup_object(Project, id=id) |
|
82 |
except Project.DoesNotExist: |
|
83 |
raise CommandError('Invalid id') |
|
84 |
try: |
|
85 |
if options['approve']: |
|
86 |
if not app: |
|
87 |
raise CommandError('Project application id is required.') |
|
88 |
app.approve() |
|
100 | 89 |
|
101 |
if options.get('enable'): |
|
102 |
group.enable() |
|
103 |
elif options.get('disable'): |
|
104 |
group.disable() |
|
90 |
if app and app.status != 'Pending': |
|
91 |
p = app.project |
|
105 | 92 |
|
106 |
except Exception, e: |
|
107 |
raise CommandError(e) |
|
93 |
if options['terminate']: |
|
94 |
p.terminate() |
|
95 |
if options['suspend']: |
|
96 |
p.suspend() |
|
97 |
except BaseException, e: |
|
98 |
import traceback |
|
99 |
traceback.print_exc() |
|
100 |
raise CommandError(e) |
Also available in: Unified diff