def get_context_data(self, **kwargs):
        """Extension of get_context_data
            populate the existing metaData values
        """
        context = super(RunMetadataDetailView, self).get_context_data(**kwargs)
        the_request = self.request

        adapter = EMOD_Adapter(the_request.user.username)
        my_run = adapter.fetch_runs(run_id=int(kwargs['run_id']))

        # if we got here by edit URL, set edit mode.
        if 'mode' in kwargs.keys() and kwargs['mode'] == 'edit' and my_run.baseline_key.user.username == the_request.user.username:
            context['edit'] = True

        try:
            context['metaData'] = RunMetaData(my_run)
        except RuntimeError:
            set_notification('alert-error', '<strong>Error!</strong> Failed to retrieve metadata.', self.request.session)
            scenario_id = my_run.baseline_key
            if scenario_id:
                return redirect("ts_emod_scenario_details", scenario_id=str(scenario_id))
            else:
                return redirect("ts.index")

        return context
    def post(self, request, *args, **kwargs):
        """Handle the post of the edit form
        """
        my_form = request.POST
        adapter = EMOD_Adapter(request.user.username)
        my_run = adapter.fetch_runs(run_id=int(kwargs['run_id']))

        metaData = RunMetaData(my_run)
        changed_flag = 0

        for property, value in my_form.iteritems():
            if property in vars(metaData):
                setattr(metaData, property, value)
                changed_flag = 1

        # unchecked checkboxes do not register in the form
        #  so must set to off here if user unchecked is_public
        if 'is_public' not in my_form.keys() and metaData.is_public == 'on':
            setattr(metaData, 'is_public', 'off')
            changed_flag = 1

        # is user has made any changes, save it
        if changed_flag == 1:
            metaData.saveMetaData()

        return redirect("ts_emod_run_metadata_details", run_id=int(kwargs['run_id']))
示例#3
0
    def get_context_data(self, form, **kwargs):
        context = super(LaunchTool, self).get_context_data(form, **kwargs)

        try:
            context['nav_button'] = self.request.session['emod_nav_button']
        except KeyError:
            pass

        # We need either a run id or scenario id (both really)
        if 'run_id' in kwargs:
            run_id = kwargs['run_id']
            if run_id > 0:
                self.storage.extra_data['emod_launch_run_id'] = run_id
                context['run_named'] = 1
                # get the run's name, to prepopulate the name field
                adapter = EMOD_Adapter(self.request.user.username)
                my_run_id = int(self.storage.extra_data['emod_launch_run_id'])
                my_run = adapter.fetch_runs(scenario_id=-1, run_id=int(my_run_id))
                context['form'].fields['name'].initial = my_run.name
                # save for checking for changes in done method
                self.storage.extra_data['old_run_name'] = my_run.name
        #else:
        # get the specified scenario - error if none specified
        try:
            if 'scenario_id' in kwargs:
                self.storage.extra_data['scenario_id'] = kwargs['scenario_id']
            elif 'scenario_id' not in self.storage.extra_data:
                self.storage.extra_data['error_invalid_id'] = 1
                Exception()
        except KeyError:
            #If there was no scenario id in the url or storage, this wizard is useless... redirect somewhere else
            set_notification('alert-error', 'Error Launching Simulation: Unknown/Invalid run provided. ' +
                                            'Please select a simulation below to launch.', self.request.session)
        return context
示例#4
0
def saveNote(request, run_id, note_id=None ):
    """Function to save Note DB Objects
    """
    if request.method == 'POST':
        #note = Note(text=request.POST.get('text'), run_key_id=run_id)
        #note.save()

        if run_id == None:
            run_id = request.POST.get('run_id')

        adapter = EMOD_Adapter(request.user.username)
        note_id = adapter.save_note(run_id, note=request.POST.get('text'))

    if note_id != None:
        set_notification('alert-success','<strong>Success!</strong> You have successfully saved the note.', request.session)
    else:
        set_notification('alert-error','<strong>Error!</strong> There was a problem saving the note.', request.session)
    return redirect(request.environ['HTTP_REFERER'])
示例#5
0
def run_to_scenario(request, run_id):
    """ Function to allow duplication of run DB Objects """
    if not request.user.is_authenticated():
        set_notification('alert-error', '<strong>Error!</strong> Please log in before duplicating. '
                                        'Run was not duplicated.', request.session)
        return redirect("ts_emod_scenario_browse")
    else:
        # Save a copy of the run to Adapter/DW
        try:
            adapter = EMOD_Adapter(request.user.username)
            old_run = adapter.fetch_runs(run_id=int(run_id), as_object=True)
            # create empty scenario
            new_scenario = request.session['scenario'] = EMODBaseline(
                name=old_run.name + ' copy',
                description='Duplicate of ' + old_run.name,
                model=DimModel.objects.get(model='EMOD'),
                user=DimUser.objects.get_or_create(username=request.user.username)[0]
            )

            new_scenario.template = DimTemplate.objects.get(id=old_run.template_key_id)

            new_scenario.model_version = new_scenario.template.model_version

            # get input files for run
            file_dict = old_run.get_input_files()
            new_scenario.add_file_from_string('campaign', 'campaign.json', file_dict['campaign.json'],
                                              description="Added during duplication from run.")
            new_scenario.add_file_from_string('config', 'config.json', file_dict['config.json'],
                                              description="Added during duplication from run.")

            # ToDo: get bin input files for run (from run's template)
            # - for now, user will have to edit, and location step will provide them

            new_scenario.save()

            set_notification('alert-success', '<strong>Success!</strong> The run ' + old_run.name
                                              + ' has been duplicated.', request.session)
        except:
            print 'Error saving the duplicate run to scenario: %s' % old_run.name
            set_notification('alert-error', '<strong>Error!</strong> Error duplicating the run '
                                            + old_run.name + '.', request.session)
            return redirect("ts_emod_scenario_browse")

    return redirect("ts_emod_scenario_details", scenario_id=str(new_scenario.id))
示例#6
0
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")
示例#7
0
文件: util.py 项目: vecnet/vnetsource
def get_average_daily_value(dim_run,
                            channel_name,
                            species_name,
                            start_year=1,
                            end_year=None):
    """
    :param dim_run:
    :type dim_run: DimRun
    :param species_name:
    :param start_year:
    :param end_year:
    :return:
    """
    adapter = EMOD_Adapter(dim_run.baseline_key.user.username)
    data = adapter.fetch_data(dim_run.dimexecution_set.all()[0],
                              channel_name=channel_name,
                              channel_type=species_name)
    data = data.values()[0]

    # Start and End years start from 1, not from 0
    start_year -= 1
    if start_year < 0:
        start_year = 0
    end_year -= 1
    if end_year < 0:
        return None

    if end_year is None:
        data = data[int(start_year*365):]
    else:
        if len(data) < int(end_year*365) + 365:
            return None
        data = data[int(start_year*365):int(end_year*365) + 365]

    if len(data) == 0:
        return None

    return sum(data) / len(data)
示例#8
0
    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
示例#9
0
    def done(self, form_list, **kwargs):
        if 'error_invalid_id' in self.storage.extra_data:
            self.storage.reset()
            set_notification('alert-error', 'Error Launching Simulation: Unknown/Invalid run provided. ' +
                                            'Please select a simulation below to launch.', self.request.session)
            return redirect("ts_emod_scenario_browse")

        if 'scenario_id' in self.storage.extra_data:
            scenario_id = int(self.storage.extra_data['scenario_id'])
        else:
            Exception()

        adapter = EMOD_Adapter(self.request.user.username)

        if 'emod_launch_run_id' in self.storage.extra_data:
            my_run_id = int(self.storage.extra_data['emod_launch_run_id'])
            my_run = adapter.fetch_runs(scenario_id=-1, run_id=int(my_run_id))
            # check for name changes from user
            start_data = self.get_cleaned_data_for_step('start')
            if self.storage.extra_data['old_run_name'] != start_data['name']:
                my_run.name = start_data['name']
                my_run.save()
        else:
            # get the scenario
            self.storage.extra_data['scenario'] = EMODBaseline.from_dw(pk=scenario_id)
            my_config = ast.literal_eval(self.storage.extra_data['scenario'].get_file_by_type('config').content)

            try:
                my_campaign = ast.literal_eval(self.storage.extra_data['scenario'].get_file_by_type('campaign').content)
            except AttributeError:
                # if file list was returned, use last file
                file_list = self.request.session['scenario'].get_file_by_type('campaign')
                my_campaign = ast.literal_eval(file_list[0].content)

            my_campaign_length = len(my_campaign['Events'])

            ###
            # create a new run instance

            if my_campaign_length > 0:
                with_interventions = "with interventions "
            else:
                with_interventions = ""

            my_simulation_duration = my_config['parameters']['Simulation_Duration']

            ## Set the run start_date based on location and config's start_time
            my_start_date = \
                datetime.datetime.strptime(
                    self.storage.extra_data['scenario'].template.climate_start_date,
                    '%Y-%m-%d').date() \
                + datetime.timedelta(days=my_config['parameters']['Start_Time'])

            # Initialize the run
            my_run = adapter.save_run(scenario_id=scenario_id,
                                      template_id=int(self.storage.extra_data['scenario'].template.id),
                                      start_date=my_start_date,
                                      duration=my_simulation_duration,
                                      name=self.get_cleaned_data_for_step('start')['name'],
                                      description='Launch Tool: ' + self.storage.extra_data['scenario'].name
                                                  + ': Run ' + with_interventions + str(datetime.datetime.now()),
                                      location_ndx=self.storage.extra_data['scenario'].template.location_key.id,
                                      timestep_interval=1,
                                      run_id=-1,
                                      as_object=True)

            my_run.baseline_key_id = scenario_id
            my_run.save()

            if settings.DEBUG:
                print "DEBUG: LaunchTool: run id: ", my_run.id

            # add in JCD for config file
            changes = []
            newdict = copy.deepcopy(my_config)

            newdict['config.json/parameters'] = newdict.pop('parameters')
            changes.append(Change.node(name='config.json', node=[newdict], mode='-'))

            # add in JCD for campaign file
            if my_campaign_length > 0:
                newdict = copy.deepcopy(my_campaign)
                newdict['campaign.json/Use_Defaults'] = newdict.pop('Use_Defaults')
                newdict['campaign.json/Events'] = newdict.pop('Events')
                changes.append(Change.node(name='campaign.json', node=[newdict], mode='-'))

            my_run.jcd = JCD.from_changes(changes)
            my_run.save()

        ### Launch the run

        # 5853 - hard-code to 1 for now
        #reps_per_exec = int(self.get_cleaned_data_for_step('start')['reps_per_exec'])
        reps_per_exec = 1

        # If this is a representative based scenario, then set it to non-editable
        representative_scenario = RepresentativeScenario.objects.filter(emod_scenario_id=scenario_id)
        if representative_scenario:
            representative_scenario[0].is_editable = False
            representative_scenario[0].save()

        # submit returns tuple (success, message)
        try:
            status = submit(my_run, user=self.request.user, manifest=True, reps_per_exec=reps_per_exec)

            set_notification('alert-success', '<strong>Success!</strong> Run launched.',
                             self.request.session)
        except (RuntimeError, TypeError, AssertionError), e:
            set_notification('alert-error', '<strong>Error!</strong> ' + str(e.message), self.request.session)
示例#10
0
    def process_step(self, form):
        """ Method to handle "after Next-click" processing of steps """

        # set the flag that determines when to save the config to the scenario
        change_config = 0
        # set the flag that determines when to save the scenario
        change_scenario = 0

        adapter = EMOD_Adapter(self.request.user.username)

        if self.steps.current == 'config':
            if self.request.session['scenario_config']['parameters']['Simulation_Duration'] != \
                    form.cleaned_data['Simulation_Duration']:
                self.request.session['scenario_config']['parameters']['Simulation_Duration'] = \
                    form.cleaned_data['Simulation_Duration']
                change_config = 1
            if self.request.session['scenario_config']['parameters']['Simulation_Type'] != \
                    form.cleaned_data['Simulation_Type']:
                self.request.session['scenario_config']['parameters']['Simulation_Type'] = \
                    form.cleaned_data['Simulation_Type']
                change_config = 1
            if self.request.session['scenario_config']['parameters']['Start_Time'] != form.cleaned_data['Start_Time']:
                self.request.session['scenario_config']['parameters']['Start_Time'] = form.cleaned_data['Start_Time']
                change_config = 1

            if self.request.session['scenario'].name != form.cleaned_data['name']:
                self.request.session['scenario'].name = form.cleaned_data['name']
                change_scenario = 1

            if self.request.session['scenario'].description != form.cleaned_data['description']:
                self.request.session['scenario'].description = form.cleaned_data['description']
                change_scenario = 1

            # Workaround for bug #5626 config file is not saved if user doesn't change Simulation Duration on
            # Step 2 of 9: Configure Simulation
            change_config = 1

            # 6230: redirect to Intervention Tool (if user clicked "Skip to Intervention Tool Button")
            # Set session variable here, so dispatch at beginning of next step knows to redirect (wizard won't allow
            #  redirect here to work.
            if 'jump_to_intervention_tool' in self.request.POST.keys():
                self.request.session['jump_to_intervention_tool'] = 1

        if self.steps.current == 'species':
            species_list = form.cleaned_data['species']
            # if the list NOT matches the old list, update it
            if species_list != self.request.session['scenario_config']['parameters']['Vector_Species_Names']:
                self.request.session['scenario_config']['parameters']['Vector_Species_Names'] = species_list
                change_config == 1

            # check also for changes in the Vector Parameters (vector list may be the same)
            my_json = dict(form.data)

            # Gather parameter sets for all species in names list.
            parameter_dict = {}
            for species in species_list:
                # add each Vector to Params list
                my_dict = {}
                for key in sorted(my_json.keys()):  # sorted so Habitat_Type/Required_Habitat_Factor order preserved
                    if str(species) + '__json_' in key:
                        if type(my_json[key]) == list:
                            my_value = my_json[key][0]
                        else:
                            my_value = my_json[key]
                        if type(my_value) not in (float, int):
                            try:
                                my_value = int(str(my_value))
                            except (ValueError, TypeError):
                                try:
                                    my_value = float(str(my_value))
                                except (ValueError, TypeError):
                                    pass

                        if 'Required_Habitat_Factor' in key.split('__json_')[1]:
                            if my_value == '':
                                continue
                            if 'Required_Habitat_Factor' in my_dict.keys():
                                my_dict['Required_Habitat_Factor'].append(my_value)
                            else:
                                my_dict.update({'Required_Habitat_Factor': [my_value]})
                        elif 'Habitat_Type' in key.split('__json_')[1]:
                            if 'Habitat_Type' in my_dict.keys():
                                my_dict['Habitat_Type'].append(my_value)
                            else:
                                my_dict.update({'Habitat_Type': [my_value]})
                        elif key.split('__json_')[1] != 'obj':
                            my_dict.update({key.split('__json_')[1]: my_value})
                parameter_dict.update({species: my_dict})
            if self.request.session['scenario_config']['parameters']['Vector_Species_Params'] != parameter_dict:
                self.request.session['scenario_config']['parameters']['Vector_Species_Params'] = parameter_dict
                change_config = 1

        if self.steps.current == 'parasite':
            my_fields = json.loads(form.cleaned_data['json'])
            for key in my_fields:
                my_value = my_fields[key]['value']
                if type(my_value) in (str, unicode):
                    try:
                        my_value = int(my_value)
                    except ValueError:
                        try:
                            my_value = float(my_value)
                        except ValueError:
                            pass

                if my_value != self.request.session['scenario_config']['parameters'][key]:
                    self.request.session['scenario_config']['parameters'][key] = my_value
                    change_config = 1

        if self.steps.current == 'scaling_factors':
            my_value = form.cleaned_data['x_Temporary_Larval_Habitat']
            if type(my_value) in (str, unicode):
                try:
                    my_value = int(my_value)
                except ValueError:
                    try:
                        my_value = float(my_value)
                    except ValueError:
                        pass
            if my_value != self.request.session['scenario_config']['parameters']['x_Temporary_Larval_Habitat']:
                self.request.session['scenario_config']['parameters']['x_Temporary_Larval_Habitat'] = my_value
                change_config = 1

        # populate new scenario with template data
        #  or with existing scenario data if this call is for Editing a scenario
        if (self.steps.current == 'location' and form.cleaned_data['template_id'])\
                and ('template_id' not in self.storage.extra_data.keys()
                     or self.storage.extra_data['template_id'] != form.cleaned_data['template_id']):
            # todo: some of this is either edit-mode or new-mode: sort out later (depending on availability of data)

            template_id = int(form.cleaned_data['template_id'])

            if 'template_id' in self.storage.extra_data \
                    and template_id != self.storage.extra_data['template_id']:
                pass  # location_change = 1
            else:
                # first time through wizard, not a location change
                # location_change = 0
                # save schema
                self.request.session['schema.json'] = ConfigData.objects.filter(type='schema',
                                                                                name='emod-v1.5-schema.txt')[0].json

                # Set name here with dummy name as adding a file will attempt to save the scenario,
                # which fails if name is null
                if self.request.session['scenario'].name is None:
                    self.request.session['scenario'].name = ''
                    self.request.session['scenario'].description = ''

                # check for campaign file...
                try:
                    # if it exists, do nothing
                    self.request.session['scenario'].get_file_by_type('campaign')
                except ObjectDoesNotExist:
                    # else: add in empty campaign file
                    self.request.session['scenario'].add_file_from_string('campaign',
                                                                          'campaign.json',
                                                                          str({"Events": []}),
                                                                          description=self.steps.current)

            my_template_obj = DimTemplate.objects.get(id=template_id)

            self.request.session['scenario'].model_version = my_template_obj.model_version
            self.request.session['scenario'].dimbaseline.model_version = my_template_obj.model_version
            self.request.session['scenario'].template_id = template_id
            self.request.session['scenario'].template = my_template_obj
            self.request.session['scenario'].save()

            location_id = my_template_obj.location_key_id

            self.request.session['scenario'].template_id = template_id
            self.request.session['scenario'].template = my_template_obj
            # needed for editing
            if self.request.session['scenario'].dimbaseline is not None:
                self.request.session['scenario'].dimbaseline.template = my_template_obj
                self.request.session['scenario'].dimbaseline.template_id = template_id
            change_scenario = 1

            # Populate the step data with the chosen location's data
            self.storage.extra_data.update({'template_id': template_id})

            # populate config to the session for multiple uses later
            template_config = ast.literal_eval(my_template_obj.get_file_content('config.json'))
            if 'edit_scenario' in self.storage.extra_data.keys() \
                    and 'config' not in self.request.session['scenario'].get_missing_files():
                # use scenario's config file
                try:
                    self.request.session['scenario_config'] = \
                        ast.literal_eval(self.request.session['scenario'].get_file_by_type('config').content)
                except AttributeError:
                    # if query set returned, use first file in "list"
                    file_list = self.request.session['scenario'].get_file_by_type('config')
                    self.request.session['scenario_config'] = \
                        ast.literal_eval(file_list[0].content)
            else:
                # use template's (or parent scenario's) config file
                self.request.session['scenario_config'] = copy.deepcopy(template_config)
            self.storage.extra_data.update({'scenario_template_config': template_config})

            change_config = 1

            #########################
            ### Add default files to scenario

            # todo: stop using template zips, when climate files are available elsewhere
            print my_template_obj.climate_url

            self.storage.extra_data.update({'location_zip_link': my_template_obj.climate_url})

            # Do we cache zip to temp dir first time, and always access them from there (if they exist)
            # FILE_UPLOAD_TEMP_DIR
            # setup work directory
            #my_directory = os.path.join(settings.MEDIA_ROOT, 'uploads/expert_emod/')
            #my_directory = os.path.join(FILE_UPLOAD_TEMP_DIR, 'ts_emod/')
            #if not os.path.exists(my_directory):
            #    os.makedirs(my_directory)

            #inStream = urllib2.urlopen('http://dl-vecnet.crc.nd.edu/downloads/wh246s153')
            #inStream = urllib2.urlopen('https://dl.vecnet.org/downloads/wh246s153')
            if 'location_zip_link' in self.storage.extra_data:
                if settings.DEBUG is True:
                    now = datetime.datetime.now()
                    print now, " DEBUG: getting zip file: ", self.storage.extra_data['location_zip_link']
                url = urlopen(self.storage.extra_data['location_zip_link'])
                zipfile = ZipFile(StringIO(url.read()))

                for zip_file_name in zipfile.namelist():
                    if settings.DEBUG is True:
                        now = datetime.datetime.now()
                        print now, " DEBUG: handling file: ", zip_file_name
                    my_file = zipfile.open(zip_file_name)
                    my_name = ntpath.basename(zip_file_name)

                    # determine which file type this is
                    # my_name contains 'demographics' ?  (use compiled, store both?) Honiara_demographics.static.compiled.json Honiara_demographics.json
                    """
                    'air binary',
                    'air json',
                    'humidity binary',
                    'humidity json',
                    'land_temp binary',
                    'land_temp json',
                    'rainfall binary',
                    'rainfall json',

                    Honiara_demographics.compiled.json
                    Honiara_demographics.json
                    Honiara_demographics.static.compiled.json
                    Honiara_demographics.static.json
                    Honiara_humidity_daily10y.bin
                    Honiara_humidity_daily10y.bin.json
                    Kenya_Nyanza_2.5arcminhumid_1365118879_climateheader.json
                    Honiara_rainfall_daily10y.bin
                    Honiara_rainfall_daily10y.bin.json
                    Honiara_temperature_daily10y.bin
                    Honiara_temperature_daily10y.bin.json
                    """
                    my_type = None
                    if '.md5' in my_name:
                        continue
                    elif 'demographics' in my_name or 'Demographics' in my_name:
                        if 'compiled' in my_name:
                            # use in scenario
                            my_type = 'demographics'
                        else:
                            # save to storage (to allow user to see contents)
                            self.storage.extra_data['demographics'] = my_file
                    elif '.json' in my_name and '.json.bin' not in my_name:
                        # 'json' files
                        if 'humid' in my_name:
                            my_type = 'humidity json'
                        elif 'rain' in my_name:
                            my_type = 'rainfall json'
                        elif 'temp' in my_name or 'tmean' in my_name:
                            my_type = 'air json'
                    else:
                        # must be a binary file
                        if 'humid' in my_name:
                            my_type = 'humidity binary'
                        elif 'rain' in my_name:
                            my_type = 'rainfall binary'
                        elif 'temp' in my_name or 'tmean' in my_name:
                            my_type = 'air binary'

                    if my_type is not None:
                        if 'binary' in my_type:
                            self.request.session['scenario'].add_file_from_string(my_type, my_name,
                                                                                  my_file.read(),
                                                                                  description=self.steps.current)
                        else:
                            self.request.session['scenario'].add_file_from_string(my_type, my_name,
                                                                                  my_file.read(),
                                                                                  description=self.steps.current)
                        change_scenario = 1

                        # todo: fix to allow separate air and land temp files
                        # for now: use same files for air temp and land temp
                        if my_type in ('air binary', 'air json'):
                            my_type = my_type.replace('air', 'land_temp')
                            self.request.session['scenario'].add_file_from_string(my_type, my_name,
                                                                                  "no land temp file",
                                                                                  description=self.steps.current)
            ### END if zip file is none

            # could save here to free up memory from the object file storage
            # but it fails without a name
            #if change_scenario == 1:
            #    self.request.session['scenario'].save()
            ###  END Zip/Input File handling
            ######################

            # config step data:
            self.storage.set_step_data('config', {u'config-name': [self.request.session['scenario']._name],
                                                  u'config-description': [self.request.session['scenario'].description],
                                                  u'config-Start_Time': [self.request.session['scenario_config']['parameters']['Start_Time']],
                                                  u'config-Simulation_Duration':
                                                  [self.request.session['scenario_config']['parameters']['Simulation_Duration']],
                                                  u'config-Simulation_Type':
                                                  [self.request.session['scenario_config']['parameters']['Simulation_Type']],
                                                  })

            # climate step data:
            self.storage.set_step_data('climate', {u'climate-location': [location_id]})

            # demographic step data:
            self.storage.set_step_data('demographic', {u'demographic-location': [location_id]})

            # parasite
            for step_name in ["vector", "parasite"]:
                # get db config for the step
                my_json = ConfigData.objects.filter(type='JSONConfig', name=step_name)[0].json

                # wizard values based on config + template values
                my_wiz = {u'orig_json_obj': [my_json]}
                my_json = json.loads(my_json)
                for key in my_json.keys():
                    try:
                        # = scenario value OR template value
                        # change into wizard format step name + -json_ + key name
                        my_wiz.update({u'' + step_name + '-json_'
                                       + key: [u'' + str(self.request.session['scenario_config']['parameters'][key])]})
                    except KeyError:
                        pass

                # insert into wizard storage
                self.storage.set_step_data(step_name, my_wiz)

            # Scaling step data:
            self.storage.set_step_data('scaling_factors',
                                       {u'scaling_factors-x_Temporary_Larval_Habitat':
                                       [self.request.session['scenario_config']['parameters']['x_Temporary_Larval_Habitat']]})

        # save any changes made to the scenario config file
        if change_config == 1 and self.steps.current != 'location':
            self.request.session['scenario'].add_file_from_string('config', 'config.json',
                                                                  str(self.request.session['scenario_config']),
                                                                  description=self.steps.current)
            change_scenario = 1

        if change_scenario == 1 and self.steps.current != 'location':
            # don't save until config step (name and description will be populated there.

            try:
                my_id, completed = self.request.session['scenario'].save()
                set_notification('alert-success', 'The simulation was saved. ', self.request.session)
            except KeyError:
                set_notification('alert-error', 'Error: The simulation was not saved. ', self.request.session)

        ###########################
        ### Create a run with scenario settings
        #    - launch it
        #    - display status
        #  ??? Do we want to force/allow user to add name/description to these runs?
        #  ??? Cap duration for initial run? 5 years?
        if self.steps.current == 'run':
            # create a new run instance
            adapter = EMOD_Adapter(self.request.user.username)

            my_template = DimTemplate.objects.get(id=self.request.session['scenario'].template.id)

            my_start_date = \
                datetime.datetime.strptime(
                    my_template.climate_start_date,
                    '%Y-%m-%d').date() \
                + datetime.timedelta(days=self.request.session['scenario_config']['parameters']['Start_Time'])

            my_run = adapter.save_run(
                                      template_id=int(self.request.session['scenario'].template.id),
                                      start_date=my_start_date,
                                      duration=100,  # 1825, 5 years
                                      #=self.request.session['scenario_config']['parameters']['Simulation_Duration'],
                                      name=form.cleaned_data['name'],  # self.request.session['scenario'].name + ': Test Run '
                                      description='Run by New Simulation Wizard',
                                      location_ndx=my_template.location_key.id,
                                      timestep_interval=1,
                                      run_id=-1,  # run_id,
                                      as_object=True)

            my_run.scenario_key_id = self.request.session['scenario'].id
            my_run.save()

            if settings.DEBUG:
                print "DEBUG: line 890 BaselineWiz: run id: ", my_run.id

            # add in JCD for it for config file
            changes = []
            newdict = copy.deepcopy(self.request.session['scenario_config'])

            newdict['config.json/parameters'] = newdict.pop('parameters')
            changes.append(Change.node(name='config.json', node=[newdict], mode='-'))
            my_run.jcd = JCD.from_changes(changes)

            my_run.save()

            ### Launch the run

            # save launch info to extra data todo: this should eventually go into success block below
            self.storage.extra_data['last_launched_run'] = my_run.id

            # submit returns tuple (success, message)
            status = submit(my_run, user=self.request.user, manifest=True, reps_per_exec=1)

            try:
                if status[0] is True:
                    set_notification('alert-success', '<strong>Success!</strong> Test Run launched.',
                                     self.request.session)
                else:
                    set_notification('alert-error', '<strong>Error!</strong> ' + status[1], self.request.session)
            except TypeError:
                pass

        return self.get_form_step_data(form)
示例#11
0
    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
示例#12
0
    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
    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(ScenarioCreateByTemplate, self).get_context_data(form=form, **kwargs)

        context["nav_button"] = "ts_ScenarioCreateByTemplate_step"  # set flag to activate nav menu highlighting

        ######### 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

            if "folder_id" in kwargs.keys():
                context["current_folder"] = kwargs["folder_id"]

            # For now: limit list to Solomon and Kenya templates
            templates_a = {}
            templates_b = {}
            templates_c = {}
            templates_d = {}
            templates_e = {}
            templates_garki = {}
            templates_new = {}

            # 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]})
            for key in templates.keys():
                if "New Location" in templates[key]["name"]:
                    templates_new.update({key: templates[key]})
            for key in templates.keys():
                if "Garki" in templates[key]["name"]:
                    templates_garki.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

            # scenario_count = scenarios.count()
            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["templates_new"] = templates_new
            context["templates_garki"] = templates_garki
            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

        return context
示例#14
0
    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, **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