Skip to content

Commit

Permalink
Merge branch 'release-2.1.4'
Browse files Browse the repository at this point in the history
closes #143
  • Loading branch information
abidibo committed Mar 31, 2021
2 parents 91289d9 + 25a6617 commit 935a0f0
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 8 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Login with user `demo` and password `demo`
- [Menu](#configuration-menu)
- [Search Field](#configuration-search-field)
- [Analytics](#configuration-analytics)
- [Page Detection](#page-detection)
- [Signals](#signals)
- [Js Utilities](#js-utilities)
- [Js Translations](#js-translations)
Expand Down Expand Up @@ -336,6 +337,33 @@ Once the service account is created, you can click the Generate New JSON Key but

Add the service account as a user in Google Analytics. The service account you created in the previous step has an email address that you can add to any of the Google Analytics views you'd like to request the data from. It's generally best to only grant the service account read-only access.

## <a name="page-detection"></a>Page Detection

Baton triggers some of its functionalities basing upon the current page. For example, it will trigger the tab functionality only when the current page is an add form or change form page.

Baton understands which page is currently displayed performing some basic regular expressions against the location pathname.
There may be cases in which you'd like to serve such contents at different and custom urls, in such cases you need a way to tell Baton which kind of page is tied to that url.

For this reason you can inject your custom hook, a javascript function which should return the page type and that receives as first argument the Baton's default function to use as fallback, i.e.

``` html
<!-- admin/base_site.html -->
<script>
(function ($, undefined) {
$(document).ready(function () {
Baton.detectPageHook = fn => /newschange/.test(location.pathname) ? 'change_form' : fn()
Baton.init(JSON.parse(document.getElementById('baton-config').textContent));
})
})(jQuery, undefined)
</script>
```

In this case we tell Baton that when the location pathname includes the string `newschange`, then the page should be considered a `change_form`, otherwise we let Baton guess the page type.

So, in order to hook into the Baton page detection system, just define a `Baton.detectPageHook` function which receives the default function as first argument and should return the page type.

The available page types are the following: `dashboard`, `admindocs`, `login`, `logout`, `passowrd_change`, `password_change_success`, `add_form`, `change_form`, `changelist`, `filer`, `default`.

## <a name="signals"></a>Signals

Baton provides a dispatcher that can be used to register function that will be called when some events occurr.
Expand Down
2 changes: 1 addition & 1 deletion baton/static/baton/app/dist/baton.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion baton/static/baton/app/dist/baton.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion baton/static/baton/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion baton/static/baton/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "baton",
"version": "2.1.3",
"version": "2.1.4",
"description": "Django Baton App",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion baton/static/baton/app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ window.Baton = {
init: function (config) {
console.info('Baton:', 'init')
this.initialized = true
let page = this.page()
let page = this.detectPageHook ? this.detectPageHook(this.page) : this.page()
$('body').addClass('page-' + page)

// toasts
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
# built documents.
#
# The short X.Y version.
version = u'2.1.3'
version = u'2.1.4'
# The full version, including alpha/beta/rc tags.
release = u'2.1.3'
release = u'2.1.4'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Getting started

installation
configuration
page_detection
signals
js_utilities
js_translations
Expand Down
25 changes: 25 additions & 0 deletions docs/page_detection.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Page Detection
==============

Baton triggers some of its functionalities basing upon the current page. For example, it will trigger the tab functionality only when the current page is an add form or change form page.

Baton understands which page is currently displayed performing some basic regular expressions against the location pathname.
There may be cases in which you'd like to serve such contents at different and custom urls, in such cases you need a way to tell Baton which kind of page is tied to that url.

For this reason you can inject your custom hook, a javascript function which should return the page type and that receives as first argument the Baton's default function to use as fallback, i.e. ::

<!-- admin/base_site.html -->
<script>
(function ($, undefined) {
$(document).ready(function () {
Baton.detectPageHook = fn => /newschange/.test(location.pathname) ? 'change_form' : fn()
Baton.init(JSON.parse(document.getElementById('baton-config').textContent));
})
})(jQuery, undefined)
</script>

In this case we tell Baton that when the location pathname includes the string ``newschange``, then the page should be considered a ``change_form``, otherwise we let Baton guess the page type.

So, in order to hook into the Baton page detection system, just define a ``Baton.detectPageHook`` function which receives the default function as first argument and should return the page type.

The available page types are the following: ``dashboard``, ``admindocs``, ``login``, ``logout``, ``passowrd_change``, ``password_change_success``, ``add_form``, ``change_form``, ``changelist``, ``filer``, ``default``.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='django-baton',
version='2.1.3',
version='2.1.4',
packages=['baton', 'baton.autodiscover', 'baton.templatetags'],
include_package_data=True,
license='MIT License',
Expand Down
5 changes: 5 additions & 0 deletions testapp/app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@
'label': 'Google search',
'url': 'http://www.google.it'
},
# {
# 'type': 'free',
# 'label': 'Dalla change form',
# 'url': '/admin/newschange/3'
# },
]
},
),
Expand Down
2 changes: 2 additions & 0 deletions testapp/app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
from django.views import static
from django.contrib.staticfiles.views import serve
from app.views import admin_search
from news.views import news_change_view

urlpatterns = [
path('admin/doc/', include('django.contrib.admindocs.urls')),
path('admin/search/', admin_search),
# path('admin/newschange/<int:id>', news_change_view),
path('admin/', admin.site.urls),
path('baton/', include('baton.urls')),
path('tinymce/', include('tinymce.urls')),
Expand Down
Binary file modified testapp/app/db.sqlite3
Binary file not shown.
6 changes: 6 additions & 0 deletions testapp/app/news/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from django.shortcuts import render
from .admin import NewsAdmin
from .models import News
from baton.autodiscover import admin

# Create your views here.

def news_change_view(request, id):
return NewsAdmin(News, admin.site).change_view(request, str(id))

0 comments on commit 935a0f0

Please sign in to comment.