|
| 1 | +# crumbles |
| 2 | +A simple library for class based view breadcrumbs for any python web framework. |
| 3 | + |
| 4 | +[](https://github.com/maerteijn/crumbles/actions/workflows/ci.yml) |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +[](https://github.com/astral-sh/ruff) |
| 12 | +[](https://mypy-lang.org/) |
| 13 | + |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +First define a specific mixin for your framework. For example Django: |
| 18 | +```python |
| 19 | +from django.urls import reverse |
| 20 | + |
| 21 | + |
| 22 | +class DjangoCrumblesViewMixin(CrumblesViewMixin): |
| 23 | + def url_resolve(self, *args, **kwargs): |
| 24 | + return reverse(*args, **kwargs) |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | +Use the Mixin in your View classes and define the breadcrumbs: |
| 29 | +```python |
| 30 | +class MyListView(View, DjangoCrumblesViewMixin): |
| 31 | + crumbles = ( |
| 32 | + CrumbleDefinition(url_name="my-list-view", title="Home"), |
| 33 | + ) |
| 34 | +``` |
| 35 | + |
| 36 | +Add a default breadcrumb to the mixin like so: |
| 37 | +```python |
| 38 | +class DjangoCrumblesViewMixin(CrumblesViewMixin): |
| 39 | + def __init__(self): |
| 40 | + self.crumbles = ( |
| 41 | + CrumbleDefinition(url_name="index", title="Home"), |
| 42 | + ) + type(self).crumbles |
| 43 | + |
| 44 | + def url_resolve(self, *args, **kwargs): |
| 45 | + return reverse(*args, **kwargs) |
| 46 | + |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +You can reuse "parent" crumbles for detail pages and use a callable for |
| 52 | +url resolve arguments or for defining the title: |
| 53 | +```python |
| 54 | +from operator import attrgetter, methodcaller |
| 55 | + |
| 56 | + |
| 57 | +class MyDetailView(View, DjangoCrumblesViewMixin): |
| 58 | + crumbles = MyListView.crumbles + ( |
| 59 | + CrumbleDefinition( |
| 60 | + url_name="my-detail-view", |
| 61 | + url_resolve_kwargs={"pk": attrgetter("pk")}, |
| 62 | + title=methodcaller("__str__"), |
| 63 | + ), |
| 64 | + ) |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +The attributes context will be retrieved from `instance.object`. If this is not available it |
| 69 | +will default to the View class instance itself. You can set a custom context variable |
| 70 | +(including dotted syntakt) by using `crumbles_context_attribute`: |
| 71 | + |
| 72 | +```python |
| 73 | +class MyDetailView(View, DjangoCrumblesViewMixin): |
| 74 | + crumbles_context_attribute = "my_object.parent" |
| 75 | + ... |
| 76 | +``` |
| 77 | + |
| 78 | +Rendering the breadcrumb should be done in a template. An example for Django (with bootstrap): |
| 79 | +```jinja2 |
| 80 | +<nav aria-label="breadcrumb"> |
| 81 | + <ol class="breadcrumb"> |
| 82 | + {% for item in view.resolve_crumbles %} |
| 83 | + {% if forloop.last %} |
| 84 | + <li class="breadcrumb-item active" aria-current="page">{{ item.title }}</li> |
| 85 | + {% else %} |
| 86 | + <li class="breadcrumb-item"> |
| 87 | + {% if item.url %} |
| 88 | + <a href="{{ item.url }}">{{ item.title}}</a> |
| 89 | + {% else %} |
| 90 | + {{ item.title }} |
| 91 | + {% endif %} |
| 92 | + </li> |
| 93 | + {% endif %} |
| 94 | + {% endfor %} |
| 95 | + </ol> |
| 96 | +</nav> |
| 97 | +``` |
| 98 | + |
| 99 | +## Development setup |
| 100 | + |
| 101 | +### First clone this repository |
| 102 | +``` |
| 103 | +git clone https://github.com/maerteijn/crumbles.git |
| 104 | +``` |
| 105 | + |
| 106 | +### Install the python project |
| 107 | +```bash |
| 108 | +pyenv virtualenv crumbles # or your alternative to create a venv |
| 109 | +pyenv activate crumbles |
| 110 | +make install |
| 111 | +``` |
| 112 | + |
| 113 | +### Linting |
| 114 | +`ruff` and `mypy` are installed and configured |
| 115 | +```bash |
| 116 | +make lint |
| 117 | +``` |
| 118 | + |
| 119 | +### Formatting |
| 120 | + |
| 121 | +`ruff` are configured |
| 122 | +```bash |
| 123 | +make format |
| 124 | +``` |
| 125 | + |
| 126 | +### Test |
| 127 | + |
| 128 | +Pytest with coverage is default enabled |
| 129 | +```bash |
| 130 | +make cov |
| 131 | +``` |
0 commit comments