This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
systems_info_view.py
114 lines (89 loc) · 3.92 KB
/
systems_info_view.py
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
103
104
105
106
107
108
109
110
111
112
113
114
from flask import request, session
from flask_restx import Resource
import cortex.lib.systems
from cortex.api import api_login_required, api_manager
from cortex.api.exceptions import (
InvalidPermissionException, NoResultsFoundException)
from cortex.api.parsers import (
pagination_arguments, process_pagination_arguments, pagination_response)
from cortex.api.serializers.systems_info_view import (
page_systems_info_view_serializer, systems_info_view_serializer)
from cortex.lib.user import (
does_user_have_permission, does_user_have_system_permission)
systems_info_view_namespace = api_manager.namespace('systems_info_view', description='System\'s Info View API')
# pylint: disable=no-self-use
@systems_info_view_namespace.route('/')
class SystemsInfoViewCollection(Resource):
"""
API Handler for multiple rows from the systems_info_view.
"""
@api_login_required('get')
@api_manager.expect(pagination_arguments)
@api_manager.marshal_with(page_systems_info_view_serializer, mask='{page,pages,per_page,total,items{id,name}}')
def get(self):
"""
Returns a paginated list of rows from the systems_info_view.
"""
page, per_page, limit_start, limit_length = process_pagination_arguments(request)
if not (does_user_have_permission("systems.all.view") or does_user_have_permission("systems.own.view")):
raise InvalidPermissionException
class_name = None
hide_inactive = False
only_other = False
show_expired = False
show_nocmdb = False
show_allocated_and_perms = False
if does_user_have_permission("systems.all.view"):
only_allocated_by = None
else:
# Show the systems where the user has permissions AND the ones they allocated.
show_allocated_and_perms = True
if request.authorization:
only_allocated_by = request.authorization.username
else:
if 'username' in session:
only_allocated_by = session['username']
else:
raise InvalidPermissionException
total = cortex.lib.systems.get_system_count(class_name=class_name, hide_inactive=hide_inactive, only_other=only_other, show_expired=show_expired, show_nocmdb=show_nocmdb, show_allocated_and_perms=show_allocated_and_perms, only_allocated_by=only_allocated_by)
results = cortex.lib.systems.get_systems(class_name=class_name, order='id', limit_start=limit_start, limit_length=limit_length, hide_inactive=hide_inactive, only_other=only_other, show_expired=show_expired, show_nocmdb=show_nocmdb, show_allocated_and_perms=show_allocated_and_perms, only_allocated_by=only_allocated_by)
return pagination_response(results, page, per_page, total)
@systems_info_view_namespace.route('/<int:system_id>')
@api_manager.response(404, 'System not found.')
@api_manager.doc(params={'system_id': 'System ID.'})
class SystemsInfoViewItem(Resource):
"""
API Handler for a system in the systems_info_view.
"""
@api_login_required('get')
@api_manager.marshal_with(systems_info_view_serializer)
def get(self, system_id):
"""
Returns a single system from systems_info_view.
"""
if not does_user_have_system_permission(system_id, "view.detail", "systems.all.view"):
raise InvalidPermissionException
system = cortex.lib.systems.get_system_by_id(system_id)
if not system:
raise NoResultsFoundException
return system
@systems_info_view_namespace.route('/<string:system_name>')
@api_manager.response(404, 'System not found.')
@api_manager.doc(params={'system_name': 'System Name.'})
class SystemsInfoViewItemByName(Resource):
"""
API Handler for a system in the systems_info_view.
(Searching by System Name).
"""
@api_login_required('get')
@api_manager.marshal_with(systems_info_view_serializer)
def get(self, system_name):
"""
Returns a single system from systems_info_view, searching by name.
"""
system = cortex.lib.systems.get_system_by_name(system_name)
if not system:
raise NoResultsFoundException
if not does_user_have_system_permission(system['id'], "view.detail", "systems.all.view"):
raise InvalidPermissionException
return system