From 33c022b13bc527c9b072463f4da34719e350ebcd Mon Sep 17 00:00:00 2001 From: Ben Cole Date: Tue, 28 Apr 2015 14:26:08 +0100 Subject: [PATCH 1/5] Add kwargs to item creation --- sitetree/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sitetree/utils.py b/sitetree/utils.py index a9c5a776..a97b9578 100644 --- a/sitetree/utils.py +++ b/sitetree/utils.py @@ -45,7 +45,7 @@ def traverse(items): def item(title, url, children=None, url_as_pattern=True, hint='', alias='', description='', in_menu=True, in_breadcrumbs=True, in_sitetree=True, access_loggedin=False, access_guest=False, - access_by_perms=None, perms_mode_all=True): + access_by_perms=None, perms_mode_all=True, **kwargs): """Dynamically creates and returns a sitetree item object. :param str title: @@ -69,7 +69,7 @@ def item(title, url, children=None, url_as_pattern=True, hint='', alias='', desc item_obj = get_tree_item_model()(title=title, url=url, urlaspattern=url_as_pattern, hint=hint, alias=alias, description=description, inmenu=in_menu, insitetree=in_sitetree, inbreadcrumbs=in_breadcrumbs, - access_loggedin=access_loggedin, access_guest=access_guest) + access_loggedin=access_loggedin, access_guest=access_guest, **kwargs) item_obj.id = generate_id_for(item_obj) item_obj.is_dynamic = True From d7093fb039a6d9d8bb191c3bc2c5f9e7917b6c18 Mon Sep 17 00:00:00 2001 From: Ben Cole Date: Tue, 28 Apr 2015 15:41:17 +0100 Subject: [PATCH 2/5] Fix code-block indentation --- docs/source/apps.rst | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/source/apps.rst b/docs/source/apps.rst index 1c62c5b3..7045b6b8 100644 --- a/docs/source/apps.rst +++ b/docs/source/apps.rst @@ -16,24 +16,24 @@ Let's suppose you have `books` application and want do define a sitetree for it. .. code-block:: python - from sitetree.utils import tree, item - - # Be sure you defined `sitetrees` in your module. - sitetrees = ( - # Define a tree with `tree` function. - tree('books', items=[ - # Then define items and their children with `item` function. - item('Books', 'books-listing', children=[ - item('Book named "{{ book.title }}"', 'books-details', in_menu=False, in_sitetree=False), - item('Add a book', 'books-add'), - item('Edit "{{ book.title }}"', 'books-edit', in_menu=False, in_sitetree=False) - ]) - ]), - # ... You can define more than one tree for your app. - ) - - - Please consider `tree` and `item` signatures for possible options. + from sitetree.utils import tree, item + + # Be sure you defined `sitetrees` in your module. + sitetrees = ( + # Define a tree with `tree` function. + tree('books', items=[ + # Then define items and their children with `item` function. + item('Books', 'books-listing', children=[ + item('Book named "{{ book.title }}"', 'books-details', in_menu=False, in_sitetree=False), + item('Add a book', 'books-add'), + item('Edit "{{ book.title }}"', 'books-edit', in_menu=False, in_sitetree=False) + ]) + ]), + # ... You can define more than one tree for your app. + ) + + +Please see `tree` and `item` signatures for possible options. Export sitetree to DB From 4630950d1aacbd9e44d42870afb07fc4a7651177 Mon Sep 17 00:00:00 2001 From: Ben Cole Date: Tue, 28 Apr 2015 15:42:28 +0100 Subject: [PATCH 3/5] Added documentation regarding custom models and extra fields --- docs/source/apps.rst | 5 +++++ docs/source/models.rst | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/source/apps.rst b/docs/source/apps.rst index 7045b6b8..db264eca 100644 --- a/docs/source/apps.rst +++ b/docs/source/apps.rst @@ -35,6 +35,11 @@ Let's suppose you have `books` application and want do define a sitetree for it. Please see `tree` and `item` signatures for possible options. +.. note:: + + If you added extra fields to the Tree and TreeItem models, + then you can specify their values when instantiating `item` see :ref:`custom-model-sitetree` + Export sitetree to DB --------------------- diff --git a/docs/source/models.rst b/docs/source/models.rst index b3165f88..023da581 100644 --- a/docs/source/models.rst +++ b/docs/source/models.rst @@ -31,7 +31,7 @@ and `TreeItemBase` classes respectively: class MyTreeItem(TreeItemBase): """And that's a tree item model with additional `css_class` field.""" - css_class = models.IntegerField('Tree item CSS class', default=50) + css_class = models.CharField('Tree item CSS class', max_length=50) @@ -57,7 +57,40 @@ to instruct Django to use them for your project instead of built-in ones: See :ref:`Overriding SiteTree Admin representation ` for more information. +.. _custom-model-sitetree: +Sitetree definition with custom models +-------------------------------------- + +Given the example model given above, you can now use the extra fields when defining a sitetree programmatically: + +.. code-block:: python + + from sitetree.utils import tree, item + + # Be sure you defined `sitetrees` in your module. + sitetrees = ( + # Define a tree with `tree` function. + tree('books', items=[ + # Then define items and their children with `item` function. + item('Books', 'books-listing', children=[ + item('Book named "{{ book.title }}"', + 'books-details', + in_menu=False, + in_sitetree=False, + css_class='book-detail'), + item('Add a book', + 'books-add', + css_class='book-add'), + item('Edit "{{ book.title }}"', + 'books-edit', + in_menu=False, + in_sitetree=False, + css_class='book-edit') + ]) + ]), + # ... You can define more than one tree for your app. + ) .. _models_referencing: From 63ebd324fbcc9ac4414538b5fe914cbf28e7ee81 Mon Sep 17 00:00:00 2001 From: Ben Cole Date: Tue, 28 Apr 2015 15:42:39 +0100 Subject: [PATCH 4/5] Ignore docs/build --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f295052b..ce355073 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .idea .tox django_sitetree.egg-info +docs/build From bae87495e94edbf20a3eb7f3930e6d0954be914c Mon Sep 17 00:00:00 2001 From: Ben Cole Date: Tue, 28 Apr 2015 16:20:47 +0100 Subject: [PATCH 5/5] Smoke test for new kwargs parameter for utils.item() --- sitetree/tests.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sitetree/tests.py b/sitetree/tests.py index 2ddcd768..e6626d02 100644 --- a/sitetree/tests.py +++ b/sitetree/tests.py @@ -606,8 +606,9 @@ def basic_test(self, new_style=False, reset_cache=False): trees = ( compose_dynamic_tree(( tree('dynamic_main_root', items=( - item('dynamic_main_root_1', 'dynamic_main_root_1_url', url_as_pattern=False), - item('dynamic_main_root_2', 'dynamic_main_root_2_url', url_as_pattern=False), + # Testing kwargs parameter for item(). Needs a full proper test + item('dynamic_main_root_1', 'dynamic_main_root_1_url', url_as_pattern=False, sort_order=2), + item('dynamic_main_root_2', 'dynamic_main_root_2_url', url_as_pattern=False, sort_order=1), )), ), target_tree_alias='main'), compose_dynamic_tree((