GithubHelp home page GithubHelp logo

Comments (4)

challet avatar challet commented on September 3, 2024 1

Thank you for the advices. I did something like the following (tried with tweaking the default "menu.html" template)

{% load sitetree %}
<ul>
	{% for item in sitetree_items %}
	<li>
        <a href="{% sitetree_url for item %}" {% if item.hint %}title="{{ item.hint }}"{% endif %} {% if item.is_current or item.in_current_branch %}class="{{ item.is_current|yesno:"current_item ," }}{{ item.in_current_branch|yesno:"current_branch," }}"{% endif %}>{{ item.title_resolved }}</a>
		{% if item.has_children %}
                        {# "mybooks" is a classical sitetree item, considered as the parent of the dynamic items #}
                        {% if item.alias == "mybooks" %} 
                            {% include "sitetree/menu.html" with sitetree_items=custom_sitetree_items %}
                        {% else %}
                            {% sitetree_children of item for menu template "sitetree/menu.html" %}
                        {% endif %
		{% endif %}
	</li>
	{% endfor %}
</ul>

With custom_sitetree_items having been set in a view :

from sitetree.utils import item
[…]
context["indice_sitetree_items"] = item(
         str(book),
         f"book_detail {book.pk}"
    )
    for book in self.current_request.user.favorite_books[:5]

I think that does the job. Thank you !

from django-sitetree.

idlesign avatar idlesign commented on September 3, 2024

Hi,

Sorry for the delay.

When calling the template tag {% sitetree_menu from "tree" include "trunk" %}, how can I (and should I ?) pass the queryset allowing to generate the items "/book/1" to "/book/5" ?

There's currently no way to represent arbitrary model entities as tree items. In many cases it would ruin the navigation (taking into account that there may be hundreds of items).
Thus, if you want to see every book in your menu (that is, if there are only a few books) you'll need to add tree items either manually (throught the admin interface) or with a help of some code.

from django-sitetree.

challet avatar challet commented on September 3, 2024

Hi,

Thank you for the answer. I've tried to explore some code implementation to achieve this. In order to make my example here more accurate, the goal is not to display all the books in the menu, but to display the top 5 favorites books for each user.

Some ideas I've tried until now :

Adding through admin interface

That would mean that the menu is the same for everyone and cannot change automatically over time. But I want it to be user bound.

Dynamic tree structuring

As suggested in the docuementation :

somewhere where app registry is already created, e.g. config.ready() or even in urls.py of your project.

So, it is also here not bound to the user. For it, I would need the dynamic registration to be made at request time, in a view for instance. But I feel like it is not means for that since the trees are not "request-self-isolated" and are stored globally

Tree handler customization

That's the closest to what I want to achieve. I've created the following class and set it as SITETREE_CLS

from sitetree.sitetreeapp import SiteTree
from sitetree.utils import item

class CustomTree(SiteTree):
    def apply_hook(self, items, sender):
        if sender in ["menu", "menu.children"]:
            # find the required "URL as pattern" treeitem by its alias.
            index = next(
                (
                    index
                    for (index, treeitem) in enumerate(items)
                    if treeitem.alias == "book_detail"
                ),
                None,
            )
            if index is not None:
                # create a set of new treeitems from a queryset result
                injected_items = [
                    item(
                        str(book),
                        f"book_detail {book.pk}"
                    )
                    for book in self.current_request.user.favorite_books[:5]
                ]
                # replace the original "URL as pattern" treeitem by the new set
                items[index : index + 1] = injected_items

        return items

It looks promising and generate new items that are indeed rendered in the html output. But

  • The new injected items are not taken into account for marking them as is_current
  • Doing it manually in the apply_hook can work but then the is_current_branch doesn't propagate and it should also be done in an other branch of the same apply_hook (it is originally done here before the call to apply_hook)
  • Some other atrtibutes like title_resolved, depth are also not set

So instead of re-doing all the logic in this method, I'm thinking about to overwrite the attach_dynamic_tree_items method to make it attach an other kind of dynamic items (i.e. on each request).
Would you think that is a good approach ? I'm a bit concerned about the cache being written following the dynamic attachement.

from django-sitetree.

idlesign avatar idlesign commented on September 3, 2024

Now I see, thank you.

You're right: there's nothing user-specific (except permissions check). I'm afraid there's no easy way to put what you want into navigation structure. It'd probably mean that you need to override a considerable part of logic and disable caching.

Personally I would debate that something highly variable like top n entities (especially depending on current user) belongs to navigation. I'd rather not to put it under sitetree.

If you still want to make that, I'd propose you to override template(s), check there for a certain item alias and render its subitems manually. That'd seem to require least effort.

from django-sitetree.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.