Пример #1
0
 def to_db(self, user, reduction_id=None, config_id=None):
     """
         Save reduction properties to DB.
         If we supply a config_id, the properties from that
         configuration will take precedence.
         If no config_id is supplied and the reduction_id 
         provided is found to be associated to a configuration,
         make a new copy of the reduction object so that we
         don't corrupt the configured reduction.
         @param user: User object
         @param reduction_id: pk of the ReductionProcess entry
         @param config_id: pk of the ReductionConfiguration entry
     """
     if not self.is_valid():
         raise RuntimeError, "Reduction options form invalid"
     
     if reduction_id is None:
         reduction_id = self.cleaned_data['reduction_id']
         
     # Find or create a reduction process entry and update it
     if reduction_id is not None:
         reduction_proc = get_object_or_404(ReductionProcess, pk=reduction_id, owner=user)
         # If the user changed the data to be reduced, create a new reduction process entry
         new_reduction = not reduction_proc.data_file==self.cleaned_data['data_file']
         # If the reduction process is configured and the config isn't the provided one
         config_obj = reduction_proc.get_config()
         new_reduction = new_reduction or (config_obj is not None and not config_obj.id == config_id)
     else:
         new_reduction = True
         
     if new_reduction:
         eqsans = Instrument.objects.get(name=INSTRUMENT_NAME)
         reduction_proc = ReductionProcess(owner=user,
                                           instrument=eqsans)
     reduction_proc.name = self.cleaned_data['reduction_name']
     reduction_proc.data_file = self.cleaned_data['data_file']
     reduction_proc.save()
     
     # Set the parameters associated with the reduction process entry
     config_property_dict = {}
     property_dict = copy.deepcopy(self.cleaned_data)
     property_dict['reduction_id'] = reduction_proc.id
     if config_id is not None:
         reduction_config = get_object_or_404(ReductionConfiguration, pk=config_id, owner=user)
         if reduction_proc not in reduction_config.reductions.all():
             reduction_config.reductions.add(reduction_proc)
         config_property_dict = json.loads(reduction_config.properties)
         property_dict.update(config_property_dict)
         reduction_proc.name = reduction_config.name
         reduction_proc.save()
         for item in reduction_config.experiments.all():
             if item not in reduction_proc.experiments.all():
                 reduction_proc.experiments.add(item)
     try:
         properties = json.dumps(property_dict)
         reduction_proc.properties = properties
         reduction_proc.save()
     except:
         logger.error("Could not process reduction properties: %s" % sys.exc_value)
     
     # Find experiment
     process_experiment(reduction_proc, self.cleaned_data['experiment'], instrument_name=INSTRUMENT_NAME)
             
     return reduction_proc.pk