11from django .apps import apps
22from django .conf import settings
3- from django .urls import reverse
3+ from django .urls import reverse_lazy
44from django .utils .translation import gettext_lazy as _
55from netbox .plugins import PluginMenu , PluginMenuButton , PluginMenuItem
66from packaging import version
2121)
2222
2323
24- def get_menu ():
25- CustomObjectType = apps .get_model (APP_LABEL , "CustomObjectType" )
26- menu_items = []
27- for custom_object_type in CustomObjectType .objects .all ():
28- model = custom_object_type .get_model ()
29- menu_items .append (
30- PluginMenuItem (
24+ class CustomObjectTypeMenuItems :
25+
26+ def __iter__ (self ):
27+ CustomObjectType = apps .get_model (APP_LABEL , "CustomObjectType" )
28+ for custom_object_type in CustomObjectType .objects .all ():
29+ model = custom_object_type .get_model ()
30+ add_button = PluginMenuButton (
31+ None ,
32+ _ ("Add" ),
33+ "mdi mdi-plus-thick" ,
34+ )
35+ add_button .url = reverse_lazy (
36+ f"plugins:{ APP_LABEL } :customobject_add" ,
37+ kwargs = {
38+ "custom_object_type" : custom_object_type .name .lower ()
39+ },
40+ )
41+ menu_item = PluginMenuItem (
3142 link = None ,
32- url = reverse (
33- f"plugins:{ APP_LABEL } :customobject_list" ,
34- kwargs = {"custom_object_type" : custom_object_type .name .lower ()},
35- ),
3643 link_text = _ (title (model ._meta .verbose_name_plural )),
37- buttons = (
38- PluginMenuButton (
39- None ,
40- _ ("Add" ),
41- "mdi mdi-plus-thick" ,
42- url = reverse (
43- f"plugins:{ APP_LABEL } :customobject_add" ,
44- kwargs = {
45- "custom_object_type" : custom_object_type .name .lower ()
46- },
47- ),
48- ),
49- ),
44+ buttons = (add_button ,),
5045 )
51- )
52- return PluginMenu (
53- label = "Custom Objects" ,
54- groups = (
55- (_ ("Object Types" ), (custom_object_type_plugin_menu_item ,)),
56- (_ ("Objects" ), tuple (menu_items )),
57- ),
58- icon_class = "mdi mdi-toy-brick-outline" ,
59- )
46+ menu_item .url = reverse_lazy (
47+ f"plugins:{ APP_LABEL } :customobject_list" ,
48+ kwargs = {"custom_object_type" : custom_object_type .name .lower ()},
49+ )
50+ yield menu_item
6051
6152
6253current_version = version .parse (settings .RELEASE .version )
63- if current_version < version .parse ("4.4.0" ):
64- menu = PluginMenu (
65- label = "Custom Objects" ,
66- groups = ((_ ("Object Types" ), (custom_object_type_plugin_menu_item ,)),),
67- icon_class = "mdi mdi-toy-brick-outline" ,
68- )
69- else :
70- menu = get_menu
54+
55+ groups = [(_ ("Object Types" ), (custom_object_type_plugin_menu_item ,))]
56+ if current_version >= version .parse ("4.3.4" ):
57+ groups .append ((_ ("Objects" ), CustomObjectTypeMenuItems ()))
58+
59+ menu = PluginMenu (
60+ label = _ ("Custom Objects" ),
61+ groups = tuple (groups ),
62+ icon_class = "mdi mdi-toy-brick-outline" ,
63+ )
0 commit comments