]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/fireinfo.py
9 from .. import fireinfo
12 from . import ui_modules
14 class BaseHandler(base
.BaseHandler
):
17 return self
.get_argument_date("when", None)
20 MIN_PROFILE_VERSION
= 0
21 MAX_PROFILE_VERSION
= 0
24 def __getattr__(self
, key
):
28 raise AttributeError(key
)
30 def __setattr__(self
, key
, val
):
34 class ProfileSendHandler(BaseHandler
):
35 def check_xsrf_cookie(self
):
36 # This cookie is not required here.
40 # Create an empty profile.
41 self
.profile
= Profile()
43 def __check_attributes(self
, profile
):
45 Check for attributes that must be provided,
52 for attr
in attributes
:
53 if attr
not in profile
:
54 raise tornado
.web
.HTTPError(400, "Profile lacks '%s' attribute: %s" % (attr
, profile
))
56 def __check_valid_ids(self
, profile
):
58 Check if IDs contain valid data.
60 for id in ("public_id", "private_id"):
61 if re
.match(r
"^([a-f0-9]{40})$", "%s" % profile
[id]) is None:
62 raise tornado
.web
.HTTPError(400, "ID '%s' has wrong format: %s" % (id, profile
))
64 def __check_equal_ids(self
, profile
):
66 Check if public_id and private_id are equal.
68 if profile
.public_id
== profile
.private_id
:
69 raise tornado
.web
.HTTPError(400, "Public and private IDs are equal: %s" % profile
)
71 def __check_matching_ids(self
, profile
):
73 Check if a profile with the given public_id is already in the
74 database. If so we need to check if the private_id matches.
76 p
= self
.profiles
.find_one({ "public_id" : profile
["public_id"]})
81 if p
.private_id
!= profile
.private_id
:
82 raise tornado
.web
.HTTPError(400, "Mismatch of private_id: %s" % profile
)
84 def __check_profile_version(self
, profile
):
86 Check if this version of the server software does support the
89 version
= profile
.profile_version
91 if version
< MIN_PROFILE_VERSION
or version
> MAX_PROFILE_VERSION
:
92 raise tornado
.web
.HTTPError(400,
93 "Profile version is not supported: %s" % version
)
95 def check_profile_blob(self
, profile
):
97 This method checks if the blob is sane.
100 self
.__check
_attributes
,
101 self
.__check
_valid
_ids
,
102 self
.__check
_equal
_ids
,
103 self
.__check
_profile
_version
,
104 # These checks require at least one database query and should be done
106 self
.__check
_matching
_ids
,
112 # If we got here, everything is okay and we can go on...
114 def get_profile_blob(self
):
115 profile
= self
.get_argument("profile", None)
117 # Send "400 bad request" if no profile was provided
119 raise tornado
.web
.HTTPError(400, "No profile received")
121 # Try to decode the profile.
123 return json
.loads(profile
)
124 except json
.decoder
.JSONDecodeError
as e
:
125 raise tornado
.web
.HTTPError(400, "Profile could not be decoded: %s" % e
)
127 # The GET method is only allowed in debugging mode.
128 def get(self
, public_id
):
129 if not self
.application
.settings
["debug"]:
130 raise tornado
.web
.HTTPError(405)
132 return self
.post(public_id
)
134 def post(self
, public_id
):
135 profile_blob
= self
.get_profile_blob()
136 #self.check_profile_blob(profile_blob)
139 location
= self
.get_remote_location()
141 location
= location
.country
143 # Handle the profile.
144 with self
.db
.transaction():
146 self
.fireinfo
.handle_profile(public_id
, profile_blob
, location
=location
)
148 except fireinfo
.ProfileParserError
as e
:
149 raise tornado
.web
.HTTPError(400, "Could not parse profile: %s" % e
)
151 self
.finish("Your profile was successfully saved to the database.")
154 class IndexHandler(BaseHandler
):
158 "latest_release" : self
.backend
.releases
.get_latest(),
161 "arches" : self
.fireinfo
.get_arch_map(when
=self
.when
),
162 "cpu_vendors" : self
.fireinfo
.get_cpu_vendors_map(when
=self
.when
),
163 "memory_avg" : self
.backend
.fireinfo
.get_average_memory_amount(when
=self
.when
),
166 "hypervisors" : self
.fireinfo
.get_hypervisor_map(when
=self
.when
),
167 "virtual_ratio" : self
.fireinfo
.get_virtual_ratio(when
=self
.when
),
170 "locations" : self
.fireinfo
.get_geo_location_map(when
=self
.when
),
174 self
.set_expires(3600)
176 self
.render("fireinfo/index.html", **data
)
179 class DriverDetail(BaseHandler
):
180 def get(self
, driver
):
182 self
.set_expires(3600)
184 self
.render("fireinfo/driver.html", driver
=driver
,
185 driver_map
=self
.fireinfo
.get_driver_map(driver
, when
=self
.when
))
188 class ProfileHandler(BaseHandler
):
189 def get(self
, profile_id
):
190 profile
= self
.fireinfo
.get_profile(profile_id
, when
=self
.when
)
192 if not profile
or not profile
.is_showable():
193 raise tornado
.web
.HTTPError(404)
196 self
.set_expires(3600)
198 self
.render("fireinfo/profile.html", profile
=profile
)
201 class RandomProfileHandler(BaseHandler
):
203 profile_id
= self
.fireinfo
.get_random_profile(when
=self
.when
)
204 if profile_id
is None:
205 raise tornado
.web
.HTTPError(404)
207 self
.redirect("/profile/%s" % profile_id
)
210 class ReleasesHandler(BaseHandler
):
213 "releases" : self
.fireinfo
.get_releases_map(when
=self
.when
),
214 "kernels" : self
.fireinfo
.get_kernels_map(when
=self
.when
),
218 self
.set_expires(3600)
220 return self
.render("fireinfo/releases.html", **data
)
223 class ProcessorsHandler(BaseHandler
):
227 for platform
in ("arm", "x86"):
229 self
.fireinfo
.get_common_cpu_flags_by_platform(platform
, when
=self
.when
)
232 self
.set_expires(3600)
234 return self
.render("fireinfo/processors.html", flags
=flags
)
237 class VendorsHandler(BaseHandler
):
239 vendors
= self
.fireinfo
.get_vendor_list(when
=self
.when
)
242 self
.set_expires(3600)
244 self
.render("fireinfo/vendors.html", vendors
=vendors
)
247 class VendorHandler(BaseHandler
):
248 def get(self
, subsystem
, vendor_id
):
249 devices
= self
.fireinfo
.get_devices_by_vendor(subsystem
, vendor_id
, when
=self
.when
)
251 raise tornado
.web
.HTTPError(404)
253 vendor_name
= self
.fireinfo
.get_vendor_string(subsystem
, vendor_id
)
256 self
.set_expires(3600)
258 self
.render("fireinfo/vendor.html", vendor_name
=vendor_name
, devices
=devices
)
261 class DeviceTableModule(ui_modules
.UIModule
):
262 def render(self
, devices
, show_group
=True, embedded
=False):
263 return self
.render_string("fireinfo/modules/table-devices.html",
264 devices
=devices
, show_group
=show_group
, embedded
=embedded
)
267 class DeviceAndGroupsTableModule(ui_modules
.UIModule
):
268 def render(self
, devices
):
269 _
= self
.locale
.translate
273 for device
in devices
:
274 if device
.cls
not in groups
:
275 groups
[device
.cls
] = []
277 groups
[device
.cls
].append(device
)
280 for key
in list(groups
.keys()):
283 # Order the groups by their name
284 groups
= list(groups
.items())
287 return self
.render_string("fireinfo/modules/table-devices-and-groups.html",
291 class AdminIndexHandler(BaseHandler
):
292 @tornado.web
.authenticated
294 if not self
.current_user
.is_staff():
295 raise tornado
.web
.HTTPError(401)
297 @tornado.web
.authenticated
299 count
= self
.backend
.fireinfo
.get_profile_count()
301 self
.render("fireinfo/admin.html", count
=count
)