FreedomBox/plinth/menu.py
Sunil Mohan Adapa 2dd00a8f08
*: Fix all typing hint related errors
- Try to mark class variables in component classes.

- Leave typing hints generic, such as 'list' and 'dict' where content is usually
not filled, too complex, or context is unimportant.

- backups: Handle failure for tarfile extraction so that methods are not called
on potentially None valued variables.

- backups: Prevent potentially passing a keyword argument twice.

- dynamicdns: Deal properly with outcome of urlparsing.

- ejabberd: Deal with failed regex match

- email: Fix a mypy compliant when iterating a filtered list.

- tor: Don't reuse variables for different typed values.

- tor: Don't reuse variables for different typed values.

- operation: Return None explicitly.

- operation: Ensure that keyword argument is not repeated.

Tests:

- Where only typing hints were modified and no syntax error came up, additional
testing was not done.

- `mypy --ignore-missing-imports .` run successfully.

- Generate developer documentation.

- Service runs without errors upon start up.

- backups: Listing and restoring specific apps from a backup works.

- backups: Mounting a remote backup repository works.

- NOT TESTED: dynamicdns: Migrating from old style configuration works.

- ejabberd: Verify that setting coturn configuration works.

- email: Test that showing configuration from postfix works.

- tor: Orport value is properly shown.

- transmission: Configuration values are properly set.

- users: Running unit tests as root works.

- operation: Operation status messages are show properly during app install.

- ./setup.py install runs

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2023-09-25 20:03:24 -04:00

104 lines
3.7 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
from typing import ClassVar
from django.urls import reverse_lazy
from plinth import app
class Menu(app.FollowerComponent):
"""Component to manage a single menu item."""
_all_menus: ClassVar[set['Menu']] = set()
def __init__(self, component_id, name=None, short_description=None,
icon=None, url_name=None, url_args=None, url_kwargs=None,
parent_url_name=None, order=50, advanced=False):
"""Initialize a new menu item with basic properties.
name is the label of the menu item.
short_description is an optional description shown on the menu item.
icon is the icon to be displayed for the menu item. Icon can be the
name of a glyphicon from the Fork Awesome font's icon set:
https://forkawesome.github.io/Fork-Awesome/icons/. In this case, the
icon name starts with the string 'fa-'. Alternatively, the icon can
also be a file under the directory plinth/modules/<app>/static/icons/,
provided without an extension. SVG icons are preferred. Currently, both
PNG and SVG icons with the same name are used. For example, if the
value of icon is 'myicon' and app_id in App class is 'myapp', then two
icons files plinth/modules/myapp/static/icons/myicon.svg and
plinth/modules/myapp/static/icons/myicon.png are used in the interface.
url_name is the name of url location that will be activated when the
menu item is selected. This is not optional. url_args and url_kwargs
are sent to reverse() when resolving url from url_name.
parent_url_name optionally specifies the menu item under which this
menu item should become a child.
order is the numerical rank of this item within the menu. Lower order
items appear closest to the top/left of the menu. By convention, we use
the spectrum between 0 and 100 to rank orders, but feel free to
disregard that. If you need more granularity, don't bother renumbering
things. Feel free to use fractional orders.
advanced decides whether to show the menu item only in advanced mode.
"""
super().__init__(component_id)
if not url_name:
raise ValueError('Valid url_name is expected')
url = reverse_lazy(url_name, args=url_args, kwargs=url_kwargs)
self.name = name
self.short_description = short_description
self.icon = icon
self.url = url
self.order = order
self.advanced = advanced
self.url_name = url_name
self.url_args = url_args
self.url_kwargs = url_kwargs
self.parent_url_name = parent_url_name
# Add self to global list of menu items.
self._all_menus.add(self)
@property
def items(self):
"""Return the list of children for this menu item."""
return [
item for item in self._all_menus
if item.parent_url_name == self.url_name
]
def sorted_items(self):
"""Return menu items in sorted order according to current locale."""
return sorted(self.items, key=lambda x: (x.order, x.name.lower()))
def active_item(self, request):
"""Return the first active item (e.g. submenu) that is found."""
for item in self.items:
if request.path.startswith(str(item.url)):
return item
return None
main_menu = None
def init():
"""Create main menu and other essential menus."""
global main_menu
main_menu = Menu('menu-index', url_name='index')
Menu('menu-apps', icon='fa-download', url_name='apps',
parent_url_name='index')
Menu('menu-system', icon='fa-cog', url_name='system',
parent_url_name='index')