def get_context_data(self, **kwargs): """Extension of get_context_data Add context data to drive menu nav highlights and breadcrumbs. """ context = super(ScenarioDetailView, self).get_context_data(**kwargs) context['nav_button'] = "browse_scenario" self.request.session['emod_nav_button'] = "browse_scenario" # This is here so that Alex can run this later and change all production database entries # all_runs = DimRun.objects.all() # for run in all_runs: # if run.start_date_key and run.start_date_key.timestamp: # run.new_start_date_key = run.start_date_key.timestamp # if run.end_date_key and run.end_date_key.timestamp: # run.new_end_date_key = run.end_date_key.timestamp # print run.new_start_date_key # print run.new_end_date_key # run.save() scenario_id = kwargs['scenario_id'] scenario = EMODBaseline.from_dw(pk=scenario_id) # Hack until DimBaseline/EMODBaseline issues are sorted out dim_scenario = DimBaseline.objects.get(id=scenario_id) # Hack for calibration species_info = {} the_config = ast.literal_eval(scenario.get_config_file().content) the_species = the_config['parameters']['Vector_Species_Params'] for specie in the_species: species_info[specie] = {} if 'EIR' in the_species[specie]: eir = the_species[specie]['EIR'] else: eir = 20.0 / float(len(the_species)) if 'Sporozoite_Rate' in the_species[specie]: sporozoite = the_species[specie]['Sporozoite_Rate'] else: sporozoite = 0.003 species_info[specie]['eir'] = eir species_info[specie]['sporozoite'] = sporozoite context['species_info'] = species_info print species_info # data_to_send = {} # data_to_send['eir'] = [] # data_to_send['sporozoite'] = [] # the_config = ast.literal_eval(scenario.get_config_file().content) # the_species = the_config['parameters']['Vector_Species_Params'] # for i in range(len(the_species)): # data_to_send['eir'].append(20) # data_to_send['sporozoite'].append(0.003) # context['data_to_send'] = data_to_send if dim_scenario.metadata and 'calibration_status' in dim_scenario.metadata: scenario.calibration_status = json.loads(dim_scenario.metadata)['calibration_status'] else: scenario.calibration_status = "Ready" if dim_scenario.metadata: current_metadata = json.loads(dim_scenario.metadata) else: current_metadata = {} if 'calibration_verification_run_id' in current_metadata: calibration_verification_run = DimRun.objects.get(id=current_metadata['calibration_verification_run_id']) if calibration_verification_run.status == '1/1/0': vector_species_parameters = the_config['parameters']['Vector_Species_Params'] calibration_verification_info = {} for species in vector_species_parameters: calibration_verification_info[species] = {} calibration_verification_info[species]['sporozoite_rate'] = {} calibration_verification_info[species]['eir'] = {} calibration_verification_info[species]['biting_rate'] = {} target_sporozoite_rate = vector_species_parameters[species]['Sporozoite_Rate'] daily_sporozoite_rate_2_3 = get_average_daily_sporozoite_rate(calibration_verification_run, species, 2, 3) daily_sporozoite_rate_4_9 = get_average_daily_sporozoite_rate(calibration_verification_run, species, 4, 9) daily_sporozoite_rate_10_20 = get_average_daily_sporozoite_rate(calibration_verification_run, species, 10, 20) calibration_verification_info[species]['sporozoite_rate']['target'] = target_sporozoite_rate calibration_verification_info[species]['sporozoite_rate']['years_2_3'] = daily_sporozoite_rate_2_3 calibration_verification_info[species]['sporozoite_rate']['years_4_9'] = daily_sporozoite_rate_4_9 calibration_verification_info[species]['sporozoite_rate']['years_10_20'] = daily_sporozoite_rate_10_20 target_average_annual_eir = vector_species_parameters[species]['EIR'] annual_eir_2_3 = get_average_daily_eir(calibration_verification_run, species, 2, 3) * 365 annual_eir_4_9 = get_average_daily_eir(calibration_verification_run, species, 4, 9) * 365 annual_eir_10_20 = get_average_daily_eir(calibration_verification_run, species, 10, 20) * 365 calibration_verification_info[species]['eir']['target'] = target_average_annual_eir calibration_verification_info[species]['eir']['years_2_3'] = annual_eir_2_3 calibration_verification_info[species]['eir']['years_4_9'] = annual_eir_4_9 calibration_verification_info[species]['eir']['years_10_20'] = annual_eir_10_20 target_average_daily_biting_rate = 'NA' daily_biting_rate_2_3 = get_average_daily_biting_rate(calibration_verification_run, species, 2, 3) daily_biting_rate_4_9 = get_average_daily_biting_rate(calibration_verification_run, species, 4, 9) daily_biting_rate_10_20 = get_average_daily_biting_rate(calibration_verification_run, species, 10, 20) calibration_verification_info[species]['biting_rate']['target'] = target_average_daily_biting_rate calibration_verification_info[species]['biting_rate']['years_2_3'] = daily_biting_rate_2_3 calibration_verification_info[species]['biting_rate']['years_4_9'] = daily_biting_rate_4_9 calibration_verification_info[species]['biting_rate']['years_10_20'] = daily_biting_rate_10_20 context['calibration_verification_info'] = calibration_verification_info context['scenario_id'] = kwargs['scenario_id'] context['scenario_userid'] = scenario.user.id context['dim_user'] = DimUser.objects.get_or_create(username=self.request.user.username)[0] scenario.last_edited = scenario._last_edited scenario.is_public = scenario._is_public # Hack until DimBaseline/EMODBaseline issues are sorted out if dim_scenario.metadata: metadata = json.loads(dim_scenario.metadata) if 'representative' in metadata: scenario.is_representative = True if 'is_complete' in metadata['representative']: scenario.representative_is_completed = True else: scenario.representative_is_completed = False scenario.name = derive_autoname(dim_scenario) else: scenario.is_representative = False context['scenario'] = scenario file_list = [] for my_file in scenario.files: if my_file['type'] == 'config': try: context['config'] = json.dumps(ast.literal_eval(scenario.get_file_by_type('config').content), sort_keys=True, indent=4, separators=(',', ': ')) except AttributeError: # if file list was returned, use last file context['config'] = json.dumps(ast.literal_eval( scenario.get_file_by_type('config')[scenario.get_file_by_type('config').count() - 1].content), sort_keys=True, indent=4, separators=(',', ': ')) elif my_file['type'] == 'campaign': try: scenario_campaign = json.dumps(ast.literal_eval(scenario.get_file_by_type('campaign').content), sort_keys=True, indent=4, separators=(',', ': ')) if len(json.loads(scenario_campaign)['Events']) > 0: context['campaign'] = scenario_campaign else: context['has_campaign'] = None except AttributeError: # if file list was returned, use last file context['campaign'] = json.dumps(ast.literal_eval( scenario.get_file_by_type('campaign')[scenario.get_file_by_type('campaign').count() - 1].content), sort_keys=True, indent=4, separators=(',', ': ')) else: file_list.append(my_file['name']) context['file_list'] = file_list # Get a list of the runs for this scenario adapter = EMOD_Adapter(self.request.user.username) run_list = DimRun.objects.filter(baseline_key=scenario.id) dim_scenario = DimBaseline.objects.get(id=scenario.id) if dim_scenario.simulation_group: simulation_group = dim_scenario.simulation_group simulation_list = Simulation.objects.filter(group=simulation_group) for simulation in simulation_list: if simulation.status == sim_status.SCRIPT_DONE: simulation.is_complete = True elif simulation.status == sim_status.SCRIPT_ERROR: simulation.has_failed = True context['simulation_list'] = simulation_list # sort descending by id (for those old runs that all have the same old time_created context['run_list'] = sorted(run_list, key=lambda x: x.id, reverse=True) # sort descending by time_saved context['run_list'] = sorted(context['run_list'], key=lambda x: x.time_created, reverse=True) note_list = [] for run in context['run_list']: try: run_diffs = [] # config diffs run_config = run.jcd.jcdict['config.json']['Changes'][0]['config.json/parameters'] scenario_config = json.loads(context['config'])['parameters'] for key in scenario_config: if scenario_config[key] != run_config[key]: run_diffs.append({'key': key, 'run': run_config[key], 'scenario': scenario_config[key]}) # ToDo: split out each key difference of Vector Species Parameters in both # campaign diffs if 'campaign.json' not in run.jcd.jcdict: run_diffs.append({'key': 'Interventions', 'run': 'None'}) else: run_campaign = run.jcd.jcdict['campaign.json']['Changes'][0]['campaign.json/Events'] if json.loads(scenario_campaign)['Events'] != run_campaign: run_diffs.append({'key': 'Interventions', 'run': run_campaign, 'scenario': str(json.loads(scenario_campaign)['Events'])}) # ToDo: split out each key difference of each Intervention in both # else: # run_diffs.append({'key': 'Interventions', 'run': 'See below', # 'scenario': '<a class="btn tooltip_link" href="#campaign">see below</a>'}) run.diffs = run_diffs except (AttributeError, KeyError): # no JCD? run.diffs = [{'Unknown Error Occurred:': 'no changes found.'}] # get the status of each run status = adapter.run_status(run.id) if status is None: run.my_completed = None else: run.my_completed = status[0] run.my_total = status[1] run.my_failed = status[2] run.percent_complete = (run.my_completed + run.my_failed) / run.my_total run.my_incomplete = int(run.my_total) - (int(run.my_completed) + int(run.my_failed)) if int(status[0]) > 0: context['has_results'] = 1 note_list.extend(adapter.fetch_notes(run_id=int(run.id), as_object=True)) if note_list != []: context['note_list'] = note_list return context
def get_context_data(self, form, **kwargs): """Extension of the get_context_data method of the NamedUrlSessionWizardView. Select/process data based on which step of the Form Wizard it's called for. - Date: passes existing start_date, end_date values or nothing then template will use min and max. - Preview gets all wizard step data gathered so far. """ context = super(BaselineWizardView, self).get_context_data(form=form, **kwargs) # All steps: if 'edit_scenario' in self.storage.extra_data: # if we're in edit mode, let templates know ("Start Over" vs "Cancel Edit" button) context['edit_scenario'] = self.storage.extra_data['edit_scenario'] ######### Start code for specific steps ######### if self.steps.current == 'location': the_request = self.request adapter = EMOD_Adapter(self.request.user.username) templates = adapter.fetch_template_list() # get for description for key in templates.keys(): if 'edit_scenario' in self.storage.extra_data \ and 'scenario' in self.request.session.keys() \ and self.request.session['scenario'].template is not None \ and self.request.session['scenario'].template.id is not None \ and self.request.session['scenario'].template.id == key: # set the checked option based on the scenario being edited templates[key]['checked'] = 1 elif 'location' in self.storage.data['step_data'].keys()\ and 'location-template_id' in self.storage.data['step_data'][u'location'].keys()\ and unicode(key) == \ self.storage.data['step_data'][u'location'][u'location-template_id'][0]: # set checked option based on wizard value already saved templates[key]['checked'] = 1 # For now: limit list to Solomon and Kenya templates templates_a = {} templates_b = {} templates_c = {} templates_d = {} templates_e = {} # reorder the templates so Vietnam 1st, Kenya 2nd, Solomon 3rd for key in templates.keys(): if 'Vietnam' in templates[key]['name']: templates_a.update({key: templates[key]}) for key in templates.keys(): if 'Kenya' in templates[key]['name']: templates_b.update({key: templates[key]}) for key in templates.keys(): if 'Solomon' in templates[key]['name']: templates_c.update({key: templates[key]}) for key in templates.keys(): if 'Uganda' in templates[key]['name']: templates_d.update({key: templates[key]}) for key in templates.keys(): if 'Bougainville' in templates[key]['name']: templates_e.update({key: templates[key]}) pager_size = the_request.POST.get('pager_size') if pager_size is None: pager_size = 10 else: pager_size = int(pager_size) pager_offset = the_request.POST.get('pager_offset') if pager_offset is None: pager_offset = 0 else: pager_offset = int(pager_offset) pager_group = the_request.POST.get('pager_group') if pager_group is None: pager_group = 0 else: try: pager_group = int(pager_group) except TypeError: pager_group = 0 template_count = len(templates) pager_count = template_count / pager_size if template_count % pager_size != 0: pager_count += 1 context['pager_offset'] = pager_offset context['pager_size'] = pager_size context['pager_group'] = pager_group context['pager_count'] = range(1, pager_count + 1) context['pager_max'] = pager_count context['templates_a'] = templates_a context['templates_b'] = templates_b context['templates_c'] = templates_c context['templates_d'] = templates_d context['templates_e'] = templates_e context['template_count'] = template_count context['current_start'] = pager_offset * pager_size + 1 context['current_stop'] = min(template_count, pager_offset * pager_size + pager_size) return context if self.steps.current == 'climate': my_template = DimTemplate.objects.get(id=self.storage.extra_data['template_id']) context['template'] = my_template # set up the context to include the files (if any) attached to the scenario my_types = [ 'air binary', #'air json', 'humidity binary', #'humidity json', #'land_temp binary', #'land_temp json', 'rainfall binary'] #'rainfall json'] # get scenario files: types/names context['climate_files'] = {} for my_file in self.request.session['scenario'].get_files(): # my_file[0] id # my_file[1] name # my_file[2] type if my_file[2] in my_types: context['climate_files'].update({my_file[2].split(' ')[0]: my_file[1]}) context['climate_files'] = sorted(context['climate_files'].iteritems()) context['scenario_id'] = self.request.session['scenario'].id my_template = DimTemplate.objects.get(id=self.storage.extra_data['template_id']) context['template'] = my_template if self.steps.current == 'demographic': my_template = DimTemplate.objects.get(id=self.storage.extra_data['template_id']) context['template'] = my_template if self.steps.current == 'species': ### # Get list of species user can base NEW species creation on (drop-down menu) # and to use for adding to a simulation # get from template try: context['species_templates'] = \ self.storage.extra_data['scenario_template_config']['parameters']['Vector_Species_Params'] except KeyError: pass ### # Modify the above list to make the "select from" sources species_list = [] try: # turn into objects for key, value in context['species_templates'].items(): json_string = '{"' + key + '":' + json.dumps(value) + "}" species_list.append(Species(name=key, is_template=1, is_public=0, json=json_string, created_by=self.request.user)) except KeyError: pass # Add in public and user's own custom species objects from DB for species in Species.objects.filter(Q(is_public=1) | Q(created_by=self.request.user)): species_list.append(species) # This serves as the "from template", "user's custom" and "public" species available context['species_list'] = species_list ### # Populate the already chosen list # # Get Baseline or Template ID for passing to SpeciesCreate View # # because wizard storage won't be available in SpeciesCreate View my_scenario_id = self.storage.extra_data.get('scenario_id') my_species = [] try: my_species.extend(self.request.session['scenario_config']['parameters']['Vector_Species_Names']) except (KeyError, TypeError): pass my_species = sorted(set(my_species)) context['species_chosen'] = {u'species-species': json.dumps(my_species), 'scenario_id': my_scenario_id, u'orig_json_obj': ConfigData.objects.filter( type='JSONConfig', name='default_emod_vector')[0].json} context['species_chosen_values'] = \ json.dumps(self.request.session['scenario_config']['parameters']['Vector_Species_Params']) if self.steps.current == 'parasite' or self.steps.current == "vector": step_name = self.steps.current # build titles for template from name, misc my_object = ConfigData.objects.filter(type='JSONConfig', name=step_name)[0] context['step_title'] = step_name.title() + " " + my_object.misc.split(":")[1].title() context['description'] = my_object.description # if the user has not already been through this step # populate the form with the template values. if not self.storage.get_step_data(step_name): try: context['template_values'] = self.request.session['scenario_config']['parameters'] except KeyError: pass if self.steps.current == 'run': form.fields['name'].initial = 'Test Run ' + str(datetime.datetime.now()) if self.steps.current == 'review': # get list of runs for this scenario adapter = EMOD_Adapter(self.request.user.username) run_list = DimRun.objects.filter(baseline_key=self.request.session['scenario'].id) if len(run_list) > 0: # sort descending by id (for those old runs that all have the same old time_created run_list = sorted(run_list, key=lambda x: x.id, reverse=True) # sort descending by time_saved run_list = sorted(run_list, key=lambda x: x.time_created, reverse=True) # get most recent run, is it still running? my_run = run_list[0] # check to see if a run is currently running status = adapter.run_status(my_run.id) if status is None: my_run.my_completed = None else: my_run.my_completed = status[0] my_run.my_failed = status[2] if status[0] > 0: context['scenario_has_results'] = 1 # this sets if any of the runs here has results my_run.my_total = status[1] my_run.my_percent_complete = float(status[0]) / float(status[1]) * 100 my_run.my_percent_failed = float(status[2]) / float(status[1]) * 100 my_run.errors = adapter.fetch_errors(my_run.id) # get the results for the last run(s) if it's finished context['my_run'] = my_run # #6329 - pass in scenario for display of info context['scenario'] = self.request.session['scenario'] my_config = self.request.session['scenario'].get_file_by_type('config').content context['Simulation_Duration'] = ast.literal_eval(my_config)['parameters']['Simulation_Duration'] if self.steps.current == 'run': # check for existing runs in this scenario if 'scenario' in self.request.session.keys(): my_runs = DimRun.objects.filter(baseline_key=self.request.session['scenario'].id) if len(my_runs) > 0: context['has_runs'] = len(my_runs) # populate the step data so if the skip button is used, # the wizard doesn't skip back to here upon wizard submission (skip isn't a submit, just goto_step) self.storage.set_step_data('run', {u'baseline_wizard_view-current_step': [u'run']}) 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(ScenarioBrowsePublicView, self).get_context_data(**kwargs) the_request = self.request context['nav_button'] = 'scenario_browse_public' if context['object_list'] is None: return context 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) paginator = Paginator(context['object_list'], pager_size) page = int(the_request.GET.get('page') or 1) try: scenario_list = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. scenario_list = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. scenario_list = paginator.page(paginator.num_pages) scenario_count = paginator.count context['pager_size'] = pager_size context['scenario_list'] = scenario_list.object_list context['pager_obj'] = scenario_list 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['dim_user'] = DimUser.objects.get_or_create(username=self.request.user.username)[0] adapter = EMOD_Adapter(self.request.user.username) for scenario in context['scenario_list']: run_list = DimRun.objects.filter(baseline_key=scenario.id).order_by('-id') scenario.run_list = run_list scenario.run_count = len(run_list) scenario.run_id = 0 # set a dummy id my_model_version = '' # get status for each run for run in run_list: status = adapter.run_status(run.id) if status is None: run.my_completed = "No" else: run.my_completed = status[0] run.my_failed = status[2] if int(status[0]) > 0: scenario.run_id = run.id scenario.run_name = run.name scenario.location_key = run.location_key #break my_model_version = run.model_version try: scenario.model_version = scenario.my_scenario.my_model_version except (AttributeError, ObjectDoesNotExist): scenario.model_version = my_model_version return context