Skip to content

Commit

Permalink
The code now works.
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonLinks committed Jan 6, 2018
1 parent 40d5162 commit 030c1bf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
Breadcrumbs
==========

Breadcrumbs are a way to display one's location in a hierarchical object model.
Breadcrumbs are a way to display one's location in a hierarchical object model. Click on the appropriate crumb, and the browser will take you to
the page for that parent object.

Github uses them at the top of each page, to let you know where you are.
Github uses them at the top of each page, to let you know where you are.

Many ZODB applications also use them.
This package displays bredcrumbs.

This package displays them.
Many ZODB applications also use them. WHile github just has a
unix directory behind each breadcrumb, with the ZODB you can
have different views of the parent objects, typically the '/index' and the
'/manage' view. So these breadcrumbs are a bit more sophisticated than
the typical github breadcrumbs.

The simplest way to use breadcrumbs is as follows:

```
from dolmen.breadcrumbs import basicBreadCrumbs
print (basicBreadCrumbs(item,request))
from dolmen.breadcrumbs import defaultBreadCrumbs
print (defaultBreadCrumbs(item,request))
```

In practice you will want to use breadcrumbs in your
Expand All @@ -23,7 +28,7 @@ web pages, and so you add them to your Page subclasses
from dolmen.breadcrumbs import basicBreadCrumbs
class MyPage(Page):
define breadcrumbs():
return basicBreadCrumbs(self.context,self.request)
return defaultBreadCrumbs(self.context,self.request)
```

Then in the Page Template just call them.
Expand All @@ -35,21 +40,22 @@ Of course if you want to customize it, you can. Let us
explore how they work.

Dolmen.Breadcrumbs assumes that there is a root object which
implements IPublicationRoot. It gets the paretns of an object (kin),
implements IPublicationRoot. It gets the parents of an object,
using dolmen.location, and then genates a list of dictionaires with the
information for each item. Finally it converst that list into html.

In [dolmen.breadcrumbs/src/dolmen/breadcrumbs/crumbs.py](./src/dolmen/breadcrumbs/crumbs.py])
There are two methods.

1.resolve_name() returns the __name__ of the object twice,or it throws an exception.
Of course for IPulicationRoot, if there is no __name__, it can return None.
You could also write a resolver which returns (__name__, title)
1.resolve_name() returns a (name, title) pair. Some objects just have a name,
so they get back (name, name). The root object IPublicationRoot,
may not even havea name, so
it returns (none, none) or (none, title).

2. breadcrumbs(item, request, resolver=resolve_name) is an iterator which
returns the data structures required to build breadcrumbs. It gets the
items parents, up to IPublicatinRoot, reverses them, and yeilds a dictinary with
the name and url of the crumb.
items parents, up to IPublicatinRoot, reverses them, and yeilds a dictinary
with the name and url of the crumb.

In [dolmen.breadcrumbs/src/dolmen/breadcrumbs/renderer.py](./src/dolmen/breadcrumbs/renderer.py) You will find two different methods. breadcrumbsrenderer() calls
render_breadcrums which calls the
Expand Down
9 changes: 5 additions & 4 deletions src/dolmen/breadcrumbs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from dolmen.breadcrumbs.crumbs import breadcrumbs
from dolmen.breadcrumbs.renderer import BreadcrumbsRenderer

def BasicBreadCrumbs(item,request):
crumbs=BreadCrynbsRender()
crumbs.breadcrums=breadcrumbs(item,request)
def defaultBreadcrumbs(item,request):
#import pdb; pdb.set_trace()
crumbs=BreadcrumbsRenderer(item,request,viewName='')
crumbs.update()
return crumbs.render()


31 changes: 19 additions & 12 deletions src/dolmen/breadcrumbs/crumbs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-

import urllib
from cromlech.browser import IPublicationRoot
from dolmen.location import get_absolute_url, lineage_chain

try:
from urllib import quote # Python 2.X
except ImportError:
from urllib.parse import quote # Python 3+

_safe = '@+' # Characters that we don't want to have quoted

Expand All @@ -17,24 +20,28 @@ def resolve_name(item):

if name is None and not IPublicationRoot.providedBy(item):
raise KeyError('Object name (%r) could not be resolved.' % item)
if (title != None)

if (title != None):
return name, title
return name, name



def breadcrumbs(item, request, viewName='index, 'resolver=resolve_name):
def breadcrumbs(item, request, viewName='', resolver=resolve_name):
#IF YOU WANT A SPECFIC VIEWNAME, THEN PREPEND A SLASH
if viewName != '':
viewName ='/' + viewName
if resolver is None:
resolver = resolve_name
kin = lineage_chain(item)
if kin:
kin.reverse()
root = kin.pop(0)
base_url = get_absolute_url(root, request)
parents = lineage_chain(item)
if parents:
parents.reverse()
root = parents.pop(0)
#base_url = get_absolute_url(root, request)
base_url="pythonLinks.info:8081"
name, title = resolver(root)
yield {'name': title, 'url': base_url + viewName}

for sibling in kin:
name, title = resolver(sibling)
base_url += '/' + urllib.quote(name.encode('utf-8'), _safe)
for ancestor in parents:
name, title = resolver(ancestor)
base_url += '/' + quote(name.encode('utf-8'), _safe)
yield {'name': title, 'url': base_url+ viewName}
12 changes: 7 additions & 5 deletions src/dolmen/breadcrumbs/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@
template = TALTemplate(path.join(TEMPLATES_DIR, 'breadcrumbs.pt'))


def render_breadcrumbs(renderer, crumbs, separator="→"):
def render_breadcrumbs(renderer, crumbs, viewName='', separator="→"):
namespace = dict(
breadcrumbs=crumbs,
viewName=crums.viewName,
target_language=ILanguage(renderer.request, None),
viewName=viewName,
target_language='en',
separator=separator)
return template.render(renderer, **namespace)
# target_language=ILanguage(renderer.request, None),
#WHAT SHOULD I HAVE DONE HERE?


@implementer(IRenderable)
class BreadcrumbsRenderer(object):

resolver = None

def __init__(self, context, request, viewName='index'):
def __init__(self, context, request, viewName=''):
self.context = context
self.request = request
self.viewName = viewName

def update(self):
self.breadcrumbs = list(
breadcrumbs(self.context, self.request, self.resolver))
breadcrumbs(self.context, self.request, self.viewName, self.resolver))

def render(self):
return render_breadcrumbs(self, self.breadcrumbs)
2 changes: 1 addition & 1 deletion src/dolmen/breadcrumbs/templates/breadcrumbs.pt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<tal:loop repeat="crumb breadcrumbs">
<span class="crumb" tal:condition="crumb['name']">
<a href=""
tal:attributes="href crumb['url'+'/' + crumb['viewName']]"
tal:attributes="href crumb['url']"
tal:content="crumb['name']">name</a>
<span class="divider"
tal:content="structure separator"
Expand Down

0 comments on commit 030c1bf

Please sign in to comment.