django-sitetree icon indicating copy to clipboard operation
django-sitetree copied to clipboard

Providing a sitetree with ones website ?

Open Carelvd opened this issue 5 years ago • 8 comments

Thank you for sitetrees it is a really nice package.

I have a minor issue though and I'm not sure if I've messed up something or if this is a bug. I have done the following :

python -m django startproject website
move website PROJECT
cd PROJECT python manage.py startapp APPLICATION

To which I add a sitetrees.py file both under the application and the website as follows :

PROJECT\
  website\
    ...
    sitetrees.py
  APPLICATION\
    ...
    sitetrees.py

Within website\sitetrees.py I have added the following :

from sitetree.utils import tree, item
print("Website:SiteTrees")

navigation = (
    item('Home',  'home' , hint = "Landing Page", ),
    item('About', 'about', hint = "General Information", access_guest = True),
   )

sitetrees = (tree('website_navigation', items=navigation),)

Within APPLICATION\sitetrees.py I have added the following :

from sitetree.utils import tree, item
print("APPLICATION:SiteTrees")

navigation = (
    item('Home',  'home' , hint = "Landing Page", ),
    item('About', 'about', hint = "General Information", access_guest = True),
    )

sitetrees = (tree('website_navigation', items=navigation),)

I then synchronize the trees as per the documentation

(venv) E:\Django\PROJECT>python manage.py sitetree_resync_apps
APPLICATION:SiteTrees
Sitetrees found in `APPLICATION` app ...
  Processing `APPLICATION_navigation` tree ...
    Adding `Home` tree item ...
    Adding `About` tree item

The resulting tree however is empty

sitetree

Usually adding something under PROJECT/website is asking for trouble, since then you have to add website as an application under INSTALLED_APPS which, as I understand it is a bit of a no no. So I'm not expecting PROJECT/website/sitetrees.py to synchronize with the tree but I was rather hoping that PROJECT/APPLICATION/sitetrees.py would work and it appears not to do so. Have I messed up here or is there a bug I've discovered ?

I'm using Python version 3.6.3 and Django 2.1..5

Carelvd avatar Jan 23 '19 07:01 Carelvd

Thank you.

Project level sitetrees module is not checked. Since you have no errors on sync all chances that items are in DB. Please check that first. Then try to restart webserver to check whether the issue is related to cache.

Also a hint: consider using dynamic trees (see register_dynamic_trees) for your project if there's no requirement to store it in the DB.

idlesign avatar Jan 23 '19 07:01 idlesign

I'm fine with the project level not being checked (I rather expected that) but if I wipe the DB, resync sitetrees and start the server again and I still get an empty tree within the database as shown in the original image. Is this the expected behaviour ? What I'm not certain of is that the APPLICATION_navigation tree is empty ?

Carelvd avatar Jan 23 '19 07:01 Carelvd

No this is not expected. I'll try to investigate the issue this week.

Have you had a chance to check sitetree_itemsin DB is empty after syncing?

idlesign avatar Jan 23 '19 07:01 idlesign

Do you mean I should interrogate the model directly through the shell ? (I'm not sure what you mean by sitetree_items)

Carelvd avatar Jan 23 '19 07:01 Carelvd

Yes you can use Django shell or pip into database table named sitetree_items directly using DB shell.

idlesign avatar Jan 23 '19 08:01 idlesign

So interrogating the model from the shell gives me the two items I specified in APPLICATION/sitetrees.py, I have now removed website/sitetree.py, wiped the database, migrated, and re-synchronized sitetrees.

>>> from sitetree.models import TreeItem
>>> TreeItem.objects.all()
<QuerySet [<TreeItem: Home>, <TreeItem: About>]>
>>> for i in TreeItem.objects.all() :
...   print(i)
...
Home
About

I can see the tree navigation in the admin interface. Adding a new item to the tree in the admin interface shows that the items I've specified in sitetrees.py may be used as potential parents, but the items themselves are not visible in the tree directly (That is they are not shown in the foreign key/reverse look up table ?).

It seems the tree itself is recreated anew every time I re-sync. I thought this might be related to issue:135 but looking at the site tree code I see the changing of the index is intentional, at least it's the default action for the primary key and this is wiped during processing :

  • From sitetree/models.py see that you've not overridden any primary keys so the default behavior of setting a new id for each new entry is enforced.

  • From sitetree/management/commands/sitetree_resync_apps.py

    # Delete trees with the same name beforehand.
    MODEL_TREE_CLASS.objects.filter(alias=tree.alias).using(using).delete()
    # Drop id to let the DB handle it.
    tree.id = None
    

Issue:166 also seems related, is there a way to clear the site tree cache ? I wiped the various __pycache__ files in case they were the problem. I also saw that the cache is reset during the execution of sitetree/management/commands/sitetree_resync_apps.py.

Hmm... register_dynamic_trees with reset_cache=True seems to use a different mechanism from sitetree_resync_apps. I tried using the one to flush the other e.g. by creating a dynamic tree with the same name as the static one but this only overwrites the existing tree with the dynamic one and removing the dynamic one again only returns the broken static one.

Carelvd avatar Jan 23 '19 08:01 Carelvd

It seems the tree itself is recreated anew every time I re-sync.

Yes. That's how it works.

Adding a new item to the tree in the admin interface shows that the items I've specified in sitetrees.py may be used as potential parents, but the items themselves are not visible in the tree directly.

Sounds like a permission-related problem. I'll try to investigate the issue this week.

idlesign avatar Jan 24 '19 02:01 idlesign

So at last I have had a chance to check this issue using demo project — https://github.com/idlesign/django-sitetree/tree/master/demo — and that's what I see after syncing:

shot

Please try the demo with your code placed in https://github.com/idlesign/django-sitetree/blob/master/demo/demo/sitetrees.py to verify wether the issue persists.

idlesign avatar Jan 30 '19 12:01 idlesign