def prepare(self): """This method is executed at the beginning of each request. """ self.set_header("Cache-Control", "no-cache, must-revalidate") self.sql_session = Session() self.r_params = self.render_params() self.r_params["warning"] = "" self.r_params["success"] = ""
class BaseHandler(RequestHandler): def prepare(self): """This method is executed at the beginning of each request. """ self.set_header("Cache-Control", "no-cache, must-revalidate") self.sql_session = Session() self.r_params = self.render_params() self.r_params["warning"] = "" self.r_params["success"] = "" def render_params(self): """Return the default render params used by almost all handlers. return (dict): default render params """ global restricted_access_only ret = {} ip_address = self.request.remote_ip arp_entry = lookup_arp_entry_from_ip(ip_address, self.sql_session) machine = None lm = None if arp_entry: machine = self.sql_session.query(Machine).filter(Machine.mac_address==arp_entry.mac_address).first() if machine: lm = self.sql_session.query(LocationMapping).filter(LocationMapping.machine==machine).first() # If a machine has no location mapping, then we assume it's not a contestant machine. self.allow_restricted_access = True if lm is None else False if not restricted_access_only: self.allow_restricted_access = True ret["ip_address"] = ip_address ret["arp_entry"] = arp_entry ret["machine"] = machine ret["lm"] = lm ret["location"] = str(lm.location) if lm is not None else '' ret["asset_number"] = machine.asset_number if machine is not None else '' ret["notes"] = machine.notes if machine is not None else '' location_for_ip = get_location_for_ip(ip_address, self.sql_session) ret["location_for_ip"] = str(location_for_ip) if location_for_ip is not None else '' ret["network_unknown"] = location_for_ip is None ret["show_sidebar"] = True ret["restricted_access_only"] = str(restricted_access_only) ret["restricted_access_only_checkbox"] = 'checked' if restricted_access_only else '' return ret def finish(self, *args, **kwds): """ Finishes this response, ending the HTTP request. We override this method in order to properly close the database. """ if hasattr(self, "sql_session"): try: self.sql_session.close() except Exception as error: logger.warning("Couldn't close SQL connection: %r" % error) try: tornado.web.RequestHandler.finish(self, *args, **kwds) except IOError: # When the client closes the connection before we reply, # Tornado raises an IOError exception, that would pollute # our log with unnecessarily critical messages logger.debug("Connection closed before our reply.") def write_error(self, status_code, **kwargs): if "exc_info" in kwargs and \ kwargs["exc_info"][0] != tornado.web.HTTPError: exc_info = kwargs["exc_info"] logger.error( "Uncaught exception (%r) while processing a request: %s" % (exc_info[1], ''.join(traceback.format_exception(*exc_info)))) # We assume that if r_params is defined then we have at least # the data we need to display a basic template with the error # information. If r_params is not defined (i.e. something went # *really* bad) we simply return a basic textual error notice. if hasattr(self, 'r_params'): self.render("error.html", status_code=status_code, **self.r_params) else: self.write("A critical error has occurred :-(") self.finish() def try_commit(self): try: self.sql_session.commit() except IntegrityError as error: self.r_params["warning"] = "Operation failed: %s" % str(error) self.sql_session.rollback() return False else: self.r_params["success"] = "Data succesfully updated." return True