summaryrefslogtreecommitdiffstats
path: root/moonclient/moonclient/subject_categories.py
blob: 810b0b5f4c6a453ff38cfe912d85b71a456023d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import logging

from cliff.lister import Lister
from cliff.command import Command


class SubjectCategoriesList(Lister):
    """List all subject categories."""

    log = logging.getLogger(__name__)

    def get_parser(self, prog_name):
        parser = super(SubjectCategoriesList, self).get_parser(prog_name)
        parser.add_argument(
            '--intraextension',
            metavar='<intraextension-uuid>',
            help='IntraExtension UUID',
        )
        return parser

    def take_action(self, parsed_args):
        if not parsed_args.intraextension:
            parsed_args.intraextension = self.app.intraextension
        data = self.app.get_url(self.app.url_prefix+"/intra_extensions/{}/subject_categories".format(parsed_args.intraextension),
                                authtoken=True)
        return (
            ("id", "name", "description"),
            ((_uuid, data[_uuid]["name"], data[_uuid]["description"]) for _uuid in data)
        )


class SubjectCategoriesAdd(Command):
    """Add a new subject category."""

    log = logging.getLogger(__name__)

    def get_parser(self, prog_name):
        parser = super(SubjectCategoriesAdd, self).get_parser(prog_name)
        parser.add_argument(
            'subject_category_name',
            metavar='<subject_category-name>',
            help='Subject category name',
        )
        parser.add_argument(
            '--intraextension',
            metavar='<intraextension-uuid>',
            help='IntraExtension UUID',
        )
        parser.add_argument(
            '--description',
            metavar='<description-str>',
            help='Subject category description',
        )
        return parser

    def take_action(self, parsed_args):
        if not parsed_args.intraextension:
            parsed_args.intraextension = self.app.intraextension
        data = self.app.get_url(self.app.url_prefix+"/intra_extensions/{}/subject_categories".format(parsed_args.intraextension),
                                post_data={
                                    "subject_category_name": parsed_args.subject_category_name,
                                    "subject_category_description": parsed_args.description},
                                authtoken=True)
        return (
            ("id", "name", "description"),
            ((_uuid, data[_uuid]["name"], data[_uuid]["description"]) for _uuid in data)
        )


class SubjectCategoriesDelete(Command):
    """Delete a subject category."""

    log = logging.getLogger(__name__)

    def get_parser(self, prog_name):
        parser = super(SubjectCategoriesDelete, self).get_parser(prog_name)
        parser.add_argument(
            'subject_category_id',
            metavar='<subject_category-uuid>',
            help='Subject category UUID',
        )
        parser.add_argument(
            '--intraextension',
            metavar='<intraextension-uuid>',
            help='IntraExtension UUID',
        )
        return parser

    def take_action(self, parsed_args):
        if not parsed_args.intraextension:
            parsed_args.intraextension = self.app.intraextension
        self.app.get_url(self.app.url_prefix+"/intra_extensions/{}/subject_categories/{}".format(
            parsed_args.intraextension,
            parsed_args.subject_category_id),
            method="DELETE",
            authtoken=True
        )