def Render(self, template_path, values, write_to_response=True): """Renders a template using supplied data values and returns HTML. Args: template_path: str path of template. values: dict of template values. write_to_response: bool, True to write to response.out.write(). Returns: str HTML of rendered template. """ path = os.path.join(os.path.dirname(__file__), 'templates', template_path) if not settings.DEV_APPSERVER: values['static_path'] = 'myapp/%s' % os.getenv( 'CURRENT_VERSION_ID') values['is_admin'] = self.IsAdminUser() if not hasattr(self, '_menu'): self._menu = GetMenu() values['menu'] = self._menu if not settings.APPROVAL_REQUIRED: if 'proposals' in values['menu']['munki_packages']['subitems']: del values['menu']['munki_packages']['subitems']['proposals'] if 'msg' not in values: values['msg'] = self.request.GET.get('msg') if 'report_type' not in values: values['report_type'] = 'undefined_report' if self.XSRF_PROTECT: values['xsrf_token'] = xsrf.XsrfTokenGenerate( values['report_type']) if hasattr(self, '_page'): values['limit'] = self._page.get('limit') values['next_page'] = self._page.get('next_page') values['results_count'] = self._page.get('results_count') values['limits'] = QUERY_LIMITS values['request_query_params'] = self.request.GET values['request_path'] = self.request.path if self._page.get('next_page'): # Generate next page link, replacing "page" query param with next_page. query_params = self.request.GET.copy() query_params['page'] = self._page.get('next_page') values['next_page_link'] = '%s?%s' % ( self.request.path, urllib.urlencode(query_params, doseq=True)) html = template.render(path, values) if write_to_response: self.response.out.write(html) return html
def get(self): self.response.write(xsrf.XsrfTokenGenerate('mock'))
def _DisplayBrokenClients(self): """Displays a report of broken clients.""" # client with broken python py_computers = models.ComputerClientBroken.all().filter( 'fixed =', False) py_computers = list(py_computers) for computer in py_computers: computer.details = computer.details.replace("'", "\\'") computer.details = computer.details.replace('"', "\\'") computer.details = re.sub('\n', '<br/>', computer.details) computer.broken_datetimes.reverse() computer.likely_fixed = False # if a UUID is set, attempt to figure out when it last connected. if computer.uuid: try: c_obj = models.Computer.get_by_key_name(computer.uuid) if c_obj.preflight_datetime > computer.broken_datetimes[0]: computer.likely_fixed = True except (IndexError, TypeError, models.db.Error): pass # clients with zero connection q = models.Computer.AllActive().filter( 'connections_on_corp =', 0).filter('connections_off_corp =', 0).fetch(admin.DEFAULT_COMPUTER_FETCH_LIMIT) zero_conn_computers = [] for c in q: if c.preflight_count_since_postflight > PREFLIGHT_COUNT_BROKEN_THRESHOLD: zero_conn_computers.append(c) zero_conn_computers.sort(key=lambda x: x.preflight_datetime, reverse=True) # clients with no recent postflight, but recent preflight fetch_limit = 1000 pf_computers = [] q = models.Computer.AllActive().filter( 'preflight_count_since_postflight >', PREFLIGHT_COUNT_BROKEN_THRESHOLD) i = 0 for c in q: i += 1 if i >= fetch_limit: # avoid DeadlineExceededError. break if not c.preflight_datetime or not c.postflight_datetime: continue # already covered zero connection clients above. pf_computers.append(c) pf_computers.sort(key=lambda x: x.preflight_datetime, reverse=True) self.Render( 'broken_clients.html', { 'py_computers': py_computers, 'zero_conn_computers': zero_conn_computers, 'pf_computers': pf_computers, 'is_security_user': auth.IsSecurityUser(), 'report_type': 'broken_clients', 'truncated': i >= fetch_limit, 'preflight_count_broken_threshold': PREFLIGHT_COUNT_BROKEN_THRESHOLD, 'host_xsrf_token': xsrf.XsrfTokenGenerate('host'), })
def _DisplayHost(self, computer, self_report): """Displays the report for a single host. Args: computer: models.Computer object to display. self_report: if True, display as self report. """ uuid = computer.uuid popup = self.request.get('format', None) == 'popup' if popup: limit = 1 else: limit = SINGLE_HOST_DATA_FETCH_LIMIT client_log_files = models.ClientLogFile.all().filter( 'uuid =', uuid).order('-mtime').fetch(limit) msu_log = models.ComputerMSULog.all().filter( 'uuid =', uuid).order('-mtime').fetch(limit) applesus_installs = models.InstallLog.all().filter( 'uuid =', uuid).filter('applesus =', True).order('-mtime').fetch(limit) installs = models.InstallLog.all().filter('uuid =', uuid).filter( 'applesus =', False).order('-mtime').fetch(limit) exits = models.PreflightExitLog.all().filter( 'uuid =', uuid).order('-mtime').fetch(limit) install_problems = models.ClientLog.all().filter( 'action =', 'install_problem').filter('uuid =', uuid).order('-mtime').fetch(limit) tags = {} tags_list = [] groups = {} groups_list = [] duplicates = [] if computer: # Generate tags data. tags_list = models.Tag.GetAllTagNamesForEntity(computer) for tag in tags_list: tags[tag] = True for tag in models.Tag.GetAllTagNames(): if tag not in tags: tags[tag] = False tags = json.dumps(tags, sort_keys=True) # Generate groups data. groups_list = models.Group.GetAllGroupNamesForUser(computer.owner) for group in groups_list: groups[group] = True for group in models.Group.GetAllGroupNames(): if group not in groups: groups[group] = False groups = json.dumps(groups, sort_keys=True) admin.AddTimezoneToComputerDatetimes(computer) computer.connection_dates.reverse() computer.connection_datetimes.reverse() duplicates = models.Computer.all().filter( 'serial =', computer.serial).fetch(20) duplicates = [e for e in duplicates if e.uuid != computer.uuid] try: uuid_lookup_url = settings.UUID_LOOKUP_URL except AttributeError: uuid_lookup_url = None try: owner_lookup_url = settings.OWNER_LOOKUP_URL except AttributeError: owner_lookup_url = None values = { 'report_type': 'host', 'uuid_lookup_url': uuid_lookup_url, 'owner_lookup_url': owner_lookup_url, 'client_site_enabled': settings.CLIENT_SITE_ENABLED, 'computer': computer, 'applesus_installs': applesus_installs, 'installs': installs, 'client_log_files': client_log_files, 'msu_log': msu_log, 'install_problems': install_problems, 'preflight_exits': exits, 'tags': tags, 'tags_list': tags_list, 'groups': groups, 'groups_list': groups_list, 'host_report': True, 'limit': SINGLE_HOST_DATA_FETCH_LIMIT, 'is_support_user': auth.IsSupportUser(), 'is_security_user': auth.IsSecurityUser(), 'is_physical_security_user': auth.IsPhysicalSecurityUser(), 'self_report': self_report, 'duplicates': duplicates, 'tags_xsrf_token': xsrf.XsrfTokenGenerate('tags'), 'groups_xsrf_token': xsrf.XsrfTokenGenerate('groups'), } if popup: self.Render('host_popup.html', values) else: self.Render('host.html', values)