Source code for jmb.core.monkey

"""
.. _monkey-patch:

============
Monkey Patch
============


Jumbo relies on some patches to the official django version that can be applied using the
function :func:`jmb.core.monkey.patch`. Before django 1.7 we used to monkey patch in ``settings``
and in ``urls.py``. In Dj1.7 AppConfig is out preferred place.

There's no needed settings to make it work apart from putting ``jmb.core`` soon enought in the
list of ``INSTALLED_APPS``.

If you want to change which patches are applied you can use

:JMB_MONKEY_PATCH_INCLUDE:  a list of all patch names to be applied

:JMB_MONKEY_PATCH_EXCLUDE: a list of all patch names to be excluded

Available patches that you may be willing to disable are:

:|namespace|: needed to make package namespace be visible

:|widgets|: better time, date, datetime picker for the admin

:|jquery|: prevents cms to replace stock jQuery with it's own

:|form_errors|: allows to print in console errors of a form

:|get_queryset|: needed for portability between old and new versions.

.. autofunction:: patch

.. automodule:: jmb.core.monkey.management

.. automodule:: jmb.core.monkey.conf

.. automodule:: jmb.core.monkey.errors_in_forms

.. automodule:: jmb.core.monkey.portability

.. .. automodule:: jmb.core.monkey.cms_plugins  # Non riesco a farlo compilare


.. _timepicker_patch:

timepicker
==========

Datetime and time widgets in Django are pretty poor ones.

Html widgets used by admin are defined in ``django.contrib.admin.options``
in a dict named ``FORMFIELD_FOR_DBFIELD_DEFAULTS``.

We overwrite it in ``jmb.core.admin.options`` and define a widget that is
derived from ``jQuery.ui``'s default one.

.. |namespace| replace:: :ref:`namespaces <namespaces>`
.. |widgets| replace:: :ref:`timepicker <timepicker_patch>`
.. |jquery| replace:: :ref:`jquery_cms <jquery_cms>`
.. |form_errors| replace:: :ref:`form-errors <monkey-errors>`
.. |get_queryset| replace:: :ref:`get_queryset <get_queryset>`

"""
from __future__ import unicode_literals

from django.conf import settings

from .management import fix_find_namespaces
from .conf import patch_cms_page_not_to_overwrite_jquery
from .errors_in_forms import fix_error_list
from .portability import fix_query_set_name

MONKEY_PATCHES = ['namespaces', 'get_queryset', 'widgets', 'jquery', 'form_errors', ]

def patch_widgets_mapping():
    """
    Modify options.FORMFIELD_FOR_DBFIELD_DEFAULTS to use :ref:`timepicker`
    """

    from jmb.core.forms import widgets
    from django.contrib.admin import options
    from django.db import models

    options.FORMFIELD_FOR_DBFIELD_DEFAULTS[models.DateTimeField] = {
        'widget': widgets.DateTimePicker}
    options.FORMFIELD_FOR_DBFIELD_DEFAULTS[models.TimeField] = {
        'widget': widgets.TimePicker}
    options.FORMFIELD_FOR_DBFIELD_DEFAULTS[models.DateField] = {
        'widget': widgets.DatePicker}


def patch_cms_plugin_pool():
    """
    Substitute cms.plugin_pool.plugin_pool so as to understand jumbo dir setup
    """
    return
    ## Prevent to import if not needed. Jmb.core must work also w/o cms
    from . import cms_plugins
    from cms import plugin_pool

    plugin_pool.plugin_pool = cms_plugins.JmbPluginPool()


[docs]def patch(include=None, exclude=None): """ Apply all patches Jumbo relyes on. Currently :ref:`namespaces`, :ref:`jquery <jquery_cms>`, and :ref:`timepicker <timepicker_patch>` :arg include: a list of strings naming the patches that will be applied (optional). :arg exclude: a list of strings naming the patches that will not be applied. 'jquery' patch will only be applied if ``cms`` is in ``INSTALLED_APPS`` """ PATCH_NAMES = include or [item for item in MONKEY_PATCHES if not item in (exclude or [])] for item in PATCH_NAMES: if item == 'namespaces': fix_find_namespaces() if item == 'get_queryset': fix_query_set_name() if item == 'widgets': patch_widgets_mapping() if item == 'jquery' and 'cms' in settings.INSTALLED_APPS: patch_cms_page_not_to_overwrite_jquery() if item == 'cms.plugins' and 'cms' in settings.INSTALLED_APPS: patch_cms_plugin_pool() if item == 'form_errors': fix_error_list()