1

I rebuild the main menu in wagtail admin with the file wagtail_hooks.py. When I update the database, the main menu will change according to the new content in the database. Part of the file wagtail_hooks.py are as follows:

factories = DicFactory.objects.all()
num = len(factories)
factoryMenus = []
equipmentMenus = []
simulationMenus = []
equipmentNum = []
simulationNum = []
for fac in factories:
    partURL = '/admin/home/dicequipment/?factory_id='
    fullURL = partURL + str(fac.id)
    factoryMenus.append(MenuItem(fac.name, fullURL, classnames='icon icon-pilcrow', order=10000))
    equipments = DicEquipment.objects.filter(factory_id = fac.id)
    equipmentNum.append(len(equipments))
    for eq in equipments:
        partURL = '/admin/home/simulation/?equipment_id='
        fullURL = partURL + str(eq.id)
        equipmentMenus.append(MenuItem(eq.name, fullURL, classnames='icon icon-chain-broken',     order=10000))
        simulations = Simulation.objects.filter(equipment_id = eq.id)
        simulationNum.append(len(simulations))
        for sim in simulations:
            partURL = '/admin/home/simulation/inspect/'
            fullURL = partURL + str(sim.id) +'/?integrityCheck=true'
            simulationMenus.append(MenuItem(sim.name, fullURL, classnames='icon icon-list-ol', order=10000))
  
#construct the main menu
@hooks.register('construct_main_menu')
def hide_images_menu_item(request, menu_items):
    menu_items[:] = [item for item in menu_items if item.name != 'images']
    menu_items[:] = [item for item in menu_items if item.name != 'documents']
    menu_items[:] = [item for item in menu_items if item.name != 'reports']
    menu_items[:] = [item for item in menu_items if item.name != 'settings']
    menu_items[:] = [item for item in menu_items if item.name != 'explorer']
    menu_items[:] = [item for item in menu_items if item.label != '工厂']
    menu_items[:] = [item for item in menu_items if item.label != '装置']
    menu_items[:] = [item for item in menu_items if item.label != '数字模型']
    menu_items[:] = [item for item in menu_items if item.label != '算例列表']
    menu_items[:] = [item for item in menu_items if item.label != '算例输入']
    menu_items[:] = [item for item in menu_items if item.label != '硬件环境']
    menu_items[:] = [item for item in menu_items if item.label != '模拟软件']
    menu_items[:] = [item for item in menu_items if item.label != '人员列表']
    menu_items[:] = [item for item in menu_items if item.label != '算例输入']
    menu_items.append(MenuItem('工厂', '/admin/home/dicfactory/', classnames='icon icon-folder-open-inverse', order=10000))
    equipmentMenuIndex = 0
    simulationMenuIndex = 0
    factoryIndex = 0
    equipmentIndex = 0
    for i in range(num):
        menu_items.append(factoryMenus[i])
        for j in range(equipmentNum[factoryIndex]):
            menu_items.append(equipmentMenus[equipmentMenuIndex])
            equipmentMenuIndex = equipmentMenuIndex +1
            for k in range(simulationNum[equipmentIndex]):
                menu_items.append(simulationMenus[simulationMenuIndex])
                simulationMenuIndex = simulationMenuIndex +1
            equipmentIndex = equipmentIndex + 1
        factoryIndex = factoryIndex + 1

How to automatically reload the file wagtail_hooks.py when database changed? Wish for your answer and help. Thank you.

This is the admin page in my website. When I add a factory, the new factory name should appear in the main menu. admin page

txf
  • 19
  • 3

1 Answers1

1

Move all of your code into the hide_images_menu_item hook function. This will then be run on every page request - code that's not in a function but just in the body of wagtail_hooks.py will only be run once, at startup.

gasman
  • 23,691
  • 1
  • 38
  • 56
  • Thanks for your answer. I have updated my question post. When I add a new tuple in a table named factory, the main menu on the admin page should automatically update to reflect this change. The code to construct the main menu in file wagtail_hooks.py is hook function hide_images_menu_item. This function read the data in the database and construct the menu. How to automatically reload the file wagtail_hooks.py or rerun the hook function hide_images_menu_item when the database changes? Wish for your answer. Thank you. – txf Feb 05 '21 at 02:01
  • As I said, the hook function runs on every page request. So, if you perform your database query - i.e. the `DicFactory.objects.all()` line - in there, instead of putting it at the top level of wagtail_hooks.py, the results will stay up to date with any database changes. – gasman Feb 06 '21 at 13:20
  • According to what you said, I have moved all the code into the hook function hide_images_menu_item , but after I update the database and reload(refresh) the page , there is no change in the main menu. – txf Feb 22 '21 at 08:12
  • Wish for your help. Are there other ways to dynamically reload watail_hooks.py in wagtail admin? – txf Feb 24 '21 at 01:40
  • I am sorry to say that I have not solved this problem yet. I have moved all the code into hide_images_menu_item hook function, but it did not work. I need your help and advice. Hope you can see my post! Hope I can receive your answer! Thank you! – txf Jul 01 '21 at 07:01
  • Thank you for your help. I have moved some code into hide_images_menu_item hook function, but I left some related variables outside. Until today, I move these variable into the function, the admin menu can be dynamically constructed according to the database. Thanks a lot to you! – txf Jul 01 '21 at 08:28