GeoNode / geonode

GeoNode is an open source platform that facilitates the creation, sharing, and collaborative use of geospatial data.
https://geonode.org/
Other
1.45k stars 1.13k forks source link

hierarchical keyword parents will not be set automatically #6727

Closed gannebamm closed 2 years ago

gannebamm commented 3 years ago

Expected Behavior

If I use a hierarchical keyword like:

_AAA_hierarchytest

_AAA_hierarchytest_A_cat

_AAA_hierarchytest_A_cat_option1

I would expect tagging a dataset with _AAA_hierarchytest_A_cat_option1 should apply all parent keywords, too.

Actual Behavior

It will only get the child keyword. Therefore you will not be able to search for it in the search window, since no parent keywords are present. If parent keywords are present everything is fine.

By this, the administrators are unable to set hierarchical trees as keywords beforehand but have to rely on an already present set of keyworded datasets to build the tree.

Steps to Reproduce the Problem

  1. Go to master.demo.geonode.org
  2. upload something and pick child as keyword
  3. try to filter per keyword from the UI

It will fail if no other dataset with parent keywords is present.

Example dataset with only the child: https://master.demo.geonode.org/layers/geonode:someHydroData0

Example dataset with the whole hierarchy of keywords: https://master.demo.geonode.org/layers/geonode_data:geonode:test_2_10_2

Use your own user on https://master.demo.geonode.org and:

  1. join https://master.demo.geonode.org/groups/group/public-group/?limit=5&offset=0
  2. change metadata keywords of https://master.demo.geonode.org/layers/geonode_data:geonode:test_2_10_2
  3. try to find https://master.demo.geonode.org/layers/geonode:someHydroData0

Specifications

gannebamm commented 3 years ago

Adding the missing hierarchical keywords is actually quite easy. You could just add all ancestors in e.g. the ResourceBase save() function like:

geonode/base/models.py:923

            elif self.pk:
                # Resource Updated
                _notification_sent = False

                # add hierarchical keyword ancestors to build the full tree
                for hkeyword in self.keywords.all():
                    ancestors = hkeyword.get_ancestors()
                    self.keywords.add(*ancestors)

This will add all keywords since they are idempotent and adding them again wont change the result.

But for some reason the hierarchy rendering often fails. Something is not working with the code at geonode/base/models.py:391

                    if (not parent and depth == 1) or \
                            (parent and depth == parent.depth):
                        ret.append(newobj)
                     else:
                         parentobj = pyobj.get_parent()
                         parentser = lnk[parentobj.pk] #throws exception...
                         if 'nodes' not in parentser:
                             parentser['nodes'] = []
                         parentser['nodes'].append(newobj)
                     lnk[pyobj.pk] = newobj
        except Exception:
            pass
        return ret

The exception will result in an emtpy ret and a broken keywords search field.

gannebamm commented 3 years ago

@mtnorthcott Hi, do you think the above is something you could use in the Catalyst Academy? The candidate has to debug the if/else except statement to figure out what exactly breaks the rendering and method. I was not yet able to understand what is wrong there.

mtnorthcott commented 3 years ago

@gannebamm Happy New Year and thanks for your suggestion. I think we could definitely look into debugging that block if time permits. If not, I would be happy to continue wherever the Academy finishes off :)