Thuenen-GeoNode-Development / thuenen_atlas

The Thünen Atlas GeoNode project
2 stars 1 forks source link

customizing TOPBAR entries #11

Closed gannebamm closed 11 months ago

gannebamm commented 11 months ago

If I follow https://docs.geonode.org/en/master/devel/workshops/index.html#customize-the-look-and-feel and add get_menu_json.py under geonode/apps/thuenen_app/templatetags/get_menu_json.py to override GeoNodes default menu, it should change the TOPBAR menu entries.

I have added some additional lines to import needed modules in get_menu_json.py

from django import template

register = template.Library()

@register.simple_tag(takes_context=True)
def get_base_right_topbar_menu(context):

    is_mobile = _is_mobile_device(context)

    if is_mobile:
        return []

    return [
        {
            "type": "link",
            "href": "/",
            "label": "Custom 3"
        },
        {
            "type": "link",
            "href": "/",
            "label": "Custom 4"
        },
    ]
@register.simple_tag(takes_context=True)
def get_base_left_topbar_menu(context):

    is_mobile = _is_mobile_device(context)

    return [
        {
            "type": "link",
            "href": "/",
            "label": "Custom 1"
        },
        {
            "type": "link",
            "href": "/",
            "label": "Custom 2"
        },
    ]

But I will get an error:

Request Method:     GET
Request URL:    http://172.17.0.1:8001/
Django Version:     3.2.20
Exception Type:     TemplateSyntaxError
Exception Value:    

Invalid block tag on line 14: 'get_menu_json'. 
In template /usr/src/django_geonode_mapstore_client/geonode_mapstore_client/templates/geonode-mapstore-client/_geonode_config.html, error at line 14

Did you forget to register or load this tag?

Exception Location:     /usr/local/lib/python3.10/dist-packages/django/template/base.py, line 531, in invalid_block_tag
Python Executable:  /bin/python3
Python Version:     3.10.12
Python Path:    

['/usr/src/geonode',
 '/usr/src/geonode',
 '/usr/src/geonode',
 '/root/.vscode-server/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/usr/local/lib/python3.10/dist-packages',
 '/usr/src/geonode-importer',
 '/usr/src/django_geonode_mapstore_client',
 '/usr/src/importer-datapackage',

> Invalid block tag on line 14: 'get_menu_json'. Did you forget to register or load this tag?

The file I would like to 'overwrite' is: https://github.com/Thuenen-GeoNode-Development/geonode-mapstore-client/blob/thuenen_4.1.x/geonode_mapstore_client/templatetags/get_menu_json.py

What did I do wrong?

gannebamm commented 11 months ago

You need to provide all needed functions. This

from avatar.templatetags.avatar_tags import avatar_url
from django import template
from django.conf import settings
from geonode.base.models import Configuration, Menu, MenuItem

register = template.Library()

def _handle_single_item(menu_item):
    m_item = {}
    m_item['type'] = 'link'
    m_item['href'] = menu_item.url
    m_item['label'] = menu_item.title
    if menu_item.blank_target:
        m_item['target'] = '_blank'
    return m_item

def _is_mobile_device(context):
    if context and 'request' in context:
        req = context['request']
        return req.user_agent.is_mobile
    return False

@register.simple_tag(takes_context=True)
def get_base_left_topbar_menu(context):

    is_mobile = _is_mobile_device(context)

    return [
        {
            "label": "Data",
            "type": "dropdown",
            "items": [
                {
                    "type": "link",
                    "href": "/catalogue/#/search/?f=dataset",
                    "label": "Datasets"
                },
                {
                    "type": "link",
                    "href": "/catalogue/#/search/?f=document",
                    "label": "Documents"
                } if not is_mobile else None
            ]
        },
        {
            "type": "link",
            "href": "/catalogue/#/search/?f=map",
            "label": "Maps"
        },
        # DO NOT SHOW GeoStories and Dashboards
        # {
        #     "type": "link",
        #     "href": "/catalogue/#/search/?f=geostory",
        #     "label": "GeoStories"
        # },
        # {
        #     "type": "link",
        #     "href": "/catalogue/#/search/?f=dashboard",
        #     "label": "Dashboards"
        # },
        {
            "type": "link",
            "href": "/catalogue/#/search/?f=featured",
            "label": "Featured"
        }
    ]

@register.simple_tag(takes_context=True)
def get_base_right_topbar_menu(context):

    is_mobile = _is_mobile_device(context)

    if is_mobile:
        return []

    home = {
        "type": "link",
        "href": "/",
        "label": "Home"
    }
    user = context.get('request').user
    about = {
            "label": "About",
            "type": "dropdown",
            "items": [
                {
                    "type": "link",
                    "href": "/people/",
                    "label": "People"
                },
                {
                    "type": "link",
                    "href": "/groups/",
                    "label": "Groups"
                }
            ]
        }
    if user.is_authenticated and not Configuration.load().read_only:
        about['items'].extend([
            {
                "type": "divider"
            },
            {
                "type": "link",
                "href": "/invitations/geonode-send-invite/",
                "label": "Invite users"
            },
            {
                "type": "link",
                "href": "/admin/people/profile/add/",
                "label": "Add user"
            } if user.is_superuser else None,
            {
                "type": "link",
                "href": "/groups/create/",
                "label": "Create group"
            }if user.is_superuser else None,
        ])
    return [home, about]

@register.simple_tag(takes_context=True)
def get_user_menu(context):

    is_mobile = _is_mobile_device(context)
    user = context.get('request').user

    if not user.is_authenticated:
        return [
            {
                "label": "Register",
                "type": "link",
                "href": "/account/signup/?next=/"
            } if settings.ACCOUNT_OPEN_SIGNUP and not Configuration.load().read_only else None,
            {
                "label": "Sign in",
                "type": "link",
                "href": "/account/login/?next=/"
            },
        ]

    devider = {
        "type": "divider"
    }

    profile_link = {
        "type": "link",
        # get href of user profile
        "href": user.get_absolute_url(),
        "label": "Profile"
    }

    logout = {
        "type": "link",
        "href": "/account/logout/?next=/",
        "label": "Log out"
    }

    if is_mobile:
        return [
            {
                # get src of user avatar
                "image": avatar_url(user),
                "type": "dropdown",
                "className": "gn-user-menu-dropdown",
                "items": [
                    profile_link,
                    devider,
                    logout
                ]
            }
        ]

    profile = {
        # get src of user avatar
        "image": avatar_url(user),
        "type": "dropdown",
        "className": "gn-user-menu-dropdown",
        "items": [
            profile_link,
            {
                "type": "link",
                "href": "/social/recent-activity",
                "label": "Recent activity"
            },
            {
                "type": "link",
                "href": "/catalogue/#/search/?f=favorite",
                "label": "Favorites"
            },
            {
                "type": "link",
                "href": "/messages/inbox/",
                "label": "Inbox"
            },
            devider,
        ]
    }
    general = [
        {
            "type": "link",
            "href": "/help/",
            "label": "Help"
        },
        devider,
        logout
    ]
    monitoring = []
    if settings.MONITORING_ENABLED:
        monitoring = [
            devider,
            {
                "type": "link",
                "href": "/monitoring/",
                "label": "Monitoring & Analytics"
            }
        ]
    admin_only = [
        {
            "type": "link",
            "href": "/admin/",
            "label": "Admin"
        },
        {
            "type": "link",
            "href": settings.GEOSERVER_WEB_UI_LOCATION,
            "label": "GeoServer"
        }
    ] + monitoring + [devider] + general

    if user.is_superuser:
        profile['items'].extend(admin_only)
    else:
        profile['items'].extend(general)

    return [profile]

@register.simple_tag
def get_menu_json(placeholder_name):
    menus = {
        m: MenuItem.objects.filter(menu=m).order_by('order')
        for m in Menu.objects.filter(placeholder__name=placeholder_name)
    }
    ms = []
    for menu, menu_items in menus.items():
        if len(menu_items) > 1:
            m = {}
            m['label'] = menu.title
            m['type'] = 'dropdown'
            m['items'] = []
            for menu_item in menu_items:
                m_item = _handle_single_item(menu_item)
                m['items'].append(m_item)

            ms.append(m)
        if len(menu_items) == 1:
            m = _handle_single_item(menu_items.first())
            ms.append(m)
    return ms

For example will work as expected and not show geostories and dashboards anymore: grafik

ridoo commented 11 months ago

@gannebamm ~is this something we should add to the thuenen_atlas project?~ Ah, I just saw this relates to geonode-mapstore-client

Did you commit this somewhere?

gannebamm commented 11 months ago

@ridoo I am still investigating options. One goal is to declutter the UI with filters we do not use like GeoApps and GeoStories. Currently, I additionally changed the about section to hold the Developer and GDPR and BSI relevant informations like Legal notice and Accessibility (see #12) for anonymous users: grafik

for logged in users also people and groups will be displayed: grafik

Superuser will also see Add user and Create group: grafik