wagtailmenus icon indicating copy to clipboard operation
wagtailmenus copied to clipboard

Menus API

Open jasonHav opened this issue 6 years ago • 17 comments

Hey guys,

Is there any consideration to adding an API endpoint to retrieve menus? Combined with the Wagtail v2 API, I can see a great toolset to make React/Angular/Vue driven Wagtail sites.

jasonHav avatar Sep 27 '18 06:09 jasonHav

Hi @jasonHav! As it happens, I've had this in mind for a while :) I've already started laying some of the groundwork for it in this release (e.g. #276, and #279). I got pretty far in a local branch, but the changes were a little too 'sweeping', so I've decided to take a more incremental approach, improving other things as I go.

ababic avatar Sep 27 '18 22:09 ababic

Hi @jasonHav. I've made quite a bit of progress on a Menus API implementation, and I'm now seeking feedback from the community to help make sure the implementation makes sense / feels natural. Would you be willing to help review the state of the implementation so far?

It's PR #302.

ababic avatar Dec 02 '18 16:12 ababic

Looking good! I'll find a project to have a play with it and get you any feedback I can. Quickly reading over the code - what was the motivation behind using rest_framework APIView vs. Wagtail's BaseAPIEndpoint?

jasonHav avatar Dec 02 '18 20:12 jasonHav

Great, thanks @jasonHav. If you need any help getting things to work, let me know.

What was the motivation behind using rest_framework APIView vs. Wagtail's BaseAPIEndpoint?

The short answer there is that there's basically very little code in BaseAPIEndpoint that's applicable to what these menu generator views are doing. As far as I can tell, there's really nothing in Wagtail or DRF that seemed like a good fit in terms of functionality. APIView seemed like the simplest possible starting point.

ababic avatar Dec 02 '18 20:12 ababic

@ababic Do you plan to release this feature soon?

I actually want to write a Vue based application and this kind of API would be great. (At least to have a serializer which I can use on my own API endpoint)

sowinski avatar Jul 10 '19 13:07 sowinski

@sowinski #302 is pretty far along, but I made some big changes recently, and a lot of tests need reworking. I still need to find a good way to allow custom field values to be rendered for sub-pages. I am going on holiday very soon though, so progress will be stalled for a while.

ababic avatar Jul 10 '19 15:07 ababic

Thank you for the update. In the meanwhile I will just write my own serializer :)

sowinski avatar Jul 10 '19 15:07 sowinski

I am trying to iterate over the main menu. MainMenu.objects.all()[0].menu_items.all() My problem is, that the returned objects are "MainMenuItem" but I need my custom "CustomMainMenuItem".

I tried to read your code but it is not easy for a beginner :D

EDIT: Found finally something: MainMenu.objects.all()[0].get_raw_menu_items()

With that method I get the first level. How can I get the next level of an menu item?

sowinski avatar Jul 11 '19 12:07 sowinski

@sowinski. Menu items aren't really designed to be recursively queried in this way, I'm afraid. Menu instances usually prefetch all of the page data they need in advance of rendering, and sub menus request their page data from the original menu instance. This pattern works well for rendering menus from template tags, but it's tricky to make use of it directly like this.

I'm afraid all I can do is recommend that you take a look at the views and serializers from #302, and see if you can work things out from there.

ababic avatar Jul 11 '19 16:07 ababic

@ababic I just wrote a hacky solution and cache the results. If someone is changing the menu I will just refresh the cash and this is it for now.

If you need help (testing, feedback, examples etc.) let me know.

sowinski avatar Jul 12 '19 10:07 sowinski

Any news on the API progress? Just starting on a project with VUE and wagtail and this would be great to use along.

onno-timmerman avatar Nov 11 '19 13:11 onno-timmerman

@onno-timmerman I've simply not had the time to look any closer at it for a few months. Where the PR is right now is: It works, but I haven't really figured out what I want to do in terms of caching/cachability, which I feel is important element. It's easy to add template fragment caching when using the template tag approach, but you kinda lose that option when you take the modern js / API route... unless the API has something built in.

ababic avatar Nov 11 '19 22:11 ababic

Using this I'm getting an error

ValueError at /wagtailmenus_api/v1/main_menu/
'MainMenuGeneratorArgumentForm' has no field named 'current_site'.

lalasmuathasim avatar Dec 06 '19 09:12 lalasmuathasim

Any updates on this feature?

noobmaster19 avatar Aug 06 '20 11:08 noobmaster19

how did you get around this situation? did you use normal wagtail pages to structure the menus?

Mardik avatar Sep 24 '20 19:09 Mardik

Has this feature been merged?

viirak avatar Sep 05 '21 08:09 viirak

@viirak no, still a work in progress

ababic avatar Sep 06 '21 06:09 ababic

Hi everyone! Is it still a WIP?

happygrizzly avatar May 30 '23 14:05 happygrizzly

Abandoned now, sorry.

ababic avatar May 30 '23 21:05 ababic

Hey folks, so I'm going ahead and closing the issue. I feel it's stale now. Feel free to open a new issue if you still require this feature.

MrCordeiro avatar May 31 '23 10:05 MrCordeiro