def getLocationGraphs(request, location_id=None): """ Function to get graph data from DW by url - Input: location ID - Output: JSON object of highchart data - if called without location ID, returns empty dict """ if location_id == None: chart_dict = {} else: adapter = EMOD_Adapter() location_dict = adapter.fetch_locations() # get HighChart data object try: chart_dict = adapter.fetch_weather_chart(location_id, 'chart-div', start_date=location_dict[int(location_id)]['start_date'], end_date=location_dict[int(location_id)]['end_date']) except: chart_dict = {} return HttpResponse(json.dumps(chart_dict), mimetype="application/json")
def get_context_data(self, **kwargs): """Extension of get_context_data Add context data to drive detail view. """ context = super(TemplateDetailView, self).get_context_data(**kwargs) context['selected_scenario'] = self.request.session['selected_scenario'] adapter = EMOD_Adapter() my_template_id = kwargs['template_id'] my_temp_list = adapter.fetch_template_list() for id in my_temp_list: if id == int(my_template_id): context['template_name'] = my_temp_list[id]['name'] context['template_description'] = my_temp_list[id]['description'] context['template_campaign_description'] = my_temp_list[id]['files']['campaign.json']['description'] context['template_config_description'] = my_temp_list[id]['files']['config.json']['description'] my_loc_list = adapter.fetch_locations() for loc_id in my_loc_list: if loc_id == my_temp_list[id]['location_ndx']: context['template_location_name'] = my_loc_list[loc_id]['place'] + ', ' + my_loc_list[loc_id]['country'] context['template_location_start_date'] = my_loc_list[loc_id]['start_date'] context['template_location_end_date'] = my_loc_list[loc_id]['end_date'] context['template_location_resolution'] = my_loc_list[loc_id]['resolution'] context['template_location_link'] = my_loc_list[loc_id]['link'] my_temp = adapter.fetch_template(int(kwargs['template_id'])) my_config = ast.literal_eval(my_temp['config.json']) context['template_campaign_warning'] = my_config['parameters']['Enable_Interventions'] context['template_config'] = re.sub("\r{2,}", '\r', my_temp['config.json']) context['template_campaign'] = re.sub("\r{2,}", '\r', my_temp['campaign.json']) return context
def get_context_data(self, **kwargs): """Extension of get_context_data Add context data to drive menu nav highlights, breadcrumbs and pagers. """ context = super(ScenarioBrowseView, self).get_context_data(**kwargs) the_request = self.request context['nav_button'] = 'scenario_browse' pager_size = the_request.POST.get('pager_size') or the_request.GET.get('pager_size') if pager_size is None: pager_size = 10 else: pager_size = int(pager_size) # Get list of folders in this folder (if no current folder, include folders with no parent) if 'folder_id' in kwargs.keys(): current_folder = kwargs['folder_id'] or the_request.POST.get('folder_id') or the_request.GET.get('folder_id') or None context['current_folder'] = Folder.objects.get(pk=current_folder) else: current_folder = None # get a list of all scenarios in order to get count of sims in each folder scenario_list_all = list(DimBaseline.objects.filter(user=DimUser.objects.get(username=self.request.user.username), is_deleted=False)) my_count = {} for scen in scenario_list_all: if scen.folder_id in my_count.keys(): my_count[scen.folder_id] += 1 else: my_count[scen.folder_id] = 1 # create a dictionary of children (keys are parents, value is list of children) folder_list_all = list(Folder.objects.filter(user=DimUser.objects.get(username=self.request.user.username), is_deleted=False)) my_dict = defaultdict(list) for folder in folder_list_all: if folder.id in my_count.keys(): counter = ' (' + str(my_count[folder.id]) + ')' else: counter = '' if folder.parent is None: my_dict[0].append({'id': folder.id, 'name': folder.name, 'child_count': counter}) else: my_dict[folder.parent.id].append({'id': folder.id, 'name': folder.name, 'child_count': counter}) current_found = 0 # Set flag that deterimines if the current folder has been visited in the tree if None in my_count.keys(): counter_root = ' (' + str(my_count[None]) + ')' else: counter_root = '' folder_tree = '{title: "My Simulations' + counter_root + '", folder: true, expanded: true' if current_folder is None: folder_tree += ', active: true' current_found = 1 # context['folder_tree'] = '{title: "My Tororo Folder full of Simulations (and folders) in home folder ", folder: true }, ' \ # '{title: "Folder 2", folder: true, ' \ # 'children: [ ' \ # '{ title: "Sub-item 2.1", folder: true }, ' \ # '{ title: "Sub-item 2.2" }] }, ' \ # '{ title: "Item 3" }' # without root: # context['folder_tree'] = '{title: "Solomon Islands", folder: true}, {title: "Second folder", folder: true, children: [ {title: "subfolder", folder: true}] }' #folder_tree = add_children(0, my_dict) # with root: # context['folder_tree'] = '{title: "My Simulations", folder: true, children: [ {title: "Solomon Islands", folder: true}, {title: "Second folder", folder: true, children: [ {title: "subfolder", folder: true}] }] }' folder_tree += add_children(0, my_dict, current_folder, current_found) + '}' context['folder_tree'] = str(folder_tree) # Get list of scenarios scenario_list = list(DimBaseline.objects.filter(user=DimUser.objects.get(username=self.request.user.username), folder=current_folder, is_deleted=False)) if scenario_list is None: return context # sort descending by id (for those old scenarios that all have the same old time_created scenario_list = sorted(scenario_list, key=lambda x: x.id, reverse=True) # sort descending by time_saved (version) scenario_list = sorted(scenario_list, key=lambda x: x.last_modified, reverse=True) # merge folders into scenario list # - show folders in file list #scenario_list = folder_list + scenario_list # Flag scenarios that are representative for scenario in scenario_list: # Hack for now. I don't remember if it is still needed, but I will fix it later. Not currently important. if scenario.description == "Made with representative workflow" and not scenario.metadata: scenario.metadata = json.dumps({'representative': ''}) scenario.save() if scenario.metadata: metadata = json.loads(scenario.metadata) if 'representative' in metadata: scenario.is_representative = True if 'is_complete' in metadata['representative']: scenario.representative_is_complete = metadata['representative']['is_complete'] else: scenario.representative_is_complete = False scenario.name = derive_autoname(scenario) else: scenario.is_representative = False paginator = Paginator(scenario_list, pager_size) page = int(the_request.GET.get('page') or 1) try: scenarios = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. scenarios = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. scenarios = paginator.page(paginator.num_pages) scenario_count = len(scenario_list) context['pager_size'] = pager_size context['scenarios'] = scenarios context['scenario_range'] = range(paginator.num_pages) context['scenario_count'] = scenario_count context['current_start'] = (page-1)*pager_size + 1 context['current_stop'] = min(scenario_count, (page-1)*pager_size + pager_size) context['my_username'] = self.request.user.username # get locations (need name from dict by id) adapter = EMOD_Adapter(self.request.user.username) template_list = adapter.fetch_template_list() location_dict = adapter.fetch_locations() # gets list based on EMOD_buffer list in model_adapter.py # determine which step edit buttons link to step_list = [i[0] for i in named_baseline_forms] for scenario in scenario_list: if scenario._meta.object_name != 'Folder': try: # check to see if it has a campaign file (besides the empty default) if scenario.is_approved: my_scenario = EMODBaseline.from_dw(pk=scenario['id']) try: scenario_campaign = json.dumps(ast.literal_eval(my_scenario.get_file_by_type('campaign').content), sort_keys=True, indent=4, separators=(',', ': ')) except (AttributeError, ObjectDoesNotExist): try: # if file list was returned, use last file scenario_campaign = json.dumps(ast.literal_eval( my_scenario.get_file_by_type('campaign')[my_scenario.get_file_by_type('campaign').count() - 1].content), sort_keys=True, indent=4, separators=(',', ': ')) except ObjectDoesNotExist: pass if len(json.loads(scenario_campaign)['Events']) > 0: scenario['has_interventions'] = 1 if scenario.template: scenario.location_name = str(DimTemplate.objects.get(id=scenario.template.id).location_key) # add model version to scenario scenario.model_version = DimTemplate.objects.get(id=scenario.template.id)\ .model_version.replace('emod ', '') except: pass return context