Implement synonym management in condition detail: add forms for adding synonyms and handle POST requests for synonym creation.
This commit is contained in:
+43
-2
@@ -830,7 +830,12 @@ def question_schema_schemas(request):
|
||||
def condition_detail(request, pk):
|
||||
condition = get_object_or_404(Condition, pk=pk)
|
||||
|
||||
form = ConditionAutocompleteForm()
|
||||
# form used for the merge UI
|
||||
merge_form = ConditionAutocompleteForm()
|
||||
|
||||
# form used to add a synonym (can post either an existing condition via autocomplete
|
||||
# or provide a plain name in 'synonym_name')
|
||||
synonym_form = ConditionAutocompleteForm()
|
||||
|
||||
can_merge = False
|
||||
|
||||
@@ -840,11 +845,47 @@ def condition_detail(request, pk):
|
||||
):
|
||||
can_merge = True
|
||||
|
||||
# Handle POST to add a synonym
|
||||
if request.method == "POST" and request.POST.get("action") == "add_synonym":
|
||||
# only allow staff/editors
|
||||
if not (
|
||||
request.user.is_superuser
|
||||
or request.user.groups.filter(name="atlas_editor").exists()
|
||||
):
|
||||
return HttpResponse("Forbidden", status=403)
|
||||
|
||||
# Try autocomplete-selected condition first
|
||||
form = ConditionAutocompleteForm(request.POST)
|
||||
other = None
|
||||
if form.is_valid():
|
||||
other = form.cleaned_data.get("condition")
|
||||
|
||||
# If no autocomplete selection, accept a free-text name
|
||||
synonym_name = request.POST.get("synonym_name")
|
||||
if not other and synonym_name:
|
||||
other, created = Condition.objects.get_or_create(name=synonym_name)
|
||||
if created:
|
||||
# mark created synonym as non-primary
|
||||
other.primary = False
|
||||
other.save()
|
||||
|
||||
if other and other != condition:
|
||||
# add mutual synonym relationship
|
||||
condition.synonym.add(other)
|
||||
other.synonym.add(condition)
|
||||
|
||||
return redirect("atlas:condition_detail", pk=condition.pk)
|
||||
|
||||
# logging.debug(atlas.subspecialty.first().name.all())
|
||||
return render(
|
||||
request,
|
||||
"atlas/condition_detail.html",
|
||||
{"condition": condition, "form": form, "can_merge": can_merge},
|
||||
{
|
||||
"condition": condition,
|
||||
"form": merge_form,
|
||||
"synonym_form": synonym_form,
|
||||
"can_merge": can_merge,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user