Skip to content

Commit

Permalink
Added Sphinx documentation!! and made a few code moves.
Browse files Browse the repository at this point in the history
- Moved `parler.appsettings.add_default_language_settings()` to `parler.utils.conf`
  (old import is still there).
- Moved `parler.admin.BaseInlineFormSet` to `parler.forms`.
  • Loading branch information
vdboor committed Jul 7, 2014
1 parent 0abc96f commit 400c99c
Show file tree
Hide file tree
Showing 46 changed files with 2,293 additions and 179 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Changelog
=========

Changes in version 1.0 (in development)
---------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ The translated model can be constructed manually too::
return self.title


class MyModel_Translations(TranslatedFieldsModel):
class MyModelTranslation(TranslatedFieldsModel):
master = models.ForeignKey(MyModel, related_name='translations', null=True)
title = models.CharField(_("Title"), max_length=200)

Expand Down Expand Up @@ -282,13 +282,13 @@ without loosing the freedom of manually using the API at your will.
TODO
====

* Documentation on RTD.
* Unittest the admin.
* ``ModelAdmin.prepopulated_fields`` doesn't work yet (you can use ``get_prepopulated_fields()`` as workaround).
* The list code currently performs one query per object. This needs to be reduced.
* Preferably, the ``TranslatedField`` proxy on the model should behave like a ``RelatedField``,
if that would nicely with the ORM too.

Please contribute your improvements or work on these area's!


Django compatibility
====================
Expand Down
177 changes: 177 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build

# User-friendly check for sphinx-build
ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/)
endif

# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .

.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext

help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " xml to make Docutils-native XML files"
@echo " pseudoxml to make pseudoxml-XML files for display purposes"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"

clean:
rm -rf $(BUILDDIR)/*

html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."

singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."

pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."

json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."

htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."

qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/django-parler.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/django-parler.qhc"

devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/django-parler"
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/django-parler"
@echo "# devhelp"

epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."

latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)."

latexpdf:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."

latexpdfja:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through platex and dvipdfmx..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."

text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo
@echo "Build finished. The text files are in $(BUILDDIR)/text."

man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."

texinfo:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
@echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)."

info:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."

gettext:
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."

changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."

linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."

doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."

xml:
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
@echo
@echo "Build finished. The XML files are in $(BUILDDIR)/xml."

pseudoxml:
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
@echo
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
Empty file.
2 changes: 2 additions & 0 deletions docs/_ext/djangodummy/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# for readthedocs
Django==1.6.1
13 changes: 13 additions & 0 deletions docs/_ext/djangodummy/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Settings file to allow parsing API documentation of Django modules,
# and provide defaults to use in the documentation.
#
# This file is placed in a subdirectory,
# so the docs root won't be detected by find_packages()

# Display sane URLs in the docs:
STATIC_URL = '/static/'

# Avoid error for missing the secret key
SECRET_KEY = 'docs'

SITE_ID = None
Empty file added docs/_ext/djangoext/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions docs/_ext/djangoext/roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Allow :django:setting:`SITE_ID` to work.

def setup(app):
app.add_crossref_type(
directivename = "setting",
rolename = "setting",
indextemplate = "pair: %s; setting",
)
121 changes: 121 additions & 0 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
Advanced usage patterns
=======================

Translations without fallback languages
---------------------------------------

When a translation is missing, the fallback language is used.
However, when an object has no fallback language, this still fails.

There are a few solutions to this problem:

1. Declare the translated attribute explicitly with ``any_language=True``::

from parler.models import TranslatableModel
from parler.fields import TranslatedField

class MyModel(TranslatableModel):
title = TranslatedField(any_language=True)

Now, the title will try to fetch one of the existing languages from the database.

2. Use :func:`~parler.models.TranslatableModel.safe_translation_getter` on attributes
which don't have an ``any_language=True`` setting. For example::

model.safe_translation_getter("fieldname", any_language=True)

3. Catch the :class:`~parler.models.TranslationDoesNotExist` exception. For example::

try:
return object.title
except TranslationDoesNotExist:
return ''

Because this exception inherits from :class:`~exceptions.AttributeError`,
templates already display empty values by default.

4. Avoid fetching untranslated objects using queryset methods. For example::

queryset.active_translations()

Which is almost identical to::

codes = get_active_language_choices()
queryset.filter(translations__language_code__in=codes).distinct()

Note that the same `ORM restrictions <https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships>`_ apply here.


Adding translated fields to an existing model:
----------------------------------------------

Create a proxy class::

from django.contrib.sites.models import Site
from parler.models import TranslatableModel, TranslatedFields


class TranslatableSite(TranslatableModel, Site):
class Meta:
proxy = True

translations = TranslatedFields()


And update the admin::

from django.contrib.sites.admin import SiteAdmin
from django.contrib.sites.models import Site
from parler.admin import TranslatableAdmin, TranslatableStackedInline


class NewSiteAdmin(TranslatableAdmin, SiteAdmin):
pass

admin.site.unregister(Site)
admin.site.register(TranslatableSite, NewSiteAdmin)


Disabling caching
-----------------

If desired, caching of translated fields can be disabled
by adding :ref:`PARLER_ENABLE_CACHING = False <PARLER_ENABLE_CACHING>` to the settings.

.. _custom-language-settings:

Customizing language settings
-----------------------------

If needed, projects can "fork" the parler language settings.
This is rarely needed. Example::

from django.conf import settings
from parler import appsettings as parler_appsettings
from parler.utils import normalize_language_code, is_supported_django_language
from parler.utils.conf import add_default_language_settings

MYCMS_DEFAULT_LANGUAGE_CODE = getattr(settings, 'MYCMS_DEFAULT_LANGUAGE_CODE', FLUENT_DEFAULT_LANGUAGE_CODE)
MYCMS_LANGUAGES = getattr(settings, 'MYCMS_LANGUAGES', parler_appsettings.PARLER_LANGUAGES)

MYCMS_DEFAULT_LANGUAGE_CODE = normalize_language_code(MYCMS_DEFAULT_LANGUAGE_CODE)

MYCMS_LANGUAGES = add_default_language_settings(
MYCMS_LANGUAGES, 'MYCMS_LANGUAGES',
hide_untranslated=False,
hide_untranslated_menu_items=False,
code=MYCMS_DEFAULT_LANGUAGE_CODE,
fallback=MYCMS_DEFAULT_LANGUAGE_CODE
)

Instead of using the functions from :mod:`parler.utils` (such as :func:`~parler.utils.get_active_language_choices`)
the project can access the language settings using::

MYCMS_LANGUAGES.get_language()
MYCMS_LANGUAGES.get_active_choices()
MYCMS_LANGUAGES.get_fallback_language()
MYCMS_LANGUAGES.get_default_language()
MYCMS_LANGUAGES.get_first_language()

These methods are added by the :func:`~parler.utils.conf.add_default_language_settings` function.
See the :class:`~parler.utils.conf.LanguagesSetting` class for details.
20 changes: 20 additions & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. _api:

API documentation
=================

.. toctree::
:maxdepth: 2

parler
parler.admin
parler.cache
parler.fields
parler.forms
parler.managers
parler.models
parler.signals
parler.utils
parler.utils.conf
parler.utils.context
parler.views
Loading

0 comments on commit 400c99c

Please sign in to comment.