def get_available_substrates(monomers, current, curatedonly=True, selected=None): if selected is None: selected = len(monomers) - 1 reason = None if current: if selected > 0: chirality = Substrate.objects.get(pk=monomers[selected - 1]).chirality else: chirality = None reason = 3 else: chirality = Substrate.objects.get(pk=monomers[-1]).chirality substrates = Substrate.objects.exclude(user__username='******') if not current or selected == len(monomers) - 1: aas = filter(lambda x: x.can_be_added(chirality, curatedonly), substrates) if reason is None: reason = 2 else: following = Substrate.objects.get(pk=monomers[selected + 1]) aas = filter(lambda x: x.can_be_added(chirality, curatedonly) and following.can_be_added(x.chirality, curatedonly), substrates) if reason is None: reason = 1 if len(monomers) == 1 and current: reason = 0 return (aas, reason)
def get_available_substrates(monomers, current, curatedonly=True, selected=None): if selected is None: selected = len(monomers) - 1 if current: if selected > 0: chirality = Substrate.objects.get(pk=monomers[selected - 1]).chirality else: chirality = None else: chirality = Substrate.objects.get(pk=monomers[-1]).chirality substrates = Substrate.objects.exclude(user__username='******') if not current or selected == len(monomers) - 1: aas = filter(lambda x: x.can_be_added(chirality, curatedonly), substrates) else: following = Substrate.objects.get(pk=monomers[selected + 1]) aas = filter(lambda x: x.can_be_added(chirality, curatedonly) and following.can_be_added(x.chirality, curatedonly), substrates) return aas
def get_available_monomers(request): reason = None if request.method == 'POST' and "monomer[]" in request.POST: monomers = request.POST.getlist("monomer[]") if 'selected' in request.POST: selected = int(request.POST['selected']) else: selected = None aas, reason = get_available_substrates(monomers, toBool(request.POST['current']), toBool(request.POST['curatedonly']), selected) else: aas = filter(lambda x: x.can_be_added(curatedonly=toBool(request.POST['curatedonly'])), Substrate.objects.exclude(user__username='******')) reason = 0 json = {} minid = float("Inf") for aa in aas: if aa.parent is None: name = aa.name if aa.name[0:2].upper() == 'L-' or aa.name[0:2].upper() == 'D-': name = aa.name[2:] if aa.pk in json: key = aa.pk elif aa.enantiomer is not None and aa.enantiomer.pk in json: key = aa.enantiomer.pk else: key = None if aa.enantiomer is None: chirality = 'N' else: chirality = aa.chirality if key is not None: if key < minid: minid = key json[key][chirality.lower() + "id"] = aa.pk json[key][chirality+'Children'] = [{"text": c.name, "id": c.pk} for c in aa.child.all()] #names[name]['name'] = name else: json[aa.pk] = {"id": aa.pk, chirality.lower() + "id": aa.pk, 'text': name, aa.chirality+'Children': [{"text": c.name, "id": c.pk} for c in aa.child.all()]} jsonlist = json.values() jsonlist.sort(lambda x,y: cmp(x['text'], y['text'])) return JsonResponse({"monomers": json, "monomerslist": jsonlist, "reason": reason})
def get_available_monomers(request): if request.method == 'POST' and "monomer[]" in request.POST: monomers = request.POST.getlist("monomer[]") if 'selected' in request.POST: selected = int(request.POST['selected']) else: selected = None aas = get_available_substrates(monomers, toBool(request.POST['current']), toBool(request.POST['curatedonly']), selected) else: aas = filter(lambda x: x.can_be_added(), Substrate.objects.exclude(user__username='******')) json = {} minid = float("Inf") for aa in aas: if aa.parent is None: name = aa.name if aa.name[0:2].upper() == 'L-' or aa.name[0:2].upper() == 'D-': name = aa.name[2:] if aa.pk in json: key = aa.pk elif aa.enantiomer is not None and aa.enantiomer.pk in json: key = aa.enantiomer.pk else: key = None if aa.enantiomer is None: chirality = 'N' else: chirality = aa.chirality if key is not None: if key < minid: minid = key json[key][chirality.lower() + "id"] = aa.pk json[key][chirality+'Children'] = [{"text": c.name, "id": c.pk} for c in aa.child.all()] #names[name]['name'] = name else: json[aa.pk] = {"id": aa.pk, chirality.lower() + "id": aa.pk, 'text': name, aa.chirality+'Children': [{"text": c.name, "id": c.pk} for c in aa.child.all()]} jsonlist = json.values() jsonlist.sort(lambda x,y: cmp(x['text'], y['text'])) return JsonResponse({"monomers": json, "monomerslist": jsonlist})