Example #1
0
    def save_new_scenario_data(self, request):
        # Create initial scenario
        # Create initial scenario
        # Create initial scenario
        adapter = EMOD_Adapter(request.user.username)

        emod_scenario = EMODBaseline(
            name='',
            description='Made with representative workflow',
            model=DimModel.objects.get(model='EMOD'),
            user=DimUser.objects.get_or_create(username=request.user.username)[0]
        )

        template_object = DimTemplate.objects.get(template_name="Representative Location")

        emod_scenario.model_version = template_object.model_version
        emod_scenario.template_id = 21
        emod_scenario.template = template_object

        # Create and add a config file
        config_json = json.loads(template_object.get_file_content('config.json'))
        emod_scenario.add_file_from_string('config', 'config.json', json.dumps(config_json), description='SOMETHING')

        # populate campaign
        try:
            campaign = json.loads(template_object.get_file_content('campaign.json'))
        except KeyError:
            # use empty campaign file
            campaign = json.loads({"Events": []})

        # Add the emod_scenario campaign file
        emod_scenario.add_file_from_string('campaign', 'campaign.json', json.dumps(campaign), description='SOMETHING')

        emod_scenario.save()
        # Create initial scenario
        # Create initial scenario
        # Create initial scenario

        # Hack until I know how to retrieve files from DimBaseline without funneling it through EMODBaseline
        dim_scenario = DimBaseline.objects.get(id=emod_scenario.id)

        # Add metadata
        current_metadata = {}
        current_metadata['representative'] = {}

        # Fill data
        current_metadata['representative']['steps_complete'] = step_number + 1
        current_metadata['representative']['is_complete'] = False
        current_metadata['representative']['is_editable'] = True
        dim_scenario.metadata = json.dumps(current_metadata)

        dim_scenario.save()

        print "Scenario ID = " + str(emod_scenario.id)

        return emod_scenario.id
    def done(self, form_list, **kwargs):
        """A method that processes all the form data at the end of Wizard

        Save collected data from multiple forms into one DB table (Baseline)
        """

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

        # populate new scenario with template data
        template_id = self.storage.get_step_data("location")["location-template_id"]
        my_name = self.storage.get_step_data("location")["location-name"]
        my_description = self.storage.get_step_data("location")["location-description"]

        # create a new scenario
        my_scenario = EMODBaseline(
            name=my_name,
            description=my_description,
            model=DimModel.objects.get(model="EMOD"),
            user=DimUser.objects.get_or_create(username=self.request.user.username)[0],
        )

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

        my_scenario.model_version = my_template_obj.model_version
        my_scenario.template_id = template_id
        my_scenario.template = my_template_obj

        my_scenario.save()

        # populate config
        template_config = ast.literal_eval(my_template_obj.get_file_content("config.json"))
        my_scenario_config = copy.deepcopy(template_config)
        # save the scenario config file
        my_scenario.add_file_from_string(
            "config", "config.json", str(my_scenario_config), description=self.steps.current
        )

        # populate campaign
        try:
            template_campaign = ast.literal_eval(my_template_obj.get_file_content("campaign.json"))
            my_campaign = str(copy.deepcopy(template_campaign))
        except KeyError:
            # use empty campaign file
            my_campaign = str({"Events": []})

        # save the scenario config file
        my_scenario.add_file_from_string("campaign", "campaign.json", my_campaign, description=self.steps.current)

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

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

        my_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 settings.DEBUG is True:
            now = datetime.datetime.now()
            print now, " DEBUG: getting zip file: ", my_location_zip_link
        url = urlopen(my_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:
                    my_scenario.add_file_from_string(my_type, my_name, my_file.read(), description=self.steps.current)
                else:
                    my_scenario.add_file_from_string(my_type, my_name, my_file.read(), description=self.steps.current)

                # 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")
                    my_scenario.add_file_from_string(
                        my_type, my_name, "no land temp file", description=self.steps.current
                    )
        ###  END Zip/Input File handling
        ######################

        try:
            my_id, completed = my_scenario.save()
            set_notification("alert-success", "The simulation was saved. ", self.request.session)

            # todo: add folder stuff to adapter/EMODBaseline (instead, for now get/save object)
            # #6574 - add the folder that owns this scenario
            if self.storage.get_step_data("location")["folder_id"] != "":
                # see todo: my_scenario.folder = int(self.storage.get_step_data('location')['folder_id'])
                temp = DimBaseline.objects.get(pk=my_scenario.id)
                temp.folder = Folder.objects.get(pk=int(self.storage.get_step_data("location")["folder_id"]))
                temp.save()

        except KeyError:
            set_notification("alert-error", "Error: The simulation was not saved. ", self.request.session)

        # check if user wants to launch, or just save
        if (
            "edit" in self.storage.data["step_data"]["location"]
            and self.storage.data["step_data"]["location"]["edit"][0] == "1"
        ):
            edit_flag = True
        else:
            edit_flag = False

        # clear out saved form data.
        self.storage.reset()

        if edit_flag:
            return redirect("ts_edit_step", step="config", scenario_id=str(my_id))
        else:
            return redirect("ts_emod_scenario_details", scenario_id=my_id)