def get(self, uuid, **kwargs): try: self.metadata = sites.get_metadata(uuid) except DataRequestException as e: return render_template(self.template, errors=e.errors) self.set_template_args(**kwargs) return render_template(self.template, **self.template_args)
def set_template_args(self, site_id=None, aggregate_id=None): """Builds a dictionary of the appropriate template arguments. """ self.template_args = {} # If an id was passed in, set the breadcrumb. The request for location # metadata may triggers an error if the object doesnt exist or the user # does not have access. So we can handle with a 404 message instead of # silently failing and listing all objects. if site_id is not None or aggregate_id is not None: if site_id is not None: location_metadata = sites.get_metadata(site_id) else: location_metadata = aggregates.get_metadata(aggregate_id) self.template_args['breadcrumb'] = self.breadcrumb_html( self.get_breadcrumb(location_metadata)) else: self.template_args['page_title'] = 'Forecasts and Observations' self.template_args['subnav'] = self.format_subnav( **self.get_subnav_kwargs(site_id=site_id, aggregate_id=aggregate_id)) table, _ = self.table_function(site_id, aggregate_id) self.template_args['data_table'] = table self.template_args['current_path'] = request.path
def breadcrumb_html(self, site_id=None, organization=None, **kwargs): breadcrumb_format = '/<a href="{url}">{text}</a>' breadcrumb = '' if self.data_type == 'cdf_forecast_group': type_label = 'CDF Forecast' else: type_label = self.data_type.title() if site_id is not None: site_metadata_request = sites.get_metadata(site_id) if site_metadata_request.status_code != 200: abort(404) site_metadata = site_metadata_request.json() breadcrumb += breadcrumb_format.format( url=url_for('data_dashboard.sites'), text='Sites') breadcrumb += breadcrumb_format.format(url=url_for( 'data_dashboard.site_view', uuid=site_id), text=site_metadata['name']) breadcrumb += breadcrumb_format.format(url=url_for( f'data_dashboard.{self.data_type}s', uuid=site_id), text=type_label) return breadcrumb
def set_site_or_aggregate_metadata(self): """Searches for a site_id or aggregate_id in self.metadata and loads the expected metadata object from the api in either the 'site' or 'aggregate' key. If the object could not be retrieved, sets a warning and reraises the DataRequestError. """ if self.metadata.get('site_id') is not None: try: self.metadata['site'] = sites.get_metadata( self.metadata['site_id']) except DataRequestException: self.template_args.update({ 'warnings': { 'Site Access': ['Site inaccessible. Plots will not be displayed.'] }, }) raise elif self.metadata.get('aggregate_id'): try: self.metadata['aggregate'] = aggregates.get_metadata( self.metadata['aggregate_id']) except DataRequestException: self.template_args.update({ 'warnings': { 'Aggregate Access': [ 'Aggregate inaccessible. Plots will not be ' 'displayed.' ] }, }) raise else: self.template_args.update({ 'warnings': { 'Warning': ['Site or aggregate has been deleted.'], } }) raise DataRequestException(404)
def get(self, uuid, **kwargs): metadata_request = sites.get_metadata(uuid) if metadata_request.status_code != 200: abort(404) self.metadata = metadata_request.json() return render_template(self.template, **self.template_args(**kwargs))
def get_site_metadata(self, site_id): site_metadata_request = sites.get_metadata(site_id) if site_metadata_request.status_code != 200: abort(404) site_metadata = site_metadata_request.json() return site_metadata
def get_site_metadata(self, site_id): site_request = sites.get_metadata(site_id) return site_request.json()