Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / resource-remove.py @ 398a9604

History | View | Annotate | Download (2.1 kB)

1 77f6e87f Sofia Papagiannaki
# Copyright 2012 GRNET S.A. All rights reserved.
2 77f6e87f Sofia Papagiannaki
#
3 77f6e87f Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 77f6e87f Sofia Papagiannaki
# without modification, are permitted provided that the following
5 77f6e87f Sofia Papagiannaki
# conditions are met:
6 77f6e87f Sofia Papagiannaki
#
7 77f6e87f Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 77f6e87f Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 77f6e87f Sofia Papagiannaki
#      disclaimer.
10 77f6e87f Sofia Papagiannaki
#
11 77f6e87f Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 77f6e87f Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 77f6e87f Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 77f6e87f Sofia Papagiannaki
#      provided with the distribution.
15 77f6e87f Sofia Papagiannaki
#
16 77f6e87f Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 77f6e87f Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 77f6e87f Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 77f6e87f Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 77f6e87f Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 77f6e87f Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 77f6e87f Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 77f6e87f Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 77f6e87f Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 77f6e87f Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 77f6e87f Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 77f6e87f Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 77f6e87f Sofia Papagiannaki
#
29 77f6e87f Sofia Papagiannaki
# The views and conclusions contained in the software and
30 77f6e87f Sofia Papagiannaki
# documentation are those of the authors and should not be
31 77f6e87f Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 77f6e87f Sofia Papagiannaki
# or implied, of GRNET S.A.
33 77f6e87f Sofia Papagiannaki
34 77f6e87f Sofia Papagiannaki
from django.core.management.base import BaseCommand, CommandError
35 77f6e87f Sofia Papagiannaki
36 77f6e87f Sofia Papagiannaki
from astakos.im.models import Resource
37 77f6e87f Sofia Papagiannaki
38 5ce3ce4f Sofia Papagiannaki
39 77f6e87f Sofia Papagiannaki
class Command(BaseCommand):
40 77f6e87f Sofia Papagiannaki
    args = "<resource>"
41 77f6e87f Sofia Papagiannaki
    help = "Add a resource"
42 5ce3ce4f Sofia Papagiannaki
43 77f6e87f Sofia Papagiannaki
    def handle(self, *args, **options):
44 77f6e87f Sofia Papagiannaki
        if len(args) < 1:
45 77f6e87f Sofia Papagiannaki
            raise CommandError("Invalid number of arguments")
46 5ce3ce4f Sofia Papagiannaki
47 77f6e87f Sofia Papagiannaki
        kwargs = {}
48 77f6e87f Sofia Papagiannaki
        if args[0].isdigit():
49 5ce3ce4f Sofia Papagiannaki
            kwargs['id'] = args[0]
50 77f6e87f Sofia Papagiannaki
        else:
51 5ce3ce4f Sofia Papagiannaki
            kwargs['name'] = args[0]
52 5ce3ce4f Sofia Papagiannaki
53 77f6e87f Sofia Papagiannaki
        try:
54 77f6e87f Sofia Papagiannaki
            r = Resource.objects.get(**kwargs)
55 77f6e87f Sofia Papagiannaki
        except Resource.DoesNotExist, e:
56 77f6e87f Sofia Papagiannaki
            raise CommandError("Invalid resource")
57 5ce3ce4f Sofia Papagiannaki
        r.delete()