def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) exp = context['object'] # dict of detail field names to their value detail_data = {} q1, q2, q3 = get_action_parameter_querysets(exp.uuid) mat_q = get_material_querysets(exp.uuid) edocs = Edocument.objects.filter(ref_edocument_uuid=exp.uuid) detail_data = { row.object_description: row.inventory_material for row in mat_q } detail_data.update({ f'{row.object_description} {row.parameter_def_description}': f'{row.parameter_value}' for row in q1 }) detail_data.update({ f'{row.object_description} {row.parameter_def_description}': f'{row.parameter_value}' for row in q2 }) detail_data.update({ f'{row.object_description} {row.parameter_def_description}': f'{row.parameter_value}' for row in q3 }) link_data = { f'{lsr_edoc.title} download link': self.request.build_absolute_uri( reverse('edoc_download', args=[lsr_edoc.pk])) for lsr_edoc in edocs } # get notes notes_raw = Note.objects.filter(note_x_note__ref_note=exp.pk) notes = [] for note in notes_raw: notes.append('-' + note.notetext) context['Notes'] = notes # get tags tags_raw = Tag.objects.filter(pk__in=TagAssign.objects.filter( ref_tag=exp.pk).values_list('tag', flat=True)) tags = [] for tag in tags_raw: tags.append(tag.display_text.strip()) detail_data['Tags'] = ', '.join(tags) context['title'] = self.model_name.replace('_', " ").capitalize() context['update_url'] = reverse_lazy(f'{self.model_name}_update', kwargs={'pk': exp.pk}) context['detail_data'] = detail_data context['file_download_links'] = link_data return context
def list(self, request, *args, **kwargs): q1, q2, q3 = get_action_parameter_querysets( kwargs['parent_lookup_uuid']) q1_mat = get_material_querysets(kwargs['parent_lookup_uuid']) exp_params1 = [{ 'nominal_value': row.parameter_value, 'actual_value': row.parameter_value, 'object_description': f'{row.object_description}', 'parameter_def_description': f'{row.parameter_def_description}' } for row in q1] exp_params2 = [{ 'nominal_value': param, 'actual_value': param, 'object_description': f'{row.object_description}', 'parameter_def_description': f'{row.parameter_def_description}' } for row in q2 for param in row.parameter_value] exp_params3 = [{ 'nominal_value': row.parameter_value, 'actual_value': row.parameter_value, 'object_description': f'{row.object_description}', 'parameter_def_description': f'{row.parameter_def_description}' } for row in q3] results = { 'experiment_parameters_1': exp_params1, 'experiment_parameters_2': exp_params2, 'experiment_parameters_3': exp_params3 } mat_params = [{ 'material_name': row.object_description, 'value': request.build_absolute_uri( reverse('inventorymaterial-detail', args=[row.inventory_material.uuid])) } for row in q1_mat] #mat_params = [{'material_name': row.object_description , 'value': row.object_uuid} for row in q1_mat] #mat_params = {row.object_description:row.object_uuid for row in q1_mat} results.update({ 'material_parameters': mat_params, 'experiment_name': '' }) serializer = ExperimentDetailSerializer(results) return Response(serializer.data)
def get_material_forms(self, exp_uuid, context): q1 = get_material_querysets(exp_uuid) # context['q1_formset'] = self.ParameterFormSet(initial=[{'value': row.parameter_value} for row in q1]) initial_q1 = [{ 'value': row.inventory_material, 'uuid': json.dumps([f'{row.object_description}']) } for row in q1] q1_details = [f'{row.object_description}' for row in q1] form_kwargs = {'org_uuid': self.request.session['current_org_id']} context['q1_material_formset'] = self.MaterialFormSet( initial=initial_q1, prefix='q1_material', form_kwargs=form_kwargs) context['q1_material_details'] = q1_details return context
def create(self, request, *args, **kwargs): template_uuid = kwargs['parent_lookup_uuid'] exp_template = Experiment.objects.get(pk=template_uuid) template_name = exp_template.description exp_name = request.data['experiment_name'] experiment_copy_uuid = experiment_copy(template_uuid, exp_name) q1, q2, q3 = get_action_parameter_querysets(experiment_copy_uuid) q1_mat = get_material_querysets(experiment_copy_uuid) self.save_material_params(q1_mat, request.data['material_parameters']) self.save_params( q1, request.data['experiment_parameters_1'], { 'parameter_val_actual': 'actual_value', 'parameter_val_nominal': 'nominal_value' }) self.save_params(q2, request.data['experiment_parameters_2'], None) if template_name in SUPPORTED_CREATE_WFS: if template_name == 'liquid_solid_extraction': lsr_edoc = Edocument.objects.get( ref_edocument_uuid=exp_template.uuid, title='LSR file') new_lsr_pk, lsr_msg = liquid_solid_extraction( data, q3, experiment_copy_uuid, lsr_edoc, exp_name) elif template_name == 'resin_weighing': lsr_edoc = Edocument.objects.get( ref_edocument_uuid=exp_template.uuid, title='LSR file') new_lsr_pk, lsr_msg = resin_weighing(experiment_copy_uuid, lsr_edoc, exp_name) elif template_name == 'perovskite_demo': new_lsr_pk, lsr_msg = perovskite_demo(data, q3, experiment_copy_uuid, exp_name) return Response({ 'experiment_detail': request.build_absolute_uri( reverse('experiment-detail', args=[experiment_copy_uuid])), 'generated_file': request.build_absolute_uri( reverse('edoc_download', args=[new_lsr_pk])) })
def process_formsets(self, request, context): """Creates formsets and gets data from the post request. Args: request ([Django Request]): Should be the POST request context (dict): Context dictionary Returns: context [dict]: Context dict, returned to the page """ # get the experiment template uuid and name exp_template = Experiment.objects.get( pk=request.session['experiment_template_uuid']) template_name = exp_template.description # construct all formsets exp_name_form = ExperimentNameForm(request.POST) q1_formset = self.NominalActualFormSet(request.POST, prefix='q1_param') q2_formset = self.NominalActualFormSet(request.POST, prefix='q2_param') q3_formset = self.NominalActualFormSet(request.POST, prefix='q3_param') q1_material_formset = self.MaterialFormSet( request.POST, prefix='q1_material', form_kwargs={'org_uuid': self.request.session['current_org_id']}) if all([ exp_name_form.is_valid(), q1_formset.is_valid(), q2_formset.is_valid(), q3_formset.is_valid(), q1_material_formset.is_valid() ]): exp_name = exp_name_form.cleaned_data['exp_name'] # make the experiment copy: this will be our new experiment experiment_copy_uuid = experiment_copy(str(exp_template.uuid), exp_name) # get the elements of the new experiment that we need to update with the form values q1, q2, q3 = get_action_parameter_querysets(experiment_copy_uuid) q1_material = get_material_querysets(experiment_copy_uuid) self.save_forms( q1, q1_formset, { 'parameter_val_nominal': 'value', 'parameter_val_actual': 'actual_value' }) self.save_forms(q1_material, q1_material_formset, {'inventory_material': 'value'}) self.save_forms(q2, q2_formset, None) # begin: template-specific logic if template_name in SUPPORTED_CREATE_WFS: #if any([f.has_changed() for f in q3_formset]): data = {} # Stick form data into this dict for i, form in enumerate(q3_formset): if form.is_valid(): query = q3[i] data[query. parameter_def_description] = form.cleaned_data[ 'value'].value if template_name == 'liquid_solid_extraction': lsr_edoc = Edocument.objects.get( ref_edocument_uuid=exp_template.uuid, title='LSR file') new_lsr_pk, lsr_msg = liquid_solid_extraction( data, q3, experiment_copy_uuid, lsr_edoc, exp_name) elif template_name == 'resin_weighing': lsr_edoc = Edocument.objects.get( ref_edocument_uuid=exp_template.uuid, title='LSR file') new_lsr_pk, lsr_msg = resin_weighing( experiment_copy_uuid, lsr_edoc, exp_name) elif template_name == 'perovskite_demo': new_lsr_pk, lsr_msg = perovskite_demo( data, q3, experiment_copy_uuid, exp_name) # handle library studio file if relevant if new_lsr_pk is not None: context['lsr_download_link'] = reverse('edoc_download', args=[new_lsr_pk]) else: messages.error( request, f'LSRGenerator failed with message: "{lsr_msg}"') context['experiment_link'] = reverse( 'experiment_view', args=[experiment_copy_uuid]) context['new_exp_name'] = exp_name return context