diff --git a/atlas/forms.py b/atlas/forms.py index fd0041bf..2d80f167 100755 --- a/atlas/forms.py +++ b/atlas/forms.py @@ -1033,6 +1033,37 @@ class ConditionAutocomplete(ModelAutocomplete): model = Condition search_attrs = ["name"] + @classmethod + def get_items_from_keys(cls, keys, context_obj=None): + """Return items for given keys, ignoring empty strings. + + The autocomplete frontend sometimes sends empty string keys which + causes the ORM to raise ValueError when filtering numeric PK fields. + Filter out falsy/blank keys before querying. If no valid keys remain + return an empty queryset. + """ + # normalize and remove empty/blank values + clean_keys = [k for k in keys if k is not None and str(k).strip() != ""] + if not clean_keys: + return cls.model.objects.none() + + # try to convert numeric keys to ints where possible + parsed_keys = [] + for k in clean_keys: + try: + parsed_keys.append(int(k)) + except Exception: + parsed_keys.append(k) + + qs = cls.model.objects.filter(pk__in=parsed_keys) + + # Return a list of dicts matching the autocomplete core expectations + # (items subscriptable by keys like 'key' / 'label'). + return [ + {"key": obj.pk, "label": getattr(obj, "name", str(obj)), "value": str(obj)} + for obj in qs + ] + class ConditionAutocompleteForm(Form): condition = ModelChoiceField( diff --git a/atlas/templates/atlas/condition_detail.html b/atlas/templates/atlas/condition_detail.html index d48e1f56..b56332d3 100755 --- a/atlas/templates/atlas/condition_detail.html +++ b/atlas/templates/atlas/condition_detail.html @@ -36,6 +36,28 @@ {% empty %} — {% endfor %} + {% if request.user.is_authenticated and can_merge %} +