1 '''
2 Inventory Hardware Module
3 '''
4
5 from ..pluginbase import PluginBase
6 from model import HardwareElement, HardwareProfile
7 import web
8 import searchbyhardware
9 import createhardwareprofile
10
11
12
14 '''
15 '''
21
22
23
24
25
28
31
34
37
40
42 return [self.actions["createhardwareprofile"]]
43
45
46 if computer.hardware:
47 return [self.actions["updatehardwareprofile"],
48 self.actions["deletehardwareprofile"]]
49 else:
50 return [self.actions["createhardwareprofile"]]
51
53 '''
54 Parses a text and adds hardware elements to the
55 db. Returns the hardware elements parsed.
56 If a hardware element is already in db, it won't be added again.
57 A hardware element is only added once.
58
59 If an error ocurred will be ignored.
60
61 Hardware_text is the text returned by a client that executed
62 ogHardwareList.
63 '''
64 orm = web.ctx.orm
65 elements = []
66
67 for line in hardware_text.split('\n'):
68 parts = line.split('=')
69 if (len(parts) != 2):
70 print '%s: Hardware element incorrect: %s' % self.human_name, line
71 continue
72
73 if (len(parts[0]) != 3):
74 print '%s: Type with size incorrect size (must be 3): %s' % self.human_name, line
75 continue
76
77 if (len(parts[1]) > 512):
78 print '%s: Too long description: %s' % self.human_name, line
79 continue
80
81 hardware_element = self._search_specific_hardware_element(parts)
82
83 if hardware_element:
84
85 hardware_element = HardwareElement()
86 hardware_element.type = parts[0]
87 hardware_element.description = parts[1]
88 orm.add(hardware_element)
89
90 elements.append(hardware_element)
91
92 orm.commit()
93
94 return elements
95
97 '''
98 Adds a profile hardware with the given computer,
99 hardware_elemengs and name. If a same hardware profile exists
100 then the function only relates computer with that profile, and
101 its name will NOT be changed.
102
103 Returns the profile.
104 '''
105
106 hardware_profile = self._search_specific_hardware_profile(hardware_elements)
107
108 if hardware_profile:
109
110 hardware_profile = HardwareProfile()
111 hardware_profile.name = name
112 hardware_profile.computer = []
113 hardware_profile.elements = hardware_elements
114 web.ctx.orm.add(hardware_profile)
115
116 hardware_profile.computers.append(computer)
117
118 return hardware_profile
120 '''
121 Searchs a element with the given type and description.
122 It is returned if it found. None is returned in other case.
123 '''
124 orm = web.ctx.orm
125 elements = orm.query(HardwareElement).all()
126
127 for element in elements:
128 if (element.type == hardware_type and
129 element.description == description):
130 return element
131
132 return None
133
135 '''
136 Searchs a profile with the given elements.
137 It is returned if it found. None is returned in other case.
138 '''
139 orm = web.ctx.orm
140 profiles = orm.query(HardwareProfile).all()
141
142 for profile in profiles:
143 if set(profile.elements) == set(elements):
144 return profile
145
146 return None
147
148
151