Skip to content

Commit

Permalink
Solved issue BU-ISCIII#310 Key error for a sample with no molecule ex…
Browse files Browse the repository at this point in the history
…traction parameters are requested
  • Loading branch information
luissian committed Sep 28, 2024
1 parent b536145 commit 8221d12
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 46 deletions.
15 changes: 13 additions & 2 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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


Expand Down
56 changes: 30 additions & 26 deletions core/utils/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
)
Expand Down
20 changes: 10 additions & 10 deletions wetlab/templates/wetlab/handling_molecules.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ <h3>Assign the Molecule Protocol to following samples.</h3>
<form method="post"
action="/wetlab/handlingMolecules"
enctype="multipart/form-data"
name="updateMoleculeProtocol"
id="updateMoleculeProtocol">
name="updateExtractionProtocol"
id="updateExtractionProtocol">
{% csrf_token %}
<input type="hidden" name="action" value="updateMoleculeProtocol" />
<input type="hidden" name="action" value="updateExtractionProtocol" />
<div id="assing_molecule" class="my-3"></div>
<input type="button"
class="btn btn-outline-secondary my-3"
Expand Down Expand Up @@ -69,11 +69,11 @@ <h3>Information missing</h3>
<p>Please add the missing information and click on the submit bottom</p>
<form method="post"
enctype="multipart/form-data"
name="updateMoleculeProtocol"
id="updateMoleculeProtocol"
name="updateExtractionProtocol"
id="updateExtractionProtocol"
class="form-horizontal well">
{% csrf_token %}
<input type="hidden" name="action" value="updateMoleculeProtocol" />
<input type="hidden" name="action" value="updateExtractionProtocol" />
<input type="hidden"
name="samples"
value="{{ molecule_recorded.incomplete_sample_ids }}" />
Expand Down Expand Up @@ -559,7 +559,7 @@ <h4>Not molecule uses have been defined yet</h4>
});

$(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
Expand All @@ -568,7 +568,7 @@ <h4>Not molecule uses have been defined yet</h4>
$("<input />").attr("type", "hidden")
.attr("name", "molecule_data")
.attr("value", data_json)
.appendTo("#updateMoleculeProtocol");
.appendTo("#updateExtractionProtocol");
$("#btnSubmit").attr("disabled", true);
return true;
});
Expand Down Expand Up @@ -622,13 +622,13 @@ <h4>Not molecule uses have been defined yet</h4>
}
});
$(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)
$("<input />").attr("type", "hidden")
.attr("name", "molecule_data")
.attr("value", data_json)
.appendTo("#updateMoleculeProtocol");
.appendTo("#updateExtractionProtocol");
$("#btnSubmit").attr("disabled", true);
return true;
});
Expand Down
5 changes: 0 additions & 5 deletions wetlab/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 12 additions & 3 deletions wetlab/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -3587,6 +3594,7 @@ def search_sample(request):
)


"""
@login_required
def set_molecule_values(request):
if request.method == "POST" and request.POST["action"] == "continueWithMolecule":
Expand Down Expand Up @@ -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(",")
Expand Down Expand Up @@ -3707,6 +3715,7 @@ def set_molecule_values(request):
{"display_list": display_list},
)
return render(request, "wetlab/setMoleculeValues.html", {})
"""


@login_required
Expand Down

0 comments on commit 8221d12

Please sign in to comment.