def new_exp(request, experiment_id=None): def generate_sample_name(sample): """ naming standard: CellLine.Library.TreatmentIfAny.shON/shOFF.time.A/B/C Example: A549.K1_2.EZH2_150nM.shON.T21.A """ sample_name = ".".join([sample.cell_model.CCLE_name, sample.shRNA_library.LibName]) # if sample.pool_number is not None: # pool_str = '_P' # tmp = '' # for p in sample.pool_number(): # tmp += '_'+p.description # pool_str += tmp[1:] # else: # pool_str = '_PNone' # sample_name = sample_name + pool_str if sample.treatment and sample.treatment_dose: treatment = "_".join([sample.treatment.compoundName, str(sample.treatment_dose)]) sample_name = ".".join([sample_name, treatment]) shrna = "shON" if not sample.shRNA_on: shrna = "shOFF" sample_name = ".".join([sample_name, shrna, "".join(["T", str(sample.time_in_days)]), sample.replicate]) if sample.other_tag: sample_name = sample_name + "." + sample.other_tag return sample_name if experiment_id: experiment = get_object_or_404(Experiment, experiment_id=experiment_id) expform = ExperimentForm(instance=experiment) samforms = SampleFormSet(instance=experiment) # if experiment.user != request.user: # return HttpResponseForbidden() else: experiment = Experiment() # (user=request.user) if request.POST: if "create_exp" in request.POST or "add_sample" in request.POST: expform = ExperimentForm(request.POST, instance=experiment) samforms = SampleFormSet(request.POST, instance=experiment) if expform.is_valid() and samforms.is_valid(): new_exp = expform.save(commit=False) # automatic generate or modify fields # save modified form new_exp.save() expform.save_m2m() # sample forms samforms.instance = new_exp if samforms.has_changed(): for samform in samforms: if samform.is_valid() and samform.has_changed() and (samform is not None): new_sam = samform.save(commit=False) # new_sam.pool_number = samform.cleaned_data['pool_number'] # new_sam.sample_id = experiment.sample_set.count() + 1 new_sam.sample_name = generate_sample_name(new_sam) # new_sam.save() # samform.save_m2m() new_sam = samforms.save() return HttpResponseRedirect("/virtual/new/confirm/%s/" % new_exp.experiment_id) elif "delete_exp" in request.POST: exp2delete = get_object_or_404(Experiment, experiment_id=experiment.experiment_id).delete() # return HttpResponseRedirect('/virtual/delete/%s/' % experiment.experiment_id) return HttpResponseRedirect("/virtual/new/confirm/%s/" % experiment.experiment_id) else: expform = ExperimentForm(instance=experiment) samforms = SampleFormSet(instance=experiment) args = {} args.update(csrf(request)) args["expform"] = expform args["samforms"] = samforms return render_to_response("newExpView/new.html", args)