Skip to content

Commit 8221d12

Browse files
committed
Solved issue BU-ISCIII#310 Key error for a sample with no molecule extraction parameters are requested
1 parent b536145 commit 8221d12

File tree

5 files changed

+65
-46
lines changed

5 files changed

+65
-46
lines changed

core/models.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,7 @@ def get_massive(self):
14821482

14831483
class MoleculePreparationManager(models.Manager):
14841484
def create_molecule(self, molecule_data):
1485+
req_upd_sample_state = False
14851486
molecule_used_obj = MoleculeType.objects.filter(
14861487
molecule_type__exact=molecule_data["molecule_type"]
14871488
).last()
@@ -1492,17 +1493,27 @@ def create_molecule(self, molecule_data):
14921493
protocol_used_obj = Protocols.objects.filter(
14931494
name__exact=molecule_data["protocol_used"], type__exact=protocol_type_obj
14941495
).last()
1496+
if ProtocolParameters.objects.filter(protocol_id=protocol_used_obj).exists():
1497+
m_state = StatesForMolecule.objects.get(
1498+
molecule_state_name__exact="defined"
1499+
)
1500+
else:
1501+
m_state = StatesForMolecule.objects.get(
1502+
molecule_state_name__exact="assigned_parameters"
1503+
)
1504+
req_upd_sample_state = True
14951505
new_molecule = self.create(
14961506
protocol_used=protocol_used_obj,
14971507
sample=molecule_data["sample"],
14981508
molecule_type=molecule_used_obj,
1499-
state=StatesForMolecule.objects.get(molecule_state_name__exact="defined"),
1509+
state=m_state,
15001510
molecule_code_id=molecule_data["molecule_code_id"],
15011511
molecule_extraction_date=molecule_data["molecule_extraction_date"],
15021512
extraction_type=molecule_data["extraction_type"],
15031513
molecule_user=User.objects.get(username__exact=molecule_data["user"]),
15041514
)
1505-
1515+
if req_upd_sample_state is True:
1516+
new_molecule.sample.set_state("Pending for use")
15061517
return new_molecule
15071518

15081519

core/utils/samples.py

+30-26
Original file line numberDiff line numberDiff line change
@@ -1293,32 +1293,37 @@ def get_molecule_protocols(apps_name):
12931293
def get_molecule_data_and_protocol_parameters(protocol_objs):
12941294
mol_data_parm = {}
12951295
for protocol_obj, mol_ids in protocol_objs.items():
1296-
prot_name = protocol_obj.get_name()
1297-
mol_data_parm[prot_name] = {}
1298-
mol_data_parm[prot_name]["params_type"] = (
1299-
core.utils.protocols.get_protocol_parameters_and_type(protocol_obj)
1300-
)
1301-
mol_data_parm[prot_name][
1302-
"fix_heading"
1303-
] = core.core_config.HEADING_FOR_MOLECULE_ADDING_PARAMETERS
1304-
mol_data_parm[prot_name]["lot_kit"] = (
1305-
core.utils.commercial_kits.get_lot_commercial_kits(protocol_obj)
1306-
)
1307-
mol_data_parm[prot_name]["param_heading"] = []
1308-
prot_params = core.models.ProtocolParameters.objects.filter(
1296+
# check if the protocol has parameters
1297+
if core.models.ProtocolParameters.objects.filter(
13091298
protocol_id=protocol_obj, parameter_used=True
1310-
).order_by("parameter_order")
1311-
for param in prot_params:
1312-
mol_data_parm[prot_name]["param_heading"].append(param.get_parameter_name())
1313-
mol_data_parm[prot_name]["param_heading_in_string"] = ";".join(
1314-
mol_data_parm[prot_name]["param_heading"]
1315-
)
1316-
mol_data_parm[prot_name]["m_data"] = list(
1317-
core.models.MoleculePreparation.objects.filter(pk__in=mol_ids).values_list(
1318-
"pk", "sample__sample_name", "molecule_code_id"
1299+
).exists():
1300+
prot_name = protocol_obj.get_name()
1301+
mol_data_parm[prot_name] = {}
1302+
mol_data_parm[prot_name]["params_type"] = (
1303+
core.utils.protocols.get_protocol_parameters_and_type(protocol_obj)
1304+
)
1305+
mol_data_parm[prot_name][
1306+
"fix_heading"
1307+
] = core.core_config.HEADING_FOR_MOLECULE_ADDING_PARAMETERS
1308+
mol_data_parm[prot_name]["lot_kit"] = (
1309+
core.utils.commercial_kits.get_lot_commercial_kits(protocol_obj)
1310+
)
1311+
mol_data_parm[prot_name]["param_heading"] = []
1312+
prot_params = core.models.ProtocolParameters.objects.filter(
1313+
protocol_id=protocol_obj, parameter_used=True
1314+
).order_by("parameter_order")
1315+
for param in prot_params:
1316+
mol_data_parm[prot_name]["param_heading"].append(
1317+
param.get_parameter_name()
1318+
)
1319+
mol_data_parm[prot_name]["param_heading_in_string"] = ";".join(
1320+
mol_data_parm[prot_name]["param_heading"]
1321+
)
1322+
mol_data_parm[prot_name]["m_data"] = list(
1323+
core.models.MoleculePreparation.objects.filter(
1324+
pk__in=mol_ids
1325+
).values_list("pk", "sample__sample_name", "molecule_code_id")
13191326
)
1320-
)
1321-
13221327
return mol_data_parm
13231328

13241329

@@ -1834,7 +1839,7 @@ def record_molecule_use(from_data, app_name):
18341839
return molecule_use_information
18351840

18361841

1837-
def record_molecules(samples, excel_data, heading, user, app_name):
1842+
def record_extract_protocol(samples, excel_data, heading, user, app_name):
18381843
"""Recored the molecues defined in excel_data. If information is missing
18391844
returns the data to display again for correcting.
18401845
@@ -1898,7 +1903,6 @@ def record_molecules(samples, excel_data, heading, user, app_name):
18981903
molecule_data["molecule_code_id"] = code_split.group(1) + str(number_code)
18991904
else:
19001905
molecule_data["molecule_code_id"] = sample_obj.get_sample_code() + "_E1"
1901-
19021906
molecule_obj = core.models.MoleculePreparation.objects.create_molecule(
19031907
molecule_data
19041908
)

wetlab/templates/wetlab/handling_molecules.html

+10-10
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ <h3>Assign the Molecule Protocol to following samples.</h3>
3838
<form method="post"
3939
action="/wetlab/handlingMolecules"
4040
enctype="multipart/form-data"
41-
name="updateMoleculeProtocol"
42-
id="updateMoleculeProtocol">
41+
name="updateExtractionProtocol"
42+
id="updateExtractionProtocol">
4343
{% csrf_token %}
44-
<input type="hidden" name="action" value="updateMoleculeProtocol" />
44+
<input type="hidden" name="action" value="updateExtractionProtocol" />
4545
<div id="assing_molecule" class="my-3"></div>
4646
<input type="button"
4747
class="btn btn-outline-secondary my-3"
@@ -69,11 +69,11 @@ <h3>Information missing</h3>
6969
<p>Please add the missing information and click on the submit bottom</p>
7070
<form method="post"
7171
enctype="multipart/form-data"
72-
name="updateMoleculeProtocol"
73-
id="updateMoleculeProtocol"
72+
name="updateExtractionProtocol"
73+
id="updateExtractionProtocol"
7474
class="form-horizontal well">
7575
{% csrf_token %}
76-
<input type="hidden" name="action" value="updateMoleculeProtocol" />
76+
<input type="hidden" name="action" value="updateExtractionProtocol" />
7777
<input type="hidden"
7878
name="samples"
7979
value="{{ molecule_recorded.incomplete_sample_ids }}" />
@@ -559,7 +559,7 @@ <h4>Not molecule uses have been defined yet</h4>
559559
});
560560

561561
$(document).ready(function () {
562-
$("#updateMoleculeProtocol").submit(function (e) {
562+
$("#updateExtractionProtocol").submit(function (e) {
563563
//stop submitting the form to see the disabled button effect
564564
// e.preventDefault();
565565
//disable the submit button
@@ -568,7 +568,7 @@ <h4>Not molecule uses have been defined yet</h4>
568568
$("<input />").attr("type", "hidden")
569569
.attr("name", "molecule_data")
570570
.attr("value", data_json)
571-
.appendTo("#updateMoleculeProtocol");
571+
.appendTo("#updateExtractionProtocol");
572572
$("#btnSubmit").attr("disabled", true);
573573
return true;
574574
});
@@ -622,13 +622,13 @@ <h4>Not molecule uses have been defined yet</h4>
622622
}
623623
});
624624
$(document).ready(function () {
625-
$("#updateMoleculeProtocol").submit(function (e) {
625+
$("#updateExtractionProtocol").submit(function (e) {
626626
var table_data = $('#missing_data').jexcel('getData')
627627
var data_json = JSON.stringify(table_data)
628628
$("<input />").attr("type", "hidden")
629629
.attr("name", "molecule_data")
630630
.attr("value", data_json)
631-
.appendTo("#updateMoleculeProtocol");
631+
.appendTo("#updateExtractionProtocol");
632632
$("#btnSubmit").attr("disabled", true);
633633
return true;
634634
});

wetlab/urls.py

-5
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,6 @@
203203
wetlab.views.sequencer_inventory,
204204
name="sequencer_inventory",
205205
),
206-
path(
207-
"setMoleculeValues",
208-
wetlab.views.set_molecule_values,
209-
name="set_molecule_values",
210-
),
211206
path(
212207
"sequencerConfiguration",
213208
wetlab.views.sequencer_configuration,

wetlab/views.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -3244,7 +3244,8 @@ def handling_molecules(request):
32443244
)
32453245

32463246
elif (
3247-
request.method == "POST" and request.POST["action"] == "updateMoleculeProtocol"
3247+
request.method == "POST"
3248+
and request.POST["action"] == "updateExtractionProtocol"
32483249
):
32493250
heading = core.core_config.HEADING_FOR_MOLECULE_PROTOCOL_DEFINITION.copy()
32503251
heading.append("s_id")
@@ -3253,7 +3254,7 @@ def handling_molecules(request):
32533254
)
32543255
if len(samples) == 0:
32553256
return redirect("handling_molecules")
3256-
molecule_recorded = core.utils.samples.record_molecules(
3257+
molecule_recorded = core.utils.samples.record_extract_protocol(
32573258
samples, excel_data, heading, request.user, __package__
32583259
)
32593260
if "incomplete" in molecule_recorded:
@@ -3268,6 +3269,12 @@ def handling_molecules(request):
32683269
molecule_recorded
32693270
)
32703271
)
3272+
if len(molecule_parameters) == 0:
3273+
return render(
3274+
request,
3275+
"wetlab/handling_molecules.html",
3276+
{"molecule_parameters_updated": True},
3277+
)
32713278
protocol_list = ";".join(list(molecule_parameters.keys()))
32723279
return render(
32733280
request,
@@ -3587,6 +3594,7 @@ def search_sample(request):
35873594
)
35883595

35893596

3597+
"""
35903598
@login_required
35913599
def set_molecule_values(request):
35923600
if request.method == "POST" and request.POST["action"] == "continueWithMolecule":
@@ -3622,7 +3630,7 @@ def set_molecule_values(request):
36223630
elif (
36233631
request.method == "POST" and request.POST["action"] == "updateMoleculeProtocol"
36243632
):
3625-
molecule_recorded = core.utils.samples.record_molecules(request)
3633+
molecule_recorded = core.utils.samples.record_extract_protocol(request)
36263634
36273635
if "heading" not in molecule_recorded:
36283636
samples = request.POST["samples"].split(",")
@@ -3707,6 +3715,7 @@ def set_molecule_values(request):
37073715
{"display_list": display_list},
37083716
)
37093717
return render(request, "wetlab/setMoleculeValues.html", {})
3718+
"""
37103719

37113720

37123721
@login_required

0 commit comments

Comments
 (0)