From 8221d123915d1a934db80fa5d051cf5ce4cf4519 Mon Sep 17 00:00:00 2001 From: luissian Date: Sat, 28 Sep 2024 11:11:30 +0200 Subject: [PATCH] Solved issue #310 Key error for a sample with no molecule extraction parameters are requested --- core/models.py | 15 ++++- core/utils/samples.py | 56 ++++++++++--------- .../templates/wetlab/handling_molecules.html | 20 +++---- wetlab/urls.py | 5 -- wetlab/views.py | 15 ++++- 5 files changed, 65 insertions(+), 46 deletions(-) diff --git a/core/models.py b/core/models.py index 5c51efb64..742c19170 100644 --- a/core/models.py +++ b/core/models.py @@ -1482,6 +1482,7 @@ def get_massive(self): class MoleculePreparationManager(models.Manager): def create_molecule(self, molecule_data): + req_upd_sample_state = False molecule_used_obj = MoleculeType.objects.filter( molecule_type__exact=molecule_data["molecule_type"] ).last() @@ -1492,17 +1493,27 @@ def create_molecule(self, molecule_data): protocol_used_obj = Protocols.objects.filter( name__exact=molecule_data["protocol_used"], type__exact=protocol_type_obj ).last() + if ProtocolParameters.objects.filter(protocol_id=protocol_used_obj).exists(): + m_state = StatesForMolecule.objects.get( + molecule_state_name__exact="defined" + ) + else: + m_state = StatesForMolecule.objects.get( + molecule_state_name__exact="assigned_parameters" + ) + req_upd_sample_state = True new_molecule = self.create( protocol_used=protocol_used_obj, sample=molecule_data["sample"], molecule_type=molecule_used_obj, - state=StatesForMolecule.objects.get(molecule_state_name__exact="defined"), + state=m_state, molecule_code_id=molecule_data["molecule_code_id"], molecule_extraction_date=molecule_data["molecule_extraction_date"], extraction_type=molecule_data["extraction_type"], molecule_user=User.objects.get(username__exact=molecule_data["user"]), ) - + if req_upd_sample_state is True: + new_molecule.sample.set_state("Pending for use") return new_molecule diff --git a/core/utils/samples.py b/core/utils/samples.py index 3c67d8a2d..6fbf59ff1 100644 --- a/core/utils/samples.py +++ b/core/utils/samples.py @@ -1293,32 +1293,37 @@ def get_molecule_protocols(apps_name): def get_molecule_data_and_protocol_parameters(protocol_objs): mol_data_parm = {} for protocol_obj, mol_ids in protocol_objs.items(): - prot_name = protocol_obj.get_name() - mol_data_parm[prot_name] = {} - mol_data_parm[prot_name]["params_type"] = ( - core.utils.protocols.get_protocol_parameters_and_type(protocol_obj) - ) - mol_data_parm[prot_name][ - "fix_heading" - ] = core.core_config.HEADING_FOR_MOLECULE_ADDING_PARAMETERS - mol_data_parm[prot_name]["lot_kit"] = ( - core.utils.commercial_kits.get_lot_commercial_kits(protocol_obj) - ) - mol_data_parm[prot_name]["param_heading"] = [] - prot_params = core.models.ProtocolParameters.objects.filter( + # check if the protocol has parameters + if core.models.ProtocolParameters.objects.filter( protocol_id=protocol_obj, parameter_used=True - ).order_by("parameter_order") - for param in prot_params: - mol_data_parm[prot_name]["param_heading"].append(param.get_parameter_name()) - mol_data_parm[prot_name]["param_heading_in_string"] = ";".join( - mol_data_parm[prot_name]["param_heading"] - ) - mol_data_parm[prot_name]["m_data"] = list( - core.models.MoleculePreparation.objects.filter(pk__in=mol_ids).values_list( - "pk", "sample__sample_name", "molecule_code_id" + ).exists(): + prot_name = protocol_obj.get_name() + mol_data_parm[prot_name] = {} + mol_data_parm[prot_name]["params_type"] = ( + core.utils.protocols.get_protocol_parameters_and_type(protocol_obj) + ) + mol_data_parm[prot_name][ + "fix_heading" + ] = core.core_config.HEADING_FOR_MOLECULE_ADDING_PARAMETERS + mol_data_parm[prot_name]["lot_kit"] = ( + core.utils.commercial_kits.get_lot_commercial_kits(protocol_obj) + ) + mol_data_parm[prot_name]["param_heading"] = [] + prot_params = core.models.ProtocolParameters.objects.filter( + protocol_id=protocol_obj, parameter_used=True + ).order_by("parameter_order") + for param in prot_params: + mol_data_parm[prot_name]["param_heading"].append( + param.get_parameter_name() + ) + mol_data_parm[prot_name]["param_heading_in_string"] = ";".join( + mol_data_parm[prot_name]["param_heading"] + ) + mol_data_parm[prot_name]["m_data"] = list( + core.models.MoleculePreparation.objects.filter( + pk__in=mol_ids + ).values_list("pk", "sample__sample_name", "molecule_code_id") ) - ) - return mol_data_parm @@ -1834,7 +1839,7 @@ def record_molecule_use(from_data, app_name): return molecule_use_information -def record_molecules(samples, excel_data, heading, user, app_name): +def record_extract_protocol(samples, excel_data, heading, user, app_name): """Recored the molecues defined in excel_data. If information is missing returns the data to display again for correcting. @@ -1898,7 +1903,6 @@ def record_molecules(samples, excel_data, heading, user, app_name): molecule_data["molecule_code_id"] = code_split.group(1) + str(number_code) else: molecule_data["molecule_code_id"] = sample_obj.get_sample_code() + "_E1" - molecule_obj = core.models.MoleculePreparation.objects.create_molecule( molecule_data ) diff --git a/wetlab/templates/wetlab/handling_molecules.html b/wetlab/templates/wetlab/handling_molecules.html index 89f5270d7..9103d258a 100644 --- a/wetlab/templates/wetlab/handling_molecules.html +++ b/wetlab/templates/wetlab/handling_molecules.html @@ -38,10 +38,10 @@

Assign the Molecule Protocol to following samples.

+ name="updateExtractionProtocol" + id="updateExtractionProtocol"> {% csrf_token %} - +
Information missing

Please add the missing information and click on the submit bottom

{% csrf_token %} - + @@ -559,7 +559,7 @@

Not molecule uses have been defined yet

}); $(document).ready(function () { - $("#updateMoleculeProtocol").submit(function (e) { + $("#updateExtractionProtocol").submit(function (e) { //stop submitting the form to see the disabled button effect // e.preventDefault(); //disable the submit button @@ -568,7 +568,7 @@

Not molecule uses have been defined yet

$("").attr("type", "hidden") .attr("name", "molecule_data") .attr("value", data_json) - .appendTo("#updateMoleculeProtocol"); + .appendTo("#updateExtractionProtocol"); $("#btnSubmit").attr("disabled", true); return true; }); @@ -622,13 +622,13 @@

Not molecule uses have been defined yet

} }); $(document).ready(function () { - $("#updateMoleculeProtocol").submit(function (e) { + $("#updateExtractionProtocol").submit(function (e) { var table_data = $('#missing_data').jexcel('getData') var data_json = JSON.stringify(table_data) $("").attr("type", "hidden") .attr("name", "molecule_data") .attr("value", data_json) - .appendTo("#updateMoleculeProtocol"); + .appendTo("#updateExtractionProtocol"); $("#btnSubmit").attr("disabled", true); return true; }); diff --git a/wetlab/urls.py b/wetlab/urls.py index dcd257e29..f2b820df8 100644 --- a/wetlab/urls.py +++ b/wetlab/urls.py @@ -203,11 +203,6 @@ wetlab.views.sequencer_inventory, name="sequencer_inventory", ), - path( - "setMoleculeValues", - wetlab.views.set_molecule_values, - name="set_molecule_values", - ), path( "sequencerConfiguration", wetlab.views.sequencer_configuration, diff --git a/wetlab/views.py b/wetlab/views.py index 49413b818..589f48a65 100644 --- a/wetlab/views.py +++ b/wetlab/views.py @@ -3244,7 +3244,8 @@ def handling_molecules(request): ) elif ( - request.method == "POST" and request.POST["action"] == "updateMoleculeProtocol" + request.method == "POST" + and request.POST["action"] == "updateExtractionProtocol" ): heading = core.core_config.HEADING_FOR_MOLECULE_PROTOCOL_DEFINITION.copy() heading.append("s_id") @@ -3253,7 +3254,7 @@ def handling_molecules(request): ) if len(samples) == 0: return redirect("handling_molecules") - molecule_recorded = core.utils.samples.record_molecules( + molecule_recorded = core.utils.samples.record_extract_protocol( samples, excel_data, heading, request.user, __package__ ) if "incomplete" in molecule_recorded: @@ -3268,6 +3269,12 @@ def handling_molecules(request): molecule_recorded ) ) + if len(molecule_parameters) == 0: + return render( + request, + "wetlab/handling_molecules.html", + {"molecule_parameters_updated": True}, + ) protocol_list = ";".join(list(molecule_parameters.keys())) return render( request, @@ -3587,6 +3594,7 @@ def search_sample(request): ) +""" @login_required def set_molecule_values(request): if request.method == "POST" and request.POST["action"] == "continueWithMolecule": @@ -3622,7 +3630,7 @@ def set_molecule_values(request): elif ( request.method == "POST" and request.POST["action"] == "updateMoleculeProtocol" ): - molecule_recorded = core.utils.samples.record_molecules(request) + molecule_recorded = core.utils.samples.record_extract_protocol(request) if "heading" not in molecule_recorded: samples = request.POST["samples"].split(",") @@ -3707,6 +3715,7 @@ def set_molecule_values(request): {"display_list": display_list}, ) return render(request, "wetlab/setMoleculeValues.html", {}) + """ @login_required