Add canonical field and update synonym handling in Condition model

- Introduced a new `canonical` ForeignKey field in the Condition model to manage alias relationships.
- Enhanced synonym retrieval methods to prefer canonical names and handle aliases more effectively.
- Updated condition detail templates to reflect canonical status and improved synonym display.
- Implemented migration scripts to establish canonical relationships based on existing synonym links.
This commit is contained in:
Ross
2025-11-20 22:48:03 +00:00
parent 9d57d03cbb
commit 6eeb4cbb1d
8 changed files with 199 additions and 29 deletions
+23 -6
View File
@@ -868,14 +868,31 @@ def condition_detail(request, pk):
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()
# created condition: treat as alias by setting canonical below
pass
if other and other != condition:
# add mutual synonym relationship
condition.synonym.add(other)
other.synonym.add(condition)
# Option B: treat synonym as alias/canonical relationship.
# Determine canonical/master for the current condition
master = condition.canonical if condition.canonical else condition
# If `other` belongs to a different canonical group, move its group
# to point at our master. If `other` has no canonical, set it.
if other.canonical:
# move the whole other group (canonical + aliases) to our master
group_master = other.canonical
members = Condition.objects.filter(Q(canonical=group_master) | Q(pk=group_master.pk))
for m in members:
if m.pk != master.pk:
m.canonical = master
m.save()
else:
# other is standalone; make it an alias of our master
other.canonical = master
other.save()
# Master is indicated by having no `canonical` pointer. Ensure
# it remains canonical (no save required unless changed elsewhere).
# If this was an HTMX request return the updated synonyms partial so the
# page can be updated without a full redirect. Otherwise perform the