root / snf-astakos-app / astakos / im / management / commands / component-add.py @ def409e0
History | View | Annotate | Download (2.8 kB)
1 | 252eef38 | Giorgos Korfiatis | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 252eef38 | Giorgos Korfiatis | #
|
3 | 252eef38 | Giorgos Korfiatis | # Redistribution and use in source and binary forms, with or
|
4 | 252eef38 | Giorgos Korfiatis | # without modification, are permitted provided that the following
|
5 | 252eef38 | Giorgos Korfiatis | # conditions are met:
|
6 | 252eef38 | Giorgos Korfiatis | #
|
7 | 252eef38 | Giorgos Korfiatis | # 1. Redistributions of source code must retain the above
|
8 | 252eef38 | Giorgos Korfiatis | # copyright notice, this list of conditions and the following
|
9 | 252eef38 | Giorgos Korfiatis | # disclaimer.
|
10 | 252eef38 | Giorgos Korfiatis | #
|
11 | 252eef38 | Giorgos Korfiatis | # 2. Redistributions in binary form must reproduce the above
|
12 | 252eef38 | Giorgos Korfiatis | # copyright notice, this list of conditions and the following
|
13 | 252eef38 | Giorgos Korfiatis | # disclaimer in the documentation and/or other materials
|
14 | 252eef38 | Giorgos Korfiatis | # provided with the distribution.
|
15 | 252eef38 | Giorgos Korfiatis | #
|
16 | 252eef38 | Giorgos Korfiatis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 252eef38 | Giorgos Korfiatis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 252eef38 | Giorgos Korfiatis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 252eef38 | Giorgos Korfiatis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 252eef38 | Giorgos Korfiatis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 252eef38 | Giorgos Korfiatis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 252eef38 | Giorgos Korfiatis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 252eef38 | Giorgos Korfiatis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 252eef38 | Giorgos Korfiatis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 252eef38 | Giorgos Korfiatis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 252eef38 | Giorgos Korfiatis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 252eef38 | Giorgos Korfiatis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 252eef38 | Giorgos Korfiatis | #
|
29 | 252eef38 | Giorgos Korfiatis | # The views and conclusions contained in the software and
|
30 | 252eef38 | Giorgos Korfiatis | # documentation are those of the authors and should not be
|
31 | 252eef38 | Giorgos Korfiatis | # interpreted as representing official policies, either expressed
|
32 | 252eef38 | Giorgos Korfiatis | # or implied, of GRNET S.A.
|
33 | 252eef38 | Giorgos Korfiatis | |
34 | eb765213 | Giorgos Korfiatis | from optparse import make_option |
35 | 252eef38 | Giorgos Korfiatis | from django.core.management.base import BaseCommand, CommandError |
36 | 252eef38 | Giorgos Korfiatis | from astakos.im.models import Component |
37 | 252eef38 | Giorgos Korfiatis | |
38 | 252eef38 | Giorgos Korfiatis | |
39 | 252eef38 | Giorgos Korfiatis | class Command(BaseCommand): |
40 | eb765213 | Giorgos Korfiatis | args = "<name>"
|
41 | 252eef38 | Giorgos Korfiatis | help = "Register a component"
|
42 | 252eef38 | Giorgos Korfiatis | |
43 | eb765213 | Giorgos Korfiatis | option_list = BaseCommand.option_list + ( |
44 | eb765213 | Giorgos Korfiatis | make_option('--ui-url',
|
45 | eb765213 | Giorgos Korfiatis | dest='ui_url',
|
46 | eb765213 | Giorgos Korfiatis | default=None,
|
47 | eb765213 | Giorgos Korfiatis | help="Set UI URL"),
|
48 | eb765213 | Giorgos Korfiatis | make_option('--base-url',
|
49 | eb765213 | Giorgos Korfiatis | dest='base_url',
|
50 | eb765213 | Giorgos Korfiatis | default=None,
|
51 | eb765213 | Giorgos Korfiatis | help="Set base URL"),
|
52 | eb765213 | Giorgos Korfiatis | ) |
53 | eb765213 | Giorgos Korfiatis | |
54 | 252eef38 | Giorgos Korfiatis | def handle(self, *args, **options): |
55 | eb765213 | Giorgos Korfiatis | if len(args) != 1: |
56 | 252eef38 | Giorgos Korfiatis | raise CommandError("Invalid number of arguments") |
57 | 252eef38 | Giorgos Korfiatis | |
58 | 252eef38 | Giorgos Korfiatis | name = args[0]
|
59 | eb765213 | Giorgos Korfiatis | base_url = options['base_url']
|
60 | eb765213 | Giorgos Korfiatis | ui_url = options['ui_url']
|
61 | eb765213 | Giorgos Korfiatis | |
62 | 252eef38 | Giorgos Korfiatis | try:
|
63 | eb765213 | Giorgos Korfiatis | Component.objects.get(name=name) |
64 | 252eef38 | Giorgos Korfiatis | m = "There already exists a component named '%s'." % name
|
65 | 252eef38 | Giorgos Korfiatis | raise CommandError(m)
|
66 | 252eef38 | Giorgos Korfiatis | except Component.DoesNotExist:
|
67 | 252eef38 | Giorgos Korfiatis | pass
|
68 | 252eef38 | Giorgos Korfiatis | |
69 | 252eef38 | Giorgos Korfiatis | try:
|
70 | eb765213 | Giorgos Korfiatis | c = Component.objects.create( |
71 | eb765213 | Giorgos Korfiatis | name=name, url=ui_url, base_url=base_url) |
72 | 252eef38 | Giorgos Korfiatis | except BaseException: |
73 | 252eef38 | Giorgos Korfiatis | raise CommandError("Failed to register component.") |
74 | 252eef38 | Giorgos Korfiatis | else:
|
75 | 252eef38 | Giorgos Korfiatis | self.stdout.write('Token: %s\n' % c.auth_token) |