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