From 5687d9836d236fc597469f12d748de17b72b7d3a Mon Sep 17 00:00:00 2001 From: Eran Rundstein Date: Thu, 6 Jun 2013 13:24:43 +0000 Subject: [PATCH 01/33] create basic failing test case for inherited models and tracker --- model_utils/tests/models.py | 7 +++++++ model_utils/tests/tests.py | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/model_utils/tests/models.py b/model_utils/tests/models.py index 99d33ad..06b3dea 100644 --- a/model_utils/tests/models.py +++ b/model_utils/tests/models.py @@ -271,6 +271,10 @@ class TrackedMultiple(models.Model): number_tracker = FieldTracker(fields=['number']) +class InheritedTracked(Tracked): + name2 = models.CharField(max_length=20) + + class ModelTracked(models.Model): name = models.CharField(max_length=20) number = models.IntegerField() @@ -300,6 +304,9 @@ class ModelTrackedMultiple(models.Model): name_tracker = ModelTracker(fields=['name']) number_tracker = ModelTracker(fields=['number']) +class InheritedModelTracked(ModelTracked): + name2 = models.CharField(max_length=20) + class StatusFieldDefaultFilled(models.Model): STATUS = Choices((0, "no", "No"), (1, "yes", "Yes")) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index 78874d3..9c427bc 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -21,9 +21,9 @@ from model_utils.tests.models import ( InheritanceManagerTestChild2, TimeStamp, Post, Article, Status, StatusPlainTuple, TimeFrame, Monitored, StatusManagerAdded, TimeFrameManagerAdded, Dude, SplitFieldAbstractParent, Car, Spot, - ModelTracked, ModelTrackedFK, ModelTrackedNotDefault, ModelTrackedMultiple, + ModelTracked, ModelTrackedFK, ModelTrackedNotDefault, ModelTrackedMultiple, InheritedModelTracked, Tracked, TrackedFK, TrackedNotDefault, TrackedNonFieldAttr, - TrackedMultiple, StatusFieldDefaultFilled, StatusFieldDefaultNotFilled) + TrackedMultiple, InheritedTracked, StatusFieldDefaultFilled, StatusFieldDefaultNotFilled) @@ -1074,6 +1074,11 @@ class FieldTrackerForeignKeyTests(FieldTrackerTestCase): self.assertCurrent(fk=self.instance.fk_id) +# TODO test stuff with name2 +class InheritedFieldTrackerTests(FieldTrackerTests): + + tracked_class = InheritedTracked + class ModelTrackerTests(FieldTrackerTests): tracked_class = ModelTracked @@ -1197,3 +1202,9 @@ class ModelTrackerForeignKeyTests(FieldTrackerForeignKeyTests): self.assertChanged(fk=self.old_fk) self.assertPrevious(fk=self.old_fk) self.assertCurrent(fk=self.instance.fk) + + +# TODO test stuff with name2 +class InheritanceModelTrackerTests(ModelTrackerTests): + + tracked_class = InheritedModelTracked \ No newline at end of file From 6aa6ba0667f49dea0e71d4ec1cca93d3d67f302c Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Mon, 20 May 2013 22:58:08 -0700 Subject: [PATCH 02/33] Add a CONTRIBUTING file --- CONTRIBUTING.rst | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 CONTRIBUTING.rst diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst new file mode 100644 index 0000000..f11939b --- /dev/null +++ b/CONTRIBUTING.rst @@ -0,0 +1,45 @@ +Contributing +============ + +Below is a list of tips for submitting issues and pull requests. These are +suggestions and not requirements. + +Submitting Issues +----------------- + +Issues are often easier to reproduce/resolve when they have: + +- A pull request with a failing test demonstrating the issue +- A code example that produces the issue consistently +- A traceback or an guest guest as to where the problem may be occurring + +Pull Requests +------------- + +When creating a pull request, try to: + +- Write tests if applicable +- Note important changes in the `CHANGES`_ file +- Update the `README`_ file if needed +- Add yourself to the `AUTHORS`_ file + +.. _AUTHORS: AUTHORS.rst +.. _CHANGES: CHANGES.rst +.. _README: README.rst + +Testing +------- + +Please add tests for your code and ensure existing tests don't break. To run +the tests against your code:: + + python setup.py test + +Please use tox to test the code against supported Python and Django versions. +First install tox:: + + pip install tox + +To run tox and generate a coverage report (in ``htmlcov`` directory):: + + ./runtests.sh From 6716e45ccd25a2c6e93fcbb82802332571a7bdff Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Thu, 25 Jul 2013 09:45:36 -0700 Subject: [PATCH 03/33] Fixup CONTRIBUTING file per Carl's suggestions --- CONTRIBUTING.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index f11939b..70d48bb 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -11,7 +11,7 @@ Issues are often easier to reproduce/resolve when they have: - A pull request with a failing test demonstrating the issue - A code example that produces the issue consistently -- A traceback or an guest guest as to where the problem may be occurring +- A traceback (when applicable) Pull Requests ------------- @@ -43,3 +43,6 @@ First install tox:: To run tox and generate a coverage report (in ``htmlcov`` directory):: ./runtests.sh + +**Please note**: Before a pull request can be merged, all tests must pass and +code/branch coverage in tests must be 100%. From bcc8ad3a646fb8dc6e97aa759c7db95b59e2e5cc Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 25 Jul 2013 11:58:46 -0500 Subject: [PATCH 04/33] Update tox.ini for Django 1.6 stable branch, South supporting Py3. --- model_utils/tests/tests.py | 2 +- tox.ini | 110 ++++++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 44 deletions(-) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index 78874d3..c5d175d 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -593,7 +593,7 @@ if introspector: def test_no_excerpt_field_works(self): - from models import NoRendered + from .models import NoRendered self.assertRaises(FieldDoesNotExist, NoRendered._meta.get_field, '_body_excerpt') diff --git a/tox.ini b/tox.ini index a94d144..6067c98 100644 --- a/tox.ini +++ b/tox.ini @@ -1,85 +1,109 @@ [tox] envlist = - py26-1.4, py26-1.5, py26-trunk, - py27-1.4, py27-1.5, py27-trunk, py27-1.4-nosouth, - py32-1.5-nosouth, py32-trunk-nosouth, - py33-1.5-nosouth, py33-trunk-nosouth + py26-1.4, py26-1.5, py26-1.6, + py27-1.4, py27-1.5, py27-1.6, py27-trunk, py27-1.5-nosouth, + py32-1.5, py32-1.6, py32-trunk, + py33-1.5, py33-1.6, py33-trunk [testenv] deps = - South == 0.7.6 + South == 0.8.1 coverage == 3.6 commands = coverage run -a setup.py test [testenv:py26-1.4] basepython = python2.6 deps = - django == 1.4.5 + Django == 1.4.5 South == 0.7.6 coverage == 3.6 [testenv:py26-1.5] basepython = python2.6 deps = - django == 1.5.1 - South == 0.7.6 + Django == 1.5.1 + South == 0.8.1 coverage == 3.6 -[testenv:py26-trunk] +[testenv:py26-1.6] basepython = python2.6 deps = - https://github.com/django/django/tarball/master - South == 0.7.6 + https://github.com/django/django/tarball/stable/1.6.x + South == 0.8.1 coverage == 3.6 [testenv:py27-1.4] basepython = python2.7 deps = - django == 1.4.5 - South == 0.7.6 + Django == 1.4.5 + South == 0.8.1 coverage == 3.6 [testenv:py27-1.5] basepython = python2.7 deps = - django == 1.5.1 - South == 0.7.6 + Django == 1.5.1 + South == 0.8.1 + coverage == 3.6 + +[testenv:py27-1.6] +basepython = python2.7 +deps = + https://github.com/django/django/tarball/stable/1.6.x + South == 0.8.1 coverage == 3.6 [testenv:py27-trunk] basepython = python2.7 deps = https://github.com/django/django/tarball/master - South == 0.7.6 + South == 0.8.1 coverage == 3.6 -[testenv:py32-1.5-nosouth] -basepython = python3.2 -deps = - django == 1.5.1 - coverage == 3.6 - -[testenv:py32-trunk-nosouth] -basepython = python3.2 -deps = - https://github.com/django/django/tarball/master - coverage == 3.6 - -[testenv:py33-1.5-nosouth] -basepython = python3.3 -deps = - django == 1.5.1 - coverage == 3.6 - -[testenv:py33-trunk-nosouth] -basepython = python3.3 -deps = - https://github.com/django/django/tarball/master - coverage == 3.6 - - -[testenv:py27-1.4-nosouth] +[testenv:py27-1.5-nosouth] basepython = python2.7 deps = - django == 1.4.5 + Django == 1.5.1 + coverage == 3.6 + +[testenv:py32-1.5] +basepython = python3.2 +deps = + Django == 1.5.1 + South == 0.8.1 + coverage == 3.6 + +[testenv:py32-1.6] +basepython = python3.2 +deps = + https://github.com/django/django/tarball/stable/1.6.x + South == 0.8.1 + coverage == 3.6 + +[testenv:py32-trunk] +basepython = python3.2 +deps = + https://github.com/django/django/tarball/master + South == 0.8.1 + coverage == 3.6 + +[testenv:py33-1.5] +basepython = python3.3 +deps = + Django == 1.5.1 + South == 0.8.1 + coverage == 3.6 + +[testenv:py33-1.6] +basepython = python3.3 +deps = + https://github.com/django/django/tarball/stable/1.6.x + South == 0.8.1 + coverage == 3.6 + +[testenv:py33-trunk] +basepython = python3.3 +deps = + https://github.com/django/django/tarball/master + South == 0.8.1 coverage == 3.6 From eb63aca03e218c5bd75f92b8ddad4e0d83166429 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 25 Jul 2013 12:00:59 -0500 Subject: [PATCH 05/33] Update .travis.yml. --- .travis.yml | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4e22c94..cf6b244 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,13 +7,14 @@ python: env: - DJANGO=Django==1.4.5 SOUTH=1 - DJANGO=Django==1.5.1 SOUTH=1 + - DJANGO=https://github.com/django/django/tarball/stable/1.6.x SOUTH=1 - DJANGO=https://github.com/django/django/tarball/master SOUTH=1 - DJANGO=Django==1.5.1 SOUTH=0 install: - - pip install $DJANGO --use-mirrors - - pip install coverage coveralls --use-mirrors - - sh -c "if [ '$SOUTH' = '1' ]; then pip install South==0.7.6; fi" + - pip install $DJANGO + - pip install coverage coveralls + - sh -c "if [ '$SOUTH' = '1' ]; then pip install South==0.8.1; fi" script: - coverage run -a setup.py test @@ -22,12 +23,16 @@ script: matrix: include: - python: 3.2 - env: DJANGO=Django==1.5.1 SOUTH=0 + env: DJANGO=Django==1.5.1 SOUTH=1 - python: 3.2 - env: DJANGO=https://github.com/django/django/tarball/master SOUTH=0 + env: DJANGO=https://github.com/django/django/tarball/stable/1.6.x SOUTH=1 + - python: 3.2 + env: DJANGO=https://github.com/django/django/tarball/master SOUTH=1 - python: 3.3 - env: DJANGO=Django==1.5.1 SOUTH=0 + env: DJANGO=Django==1.5.1 SOUTH=1 - python: 3.3 - env: DJANGO=https://github.com/django/django/tarball/master SOUTH=0 + env: DJANGO=https://github.com/django/django/tarball/stable/1.6.x SOUTH=1 + - python: 3.3 + env: DJANGO=https://github.com/django/django/tarball/master SOUTH=1 after_success: coveralls From 78db327667685ea47399fc7bcd8138ad4c8d3556 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 25 Jul 2013 12:07:31 -0500 Subject: [PATCH 06/33] Fix .travis.yml to not run tests against Django master with Python 2.6. --- .travis.yml | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index cf6b244..02281be 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,13 +3,14 @@ language: python python: - "2.6" - "2.7" + - "3.2" + - "3.3" env: - DJANGO=Django==1.4.5 SOUTH=1 - DJANGO=Django==1.5.1 SOUTH=1 - DJANGO=https://github.com/django/django/tarball/stable/1.6.x SOUTH=1 - DJANGO=https://github.com/django/django/tarball/master SOUTH=1 - - DJANGO=Django==1.5.1 SOUTH=0 install: - pip install $DJANGO @@ -21,18 +22,15 @@ script: - coverage report matrix: + exclude: + - python: 2.6 + env: DJANGO=https://github.com/django/django/tarball/master SOUTH=1 + - python: 3.2 + env: DJANGO=Django==1.4.5 SOUTH=1 + - python: 3.3 + end: DJANGO=Django==1.4.5 SOUTH=1 include: - - python: 3.2 - env: DJANGO=Django==1.5.1 SOUTH=1 - - python: 3.2 - env: DJANGO=https://github.com/django/django/tarball/stable/1.6.x SOUTH=1 - - python: 3.2 - env: DJANGO=https://github.com/django/django/tarball/master SOUTH=1 - - python: 3.3 - env: DJANGO=Django==1.5.1 SOUTH=1 - - python: 3.3 - env: DJANGO=https://github.com/django/django/tarball/stable/1.6.x SOUTH=1 - - python: 3.3 - env: DJANGO=https://github.com/django/django/tarball/master SOUTH=1 + - python: 2.7 + env: DJANGO=Django==1.5.1 SOUTH=0 after_success: coveralls From 9a5a2bcf88c9ddf6ccf2bf90bba467726126f3d7 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 25 Jul 2013 12:16:11 -0500 Subject: [PATCH 07/33] Use proper unittest test-skipping. --- model_utils/tests/tests.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index c5d175d..f1fe411 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -1,7 +1,11 @@ from __future__ import unicode_literals -import pickle from datetime import datetime, timedelta +import pickle +try: + from unittest import skipUnless +except ImportError: # Python 2.6 + from django.utils.unittest import skipUnless import django from django.db import models @@ -583,25 +587,24 @@ try: except ImportError: introspector = None -# @@@ use skipUnless once Django 1.3 is minimum supported version -if introspector: - class SouthFreezingTests(TestCase): - def test_introspector_adds_no_excerpt_field(self): - mf = Article._meta.get_field('body') - args, kwargs = introspector(mf) - self.assertEqual(kwargs['no_excerpt_field'], 'True') +@skipUnless(introspector, 'South is not installed') +class SouthFreezingTests(TestCase): + def test_introspector_adds_no_excerpt_field(self): + mf = Article._meta.get_field('body') + args, kwargs = introspector(mf) + self.assertEqual(kwargs['no_excerpt_field'], 'True') - def test_no_excerpt_field_works(self): - from .models import NoRendered - self.assertRaises(FieldDoesNotExist, - NoRendered._meta.get_field, - '_body_excerpt') + def test_no_excerpt_field_works(self): + from .models import NoRendered + self.assertRaises(FieldDoesNotExist, + NoRendered._meta.get_field, + '_body_excerpt') - def test_status_field_no_check_for_status(self): - sf = StatusFieldDefaultFilled._meta.get_field('status') - args, kwargs = introspector(sf) - self.assertEqual(kwargs['no_check_for_status'], 'True') + def test_status_field_no_check_for_status(self): + sf = StatusFieldDefaultFilled._meta.get_field('status') + args, kwargs = introspector(sf) + self.assertEqual(kwargs['no_check_for_status'], 'True') From 0481f0aafe5a14866116a95921eca3d37b473921 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 25 Jul 2013 12:23:59 -0500 Subject: [PATCH 08/33] Don't use unnecessary quotes in .travis.yml. --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 02281be..744aec9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,10 @@ language: python python: - - "2.6" - - "2.7" - - "3.2" - - "3.3" + - 2.6 + - 2.7 + - 3.2 + - 3.3 env: - DJANGO=Django==1.4.5 SOUTH=1 From 6f1876b8de455cf8e6fa07c48a05f92617eafe05 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 25 Jul 2013 12:28:03 -0500 Subject: [PATCH 09/33] Fix typo in .travis.yml. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 744aec9..a7cf493 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ matrix: - python: 3.2 env: DJANGO=Django==1.4.5 SOUTH=1 - python: 3.3 - end: DJANGO=Django==1.4.5 SOUTH=1 + env: DJANGO=Django==1.4.5 SOUTH=1 include: - python: 2.7 env: DJANGO=Django==1.5.1 SOUTH=0 From 98f078d718aca346d26a38eb1efc79b1be3598c9 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Mon, 29 Jul 2013 12:54:49 -0700 Subject: [PATCH 10/33] Allow FieldTracker to work in child models Fixes #57. --- model_utils/tracker.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/model_utils/tracker.py b/model_utils/tracker.py index 31fd0f2..39db70c 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -70,10 +70,13 @@ class FieldTracker(object): if self.fields is None: self.fields = [field.attname for field in sender._meta.local_fields] self.field_map = self.get_field_map(sender) - models.signals.post_init.connect(self.initialize_tracker, sender=sender) + models.signals.post_init.connect(self.initialize_tracker) + self.model_class = sender setattr(sender, self.name, self) def initialize_tracker(self, sender, instance, **kwargs): + if not isinstance(instance, self.model_class): + return # Only init instances of given model (including children) tracker = self.tracker_class(instance, self.fields, self.field_map) setattr(instance, self.attname, tracker) tracker.set_saved_fields() From e79539a39c007a22d89212d368ee1766ae9d8f57 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Mon, 29 Jul 2013 12:57:02 -0700 Subject: [PATCH 11/33] Note FieldTracker fix in CHANGES --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 0b2998f..bdc0d70 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,8 @@ CHANGES master (unreleased) ------------------- +- Fixed ``FieldTracker`` usage on inherited models. Fixes GH-57. + 1.4.0 (2013.06.03) ------------------ From 75646a1874bd27c121e88ecbf4d809670920cc5d Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Tue, 30 Jul 2013 10:16:43 -0700 Subject: [PATCH 12/33] Improve FieldTracker tests for inherited models --- model_utils/tests/tests.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index 9c427bc..bc6acb9 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -1074,11 +1074,16 @@ class FieldTrackerForeignKeyTests(FieldTrackerTestCase): self.assertCurrent(fk=self.instance.fk_id) -# TODO test stuff with name2 class InheritedFieldTrackerTests(FieldTrackerTests): tracked_class = InheritedTracked + def test_child_fields_not_tracked(self): + self.name2 = 'test' + self.assertEqual(self.tracker.previous('name2'), None) + self.assertRaises(FieldError, self.tracker.has_changed, 'name2') + + class ModelTrackerTests(FieldTrackerTests): tracked_class = ModelTracked @@ -1204,7 +1209,11 @@ class ModelTrackerForeignKeyTests(FieldTrackerForeignKeyTests): self.assertCurrent(fk=self.instance.fk) -# TODO test stuff with name2 -class InheritanceModelTrackerTests(ModelTrackerTests): +class InheritedModelTrackerTests(ModelTrackerTests): - tracked_class = InheritedModelTracked \ No newline at end of file + tracked_class = InheritedModelTracked + + def test_child_fields_not_tracked(self): + self.name2 = 'test' + self.assertEqual(self.tracker.previous('name2'), None) + self.assertTrue(self.tracker.has_changed('name2')) From e84a76979bb569a9d326ebf0ff9795668ffb946f Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 31 Jul 2013 22:55:43 -0700 Subject: [PATCH 13/33] Add initial documentation (based on README) --- docs/Makefile | 177 +++++++++++++++++ docs/conf.py | 248 ++++++++++++++++++++++++ docs/index.rst | 33 ++++ docs/make.bat | 242 +++++++++++++++++++++++ docs/setup.rst | 22 +++ docs/usage.rst | 508 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1230 insertions(+) create mode 100644 docs/Makefile create mode 100644 docs/conf.py create mode 100644 docs/index.rst create mode 100644 docs/make.bat create mode 100644 docs/setup.rst create mode 100644 docs/usage.rst diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..452f59f --- /dev/null +++ b/docs/Makefile @@ -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 ' where 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-model-utils.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/django-model-utils.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-model-utils" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/django-model-utils" + @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." diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..15eaebf --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,248 @@ +# -*- coding: utf-8 -*- +# +# django-model-utils documentation build configuration file, created by +# sphinx-quickstart on Wed Jul 31 22:27:07 2013. +# +# This file is execfile()d with the current directory set to its containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys, os + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +#sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ----------------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be extensions +# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = [] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'django-model-utils' +copyright = u'2013, Carl Meyer' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '1.4' +# The full version, including alpha/beta/rc tags. +release = '1.4.0' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +#language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +#keep_warnings = False + + +# -- Options for HTML output --------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'default' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'django-model-utilsdoc' + + +# -- Options for LaTeX output -------------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +#'preamble': '', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass [howto/manual]). +latex_documents = [ + ('index', 'django-model-utils.tex', u'django-model-utils Documentation', + u'Carl Meyer', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output -------------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'django-model-utils', u'django-model-utils Documentation', + [u'Carl Meyer'], 1) +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------------ + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'django-model-utils', u'django-model-utils Documentation', + u'Carl Meyer', 'django-model-utils', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +#texinfo_no_detailmenu = False diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..2dd94ed --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,33 @@ +================== +django-model-utils +================== + +Django model mixins and utilities. + +Contents +======== + +.. toctree:: + :maxdepth: 3 + + setup + usage + +Contributing +============ + +Please file bugs and send pull requests to the `GitHub repository`_ and `issue +tracker`_. + +.. _GitHub repository: https://github.com/carljm/django-model-utils/ +.. _issue tracker: https://github.com/carljm/django-model-utils/issues + + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` + diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..fb1a0ef --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,242 @@ +@ECHO OFF + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set BUILDDIR=_build +set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . +set I18NSPHINXOPTS=%SPHINXOPTS% . +if NOT "%PAPER%" == "" ( + set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% + set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% +) + +if "%1" == "" goto help + +if "%1" == "help" ( + :help + echo.Please use `make ^` where ^ 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. text to make text files + echo. man to make manual pages + echo. texinfo to make Texinfo files + echo. gettext to make PO message catalogs + echo. changes to make an overview over 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 + goto end +) + +if "%1" == "clean" ( + for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i + del /q /s %BUILDDIR%\* + goto end +) + + +%SPHINXBUILD% 2> nul +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "html" ( + %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/html. + goto end +) + +if "%1" == "dirhtml" ( + %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. + goto end +) + +if "%1" == "singlehtml" ( + %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. + goto end +) + +if "%1" == "pickle" ( + %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the pickle files. + goto end +) + +if "%1" == "json" ( + %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the JSON files. + goto end +) + +if "%1" == "htmlhelp" ( + %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run HTML Help Workshop with the ^ +.hhp project file in %BUILDDIR%/htmlhelp. + goto end +) + +if "%1" == "qthelp" ( + %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run "qcollectiongenerator" with the ^ +.qhcp project file in %BUILDDIR%/qthelp, like this: + echo.^> qcollectiongenerator %BUILDDIR%\qthelp\django-model-utils.qhcp + echo.To view the help file: + echo.^> assistant -collectionFile %BUILDDIR%\qthelp\django-model-utils.ghc + goto end +) + +if "%1" == "devhelp" ( + %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. + goto end +) + +if "%1" == "epub" ( + %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The epub file is in %BUILDDIR%/epub. + goto end +) + +if "%1" == "latex" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "latexpdf" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + cd %BUILDDIR%/latex + make all-pdf + cd %BUILDDIR%/.. + echo. + echo.Build finished; the PDF files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "latexpdfja" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + cd %BUILDDIR%/latex + make all-pdf-ja + cd %BUILDDIR%/.. + echo. + echo.Build finished; the PDF files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "text" ( + %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The text files are in %BUILDDIR%/text. + goto end +) + +if "%1" == "man" ( + %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The manual pages are in %BUILDDIR%/man. + goto end +) + +if "%1" == "texinfo" ( + %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. + goto end +) + +if "%1" == "gettext" ( + %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The message catalogs are in %BUILDDIR%/locale. + goto end +) + +if "%1" == "changes" ( + %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes + if errorlevel 1 exit /b 1 + echo. + echo.The overview file is in %BUILDDIR%/changes. + goto end +) + +if "%1" == "linkcheck" ( + %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck + if errorlevel 1 exit /b 1 + echo. + echo.Link check complete; look for any errors in the above output ^ +or in %BUILDDIR%/linkcheck/output.txt. + goto end +) + +if "%1" == "doctest" ( + %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest + if errorlevel 1 exit /b 1 + echo. + echo.Testing of doctests in the sources finished, look at the ^ +results in %BUILDDIR%/doctest/output.txt. + goto end +) + +if "%1" == "xml" ( + %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The XML files are in %BUILDDIR%/xml. + goto end +) + +if "%1" == "pseudoxml" ( + %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. + goto end +) + +:end diff --git a/docs/setup.rst b/docs/setup.rst new file mode 100644 index 0000000..c2ff096 --- /dev/null +++ b/docs/setup.rst @@ -0,0 +1,22 @@ +===== +Setup +===== + +Installation +============ + +Install from PyPI with ``pip``:: + + pip install django-model-utils + +To use ``django-model-utils`` in your Django project, just import and +use the utility classes described in this documentation; there is no need to +modify your ``INSTALLED_APPS`` setting. + +Dependencies +============ + +``django-model-utils`` supports `Django`_ 1.4.2 and later on Python 2.6, 2.7, +3.2, and 3.3. + +.. _Django: http://www.djangoproject.com/ diff --git a/docs/usage.rst b/docs/usage.rst new file mode 100644 index 0000000..f358ce9 --- /dev/null +++ b/docs/usage.rst @@ -0,0 +1,508 @@ +===== +Usage +===== + +Fields +====== + +Choices +------- + +``Choices`` provides some conveniences for setting ``choices`` on a Django model field: + +.. code-block:: python + + from model_utils import Choices + + class Article(models.Model): + STATUS = Choices('draft', 'published') + # ... + status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20) + +A ``Choices`` object is initialized with any number of choices. In the +simplest case, each choice is a string; that string will be used both +as the database representation of the choice, and the human-readable +representation. Note that you can access options as attributes on the +``Choices`` object: ``STATUS.draft``. + +But you may want your human-readable versions translated, in which +case you need to separate the human-readable version from the DB +representation. In this case you can provide choices as two-tuples: + +.. code-block:: python + + from model_utils import Choices + + class Article(models.Model): + STATUS = Choices(('draft', _('draft')), ('published', _('published'))) + # ... + status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20) + +But what if your database representation of choices is constrained in +a way that would hinder readability of your code? For instance, you +may need to use an ``IntegerField`` rather than a ``CharField``, or +you may want the database to order the values in your field in some +specific way. In this case, you can provide your choices as triples, +where the first element is the database representation, the second is +a valid Python identifier you will use in your code as a constant, and +the third is the human-readable version: + +.. code-block:: python + + from model_utils import Choices + + class Article(models.Model): + STATUS = Choices((0, 'draft', _('draft')), (1, 'published', _('published'))) + # ... + status = models.IntegerField(choices=STATUS, default=STATUS.draft) + + +StatusField +----------- + +A simple convenience for giving a model a set of "states." +``StatusField`` is a ``CharField`` subclass that expects to find a +``STATUS`` class attribute on its model, and uses that as its +``choices``. Also sets a default ``max_length`` of 100, and sets its +default value to the first item in the ``STATUS`` choices: + +.. code-block:: python + + from model_utils.fields import StatusField + from model_utils import Choices + + class Article(models.Model): + STATUS = Choices('draft', 'published') + # ... + status = StatusField() + +(The ``STATUS`` class attribute does not have to be a `Choices`_ +instance, it can be an ordinary list of two-tuples). + +``StatusField`` does not set ``db_index=True`` automatically; if you +expect to frequently filter on your status field (and it will have +enough selectivity to make an index worthwhile) you may want to add this +yourself. + + +MonitorField +------------ + +A ``DateTimeField`` subclass that monitors another field on the model, +and updates itself to the current date-time whenever the monitored +field changes: + +.. code-block:: python + + from model_utils.fields import MonitorField, StatusField + + class Article(models.Model): + STATUS = Choices('draft', 'published') + + status = StatusField() + status_changed = MonitorField(monitor='status') + +(A ``MonitorField`` can monitor any type of field for changes, not only a +``StatusField``.) + +SplitField +---------- + +A ``TextField`` subclass that automatically pulls an excerpt out of +its content (based on a "split here" marker or a default number of +initial paragraphs) and stores both its content and excerpt values in +the database. + +A ``SplitField`` is easy to add to any model definition: + +.. code-block:: python + + from django.db import models + from model_utils.fields import SplitField + + class Article(models.Model): + title = models.CharField(max_length=100) + body = SplitField() + +``SplitField`` automatically creates an extra non-editable field +``_body_excerpt`` to store the excerpt. This field doesn't need to be +accessed directly; see below. + +Accessing a SplitField on a model +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +When accessing an attribute of a model that was declared as a +``SplitField``, a ``SplitText`` object is returned. The ``SplitText`` +object has three attributes: + +``content``: + The full field contents. +``excerpt``: + The excerpt of ``content`` (read-only). +``has_more``: + True if the excerpt and content are different, False otherwise. + +This object also has a ``__unicode__`` method that returns the full +content, allowing ``SplitField`` attributes to appear in templates +without having to access ``content`` directly. + +Assuming the ``Article`` model above: + +.. code-block:: pycon + + >>> a = Article.objects.all()[0] + >>> a.body.content + u'some text\n\n\n\nmore text' + >>> a.body.excerpt + u'some text\n' + >>> unicode(a.body) + u'some text\n\n\n\nmore text' + +Assignment to ``a.body`` is equivalent to assignment to +``a.body.content``. + +.. note:: + + a.body.excerpt is only updated when a.save() is called + + +Customized excerpting +~~~~~~~~~~~~~~~~~~~~~ + +By default, ``SplitField`` looks for the marker ```` +alone on a line and takes everything before that marker as the +excerpt. This marker can be customized by setting the ``SPLIT_MARKER`` +setting. + +If no marker is found in the content, the first two paragraphs (where +paragraphs are blocks of text separated by a blank line) are taken to +be the excerpt. This number can be customized by setting the +``SPLIT_DEFAULT_PARAGRAPHS`` setting. + +Models +====== + +TimeFramedModel +--------------- + +An abstract base class for any model that expresses a time-range. Adds +``start`` and ``end`` nullable DateTimeFields, and a ``timeframed`` +manager that returns only objects for whom the current date-time lies +within their time range. + +StatusModel +----------- + +Pulls together `StatusField`_, `MonitorField`_ and `QueryManager`_ +into an abstract base class for any model with a "status." + +Just provide a ``STATUS`` class-attribute (a `Choices`_ object or a +list of two-tuples), and your model will have a ``status`` field with +those choices, a ``status_changed`` field containing the date-time the +``status`` was last changed, and a manager for each status that +returns objects with that status only: + +.. code-block:: python + + from model_utils.models import StatusModel + from model_utils import Choices + + class Article(StatusModel): + STATUS = Choices('draft', 'published') + + # ... + + a = Article() + a.status = Article.STATUS.published + + # this save will update a.status_changed + a.save() + + # this query will only return published articles: + Article.published.all() + +Model Managers +============== + +InheritanceManager +------------------ + +This manager (`contributed by Jeff Elmore`_) should be attached to a base model +class in a model-inheritance tree. It allows queries on that base model to +return heterogenous results of the actual proper subtypes, without any +additional queries. + +For instance, if you have a ``Place`` model with subclasses ``Restaurant`` and +``Bar``, you may want to query all Places: + +.. code-block:: python + + nearby_places = Place.objects.filter(location='here') + +But when you iterate over ``nearby_places``, you'll get only ``Place`` +instances back, even for objects that are "really" ``Restaurant`` or ``Bar``. +If you attach an ``InheritanceManager`` to ``Place``, you can just call the +``select_subclasses()`` method on the ``InheritanceManager`` or any +``QuerySet`` from it, and the resulting objects will be instances of +``Restaurant`` or ``Bar``: + +.. code-block:: python + + from model_utils.managers import InheritanceManager + + class Place(models.Model): + # ... + objects = InheritanceManager() + + class Restaurant(Place): + # ... + + class Bar(Place): + # ... + + nearby_places = Place.objects.filter(location='here').select_subclasses() + for place in nearby_places: + # "place" will automatically be an instance of Place, Restaurant, or Bar + +The database query performed will have an extra join for each subclass; if you +want to reduce the number of joins and you only need particular subclasses to +be returned as their actual type, you can pass subclass names to +``select_subclasses()``, much like the built-in ``select_related()`` method: + +.. code-block:: python + + nearby_places = Place.objects.select_subclasses("restaurant") + # restaurants will be Restaurant instances, bars will still be Place instances + +``InheritanceManager`` also provides a subclass-fetching alternative to the +``get()`` method: + +.. code-block:: python + + place = Place.objects.get_subclass(id=some_id) + # "place" will automatically be an instance of Place, Restaurant, or Bar + +If you don't explicitly call ``select_subclasses()`` or ``get_subclass()``, +an ``InheritanceManager`` behaves identically to a normal ``Manager``; so +it's safe to use as your default manager for the model. + +.. note:: + + Due to `Django bug #16572`_, on Django versions prior to 1.6 + ``InheritanceManager`` only supports a single level of model inheritance; + it won't work for grandchild models. + +.. _contributed by Jeff Elmore: http://jeffelmore.org/2010/11/11/automatic-downcasting-of-inherited-models-in-django/ +.. _Django bug #16572: https://code.djangoproject.com/ticket/16572 + + +TimeStampedModel +---------------- + +This abstract base class just provides self-updating ``created`` and +``modified`` fields on any model that inherits from it. + +QueryManager +------------ + +Many custom model managers do nothing more than return a QuerySet that +is filtered in some way. ``QueryManager`` allows you to express this +pattern with a minimum of boilerplate: + +.. code-block:: python + + from django.db import models + from model_utils.managers import QueryManager + + class Post(models.Model): + ... + published = models.BooleanField() + pub_date = models.DateField() + ... + + objects = models.Manager() + public = QueryManager(published=True).order_by('-pub_date') + +The kwargs passed to ``QueryManager`` will be passed as-is to the +``QuerySet.filter()`` method. You can also pass a ``Q`` object to +``QueryManager`` to express more complex conditions. Note that you can +set the ordering of the ``QuerySet`` returned by the ``QueryManager`` +by chaining a call to ``.order_by()`` on the ``QueryManager`` (this is +not required). + + +PassThroughManager +------------------ + +A common "gotcha" when defining methods on a custom manager class is that those +same methods are not automatically also available on the QuerySets returned by +that manager, so are not "chainable". This can be counterintuitive, as most of +the public QuerySet API is mirrored on managers. It is possible to create a +custom Manager that returns QuerySets that have the same additional methods, +but this requires boilerplate code. The ``PassThroughManager`` class +(`contributed by Paul McLanahan`_) removes this boilerplate. + +.. _contributed by Paul McLanahan: http://paulm.us/post/3717466639/passthroughmanager-for-django + +To use ``PassThroughManager``, rather than defining a custom manager with +additional methods, define a custom ``QuerySet`` subclass with the additional +methods you want, and pass that ``QuerySet`` subclass to the +``PassThroughManager.for_queryset_class()`` class method. The returned +``PassThroughManager`` subclass will always return instances of your custom +``QuerySet``, and you can also call methods of your custom ``QuerySet`` +directly on the manager: + +.. code-block:: python + + from datetime import datetime + from django.db import models + from django.db.models.query import QuerySet + from model_utils.managers import PassThroughManager + + class PostQuerySet(QuerySet): + def by_author(self, user): + return self.filter(user=user) + + def published(self): + return self.filter(published__lte=datetime.now()) + + def unpublished(self): + return self.filter(published__gte=datetime.now()) + + + class Post(models.Model): + user = models.ForeignKey(User) + published = models.DateTimeField() + + objects = PassThroughManager.for_queryset_class(PostQuerySet)() + + Post.objects.published() + Post.objects.by_author(user=request.user).unpublished() + + +Field Tracker +============= + +A ``FieldTracker`` can be added to a model to track changes in model fields. A +``FieldTracker`` allows querying for field changes since a model instance was +last saved. An example of applying ``FieldTracker`` to a model: + +.. code-block:: python + + from django.db import models + from model_utils import FieldTracker + + class Post(models.Model): + title = models.CharField(max_length=100) + body = models.TextField() + + tracker = FieldTracker() + +.. note:: + + ``django-model-utils`` 1.3.0 introduced the ``ModelTracker`` object for + tracking changes to model field values. Unfortunately ``ModelTracker`` + suffered from some serious flaws in its handling of ``ForeignKey`` fields, + potentially resulting in many extra database queries if a ``ForeignKey`` + field was tracked. In order to avoid breaking API backwards-compatibility, + ``ModelTracker`` retains the previous behavior but is deprecated, and + ``FieldTracker`` has been introduced to provide better ``ForeignKey`` + handling. All uses of ``ModelTracker`` should be replaced by + ``FieldTracker``. + + Summary of differences between ``ModelTracker`` and ``FieldTracker``: + + * The previous value returned for a tracked ``ForeignKey`` field will now + be the raw ID rather than the full object (avoiding extra database + queries). (GH-43) + + * The ``changed()`` method no longer returns the empty dictionary for all + unsaved instances; rather, ``None`` is considered to be the initial value + of all fields if the model has never been saved, thus ``changed()`` on an + unsaved instance will return a dictionary containing all fields whose + current value is not ``None``. + + * The ``has_changed()`` method no longer crashes after an object's first + save. (GH-53). + + +Accessing a field tracker +------------------------- + +There are multiple methods available for checking for changes in model fields. + + +previous +~~~~~~~~ +Returns the value of the given field during the last save: + +.. code-block:: pycon + + >>> a = Post.objects.create(title='First Post') + >>> a.title = 'Welcome' + >>> a.tracker.previous('title') + u'First Post' + +Returns ``None`` when the model instance isn't saved yet. + + +has_changed +~~~~~~~~~~~ +Returns ``True`` if the given field has changed since the last save: + +.. code-block:: pycon + + >>> a = Post.objects.create(title='First Post') + >>> a.title = 'Welcome' + >>> a.tracker.has_changed('title') + True + >>> a.tracker.has_changed('body') + False + +The ``has_changed`` method relies on ``previous`` to determine whether a +field's values has changed. + + +changed +~~~~~~~ +Returns a dictionary of all fields that have been changed since the last save +and the values of the fields during the last save: + +.. code-block:: pycon + + >>> a = Post.objects.create(title='First Post') + >>> a.title = 'Welcome' + >>> a.body = 'First post!' + >>> a.tracker.changed() + {'title': 'First Post', 'body': ''} + +The ``changed`` method relies on ``has_changed`` to determine which fields +have changed. + + +Tracking specific fields +------------------------ + +A fields parameter can be given to ``FieldTracker`` to limit tracking to +specific fields: + +.. code-block:: python + + from django.db import models + from model_utils import FieldTracker + + class Post(models.Model): + title = models.CharField(max_length=100) + body = models.TextField() + + title_tracker = FieldTracker(fields=['title']) + +An example using the model specified above: + +.. code-block:: pycon + + >>> a = Post.objects.create(title='First Post') + >>> a.body = 'First post!' + >>> a.title_tracker.changed() + {'title': None} + From 9a24ab0a37226d1364043b0aae631d59e6d1c49a Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 31 Jul 2013 22:57:15 -0700 Subject: [PATCH 14/33] Exclude docs html output from git --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 40b533c..83041fe 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ HGREV Django-*.egg *.pyc htmlcov/ +docs/_build/ From 1ff3f267f9e6324790a76725376ee536fc87b180 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 31 Jul 2013 22:57:41 -0700 Subject: [PATCH 15/33] Remove documentation from README --- README.rst | 516 ----------------------------------------------------- 1 file changed, 516 deletions(-) diff --git a/README.rst b/README.rst index 4d3fc08..64e17ae 100644 --- a/README.rst +++ b/README.rst @@ -11,25 +11,6 @@ django-model-utils Django model mixins and utilities. -Installation -============ - -Install from PyPI with ``pip``:: - - pip install django-model-utils - -To use ``django-model-utils`` in your Django project, just import and -use the utility classes described below; there is no need to modify -your ``INSTALLED_APPS`` setting. - -Dependencies ------------- - -``django-model-utils`` supports `Django`_ 1.4.2 and later on Python 2.6, 2.7, -3.2, and 3.3. - -.. _Django: http://www.djangoproject.com/ - Contributing ============ @@ -46,500 +27,3 @@ pull requests tracked in it are closed, but all new issues should be filed at GitHub.) .. _BitBucket: https://bitbucket.org/carljm/django-model-utils/overview - - -Choices -======= - -``Choices`` provides some conveniences for setting ``choices`` on a Django model field: - -.. code-block:: python - - from model_utils import Choices - - class Article(models.Model): - STATUS = Choices('draft', 'published') - # ... - status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20) - -A ``Choices`` object is initialized with any number of choices. In the -simplest case, each choice is a string; that string will be used both -as the database representation of the choice, and the human-readable -representation. Note that you can access options as attributes on the -``Choices`` object: ``STATUS.draft``. - -But you may want your human-readable versions translated, in which -case you need to separate the human-readable version from the DB -representation. In this case you can provide choices as two-tuples: - -.. code-block:: python - - from model_utils import Choices - - class Article(models.Model): - STATUS = Choices(('draft', _('draft')), ('published', _('published'))) - # ... - status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20) - -But what if your database representation of choices is constrained in -a way that would hinder readability of your code? For instance, you -may need to use an ``IntegerField`` rather than a ``CharField``, or -you may want the database to order the values in your field in some -specific way. In this case, you can provide your choices as triples, -where the first element is the database representation, the second is -a valid Python identifier you will use in your code as a constant, and -the third is the human-readable version: - -.. code-block:: python - - from model_utils import Choices - - class Article(models.Model): - STATUS = Choices((0, 'draft', _('draft')), (1, 'published', _('published'))) - # ... - status = models.IntegerField(choices=STATUS, default=STATUS.draft) - - -StatusField -=========== - -A simple convenience for giving a model a set of "states." -``StatusField`` is a ``CharField`` subclass that expects to find a -``STATUS`` class attribute on its model, and uses that as its -``choices``. Also sets a default ``max_length`` of 100, and sets its -default value to the first item in the ``STATUS`` choices: - -.. code-block:: python - - from model_utils.fields import StatusField - from model_utils import Choices - - class Article(models.Model): - STATUS = Choices('draft', 'published') - # ... - status = StatusField() - -(The ``STATUS`` class attribute does not have to be a `Choices`_ -instance, it can be an ordinary list of two-tuples). - -``StatusField`` does not set ``db_index=True`` automatically; if you -expect to frequently filter on your status field (and it will have -enough selectivity to make an index worthwhile) you may want to add this -yourself. - - -MonitorField -============ - -A ``DateTimeField`` subclass that monitors another field on the model, -and updates itself to the current date-time whenever the monitored -field changes: - -.. code-block:: python - - from model_utils.fields import MonitorField, StatusField - - class Article(models.Model): - STATUS = Choices('draft', 'published') - - status = StatusField() - status_changed = MonitorField(monitor='status') - -(A ``MonitorField`` can monitor any type of field for changes, not only a -``StatusField``.) - -SplitField -========== - -A ``TextField`` subclass that automatically pulls an excerpt out of -its content (based on a "split here" marker or a default number of -initial paragraphs) and stores both its content and excerpt values in -the database. - -A ``SplitField`` is easy to add to any model definition: - -.. code-block:: python - - from django.db import models - from model_utils.fields import SplitField - - class Article(models.Model): - title = models.CharField(max_length=100) - body = SplitField() - -``SplitField`` automatically creates an extra non-editable field -``_body_excerpt`` to store the excerpt. This field doesn't need to be -accessed directly; see below. - -Accessing a SplitField on a model ---------------------------------- - -When accessing an attribute of a model that was declared as a -``SplitField``, a ``SplitText`` object is returned. The ``SplitText`` -object has three attributes: - -``content``: - The full field contents. -``excerpt``: - The excerpt of ``content`` (read-only). -``has_more``: - True if the excerpt and content are different, False otherwise. - -This object also has a ``__unicode__`` method that returns the full -content, allowing ``SplitField`` attributes to appear in templates -without having to access ``content`` directly. - -Assuming the ``Article`` model above: - -.. code-block:: pycon - - >>> a = Article.objects.all()[0] - >>> a.body.content - u'some text\n\n\n\nmore text' - >>> a.body.excerpt - u'some text\n' - >>> unicode(a.body) - u'some text\n\n\n\nmore text' - -Assignment to ``a.body`` is equivalent to assignment to -``a.body.content``. - -.. note:: - - a.body.excerpt is only updated when a.save() is called - - -Customized excerpting ---------------------- - -By default, ``SplitField`` looks for the marker ```` -alone on a line and takes everything before that marker as the -excerpt. This marker can be customized by setting the ``SPLIT_MARKER`` -setting. - -If no marker is found in the content, the first two paragraphs (where -paragraphs are blocks of text separated by a blank line) are taken to -be the excerpt. This number can be customized by setting the -``SPLIT_DEFAULT_PARAGRAPHS`` setting. - -TimeFramedModel -=============== - -An abstract base class for any model that expresses a time-range. Adds -``start`` and ``end`` nullable DateTimeFields, and a ``timeframed`` -manager that returns only objects for whom the current date-time lies -within their time range. - -StatusModel -=========== - -Pulls together `StatusField`_, `MonitorField`_ and `QueryManager`_ -into an abstract base class for any model with a "status." - -Just provide a ``STATUS`` class-attribute (a `Choices`_ object or a -list of two-tuples), and your model will have a ``status`` field with -those choices, a ``status_changed`` field containing the date-time the -``status`` was last changed, and a manager for each status that -returns objects with that status only: - -.. code-block:: python - - from model_utils.models import StatusModel - from model_utils import Choices - - class Article(StatusModel): - STATUS = Choices('draft', 'published') - - # ... - - a = Article() - a.status = Article.STATUS.published - - # this save will update a.status_changed - a.save() - - # this query will only return published articles: - Article.published.all() - -InheritanceManager -================== - -This manager (`contributed by Jeff Elmore`_) should be attached to a base model -class in a model-inheritance tree. It allows queries on that base model to -return heterogenous results of the actual proper subtypes, without any -additional queries. - -For instance, if you have a ``Place`` model with subclasses ``Restaurant`` and -``Bar``, you may want to query all Places: - -.. code-block:: python - - nearby_places = Place.objects.filter(location='here') - -But when you iterate over ``nearby_places``, you'll get only ``Place`` -instances back, even for objects that are "really" ``Restaurant`` or ``Bar``. -If you attach an ``InheritanceManager`` to ``Place``, you can just call the -``select_subclasses()`` method on the ``InheritanceManager`` or any -``QuerySet`` from it, and the resulting objects will be instances of -``Restaurant`` or ``Bar``: - -.. code-block:: python - - from model_utils.managers import InheritanceManager - - class Place(models.Model): - # ... - objects = InheritanceManager() - - class Restaurant(Place): - # ... - - class Bar(Place): - # ... - - nearby_places = Place.objects.filter(location='here').select_subclasses() - for place in nearby_places: - # "place" will automatically be an instance of Place, Restaurant, or Bar - -The database query performed will have an extra join for each subclass; if you -want to reduce the number of joins and you only need particular subclasses to -be returned as their actual type, you can pass subclass names to -``select_subclasses()``, much like the built-in ``select_related()`` method: - -.. code-block:: python - - nearby_places = Place.objects.select_subclasses("restaurant") - # restaurants will be Restaurant instances, bars will still be Place instances - -``InheritanceManager`` also provides a subclass-fetching alternative to the -``get()`` method: - -.. code-block:: python - - place = Place.objects.get_subclass(id=some_id) - # "place" will automatically be an instance of Place, Restaurant, or Bar - -If you don't explicitly call ``select_subclasses()`` or ``get_subclass()``, -an ``InheritanceManager`` behaves identically to a normal ``Manager``; so -it's safe to use as your default manager for the model. - -.. note:: - - Due to `Django bug #16572`_, on Django versions prior to 1.6 - ``InheritanceManager`` only supports a single level of model inheritance; - it won't work for grandchild models. - -.. _contributed by Jeff Elmore: http://jeffelmore.org/2010/11/11/automatic-downcasting-of-inherited-models-in-django/ -.. _Django bug #16572: https://code.djangoproject.com/ticket/16572 - - -TimeStampedModel -================ - -This abstract base class just provides self-updating ``created`` and -``modified`` fields on any model that inherits from it. - -QueryManager -============ - -Many custom model managers do nothing more than return a QuerySet that -is filtered in some way. ``QueryManager`` allows you to express this -pattern with a minimum of boilerplate: - -.. code-block:: python - - from django.db import models - from model_utils.managers import QueryManager - - class Post(models.Model): - ... - published = models.BooleanField() - pub_date = models.DateField() - ... - - objects = models.Manager() - public = QueryManager(published=True).order_by('-pub_date') - -The kwargs passed to ``QueryManager`` will be passed as-is to the -``QuerySet.filter()`` method. You can also pass a ``Q`` object to -``QueryManager`` to express more complex conditions. Note that you can -set the ordering of the ``QuerySet`` returned by the ``QueryManager`` -by chaining a call to ``.order_by()`` on the ``QueryManager`` (this is -not required). - - -PassThroughManager -================== - -A common "gotcha" when defining methods on a custom manager class is that those -same methods are not automatically also available on the QuerySets returned by -that manager, so are not "chainable". This can be counterintuitive, as most of -the public QuerySet API is mirrored on managers. It is possible to create a -custom Manager that returns QuerySets that have the same additional methods, -but this requires boilerplate code. The ``PassThroughManager`` class -(`contributed by Paul McLanahan`_) removes this boilerplate. - -.. _contributed by Paul McLanahan: http://paulm.us/post/3717466639/passthroughmanager-for-django - -To use ``PassThroughManager``, rather than defining a custom manager with -additional methods, define a custom ``QuerySet`` subclass with the additional -methods you want, and pass that ``QuerySet`` subclass to the -``PassThroughManager.for_queryset_class()`` class method. The returned -``PassThroughManager`` subclass will always return instances of your custom -``QuerySet``, and you can also call methods of your custom ``QuerySet`` -directly on the manager: - -.. code-block:: python - - from datetime import datetime - from django.db import models - from django.db.models.query import QuerySet - from model_utils.managers import PassThroughManager - - class PostQuerySet(QuerySet): - def by_author(self, user): - return self.filter(user=user) - - def published(self): - return self.filter(published__lte=datetime.now()) - - def unpublished(self): - return self.filter(published__gte=datetime.now()) - - - class Post(models.Model): - user = models.ForeignKey(User) - published = models.DateTimeField() - - objects = PassThroughManager.for_queryset_class(PostQuerySet)() - - Post.objects.published() - Post.objects.by_author(user=request.user).unpublished() - - -FieldTracker -============ - -A ``FieldTracker`` can be added to a model to track changes in model fields. A -``FieldTracker`` allows querying for field changes since a model instance was -last saved. An example of applying ``FieldTracker`` to a model: - -.. code-block:: python - - from django.db import models - from model_utils import FieldTracker - - class Post(models.Model): - title = models.CharField(max_length=100) - body = models.TextField() - - tracker = FieldTracker() - -.. note:: - - ``django-model-utils`` 1.3.0 introduced the ``ModelTracker`` object for - tracking changes to model field values. Unfortunately ``ModelTracker`` - suffered from some serious flaws in its handling of ``ForeignKey`` fields, - potentially resulting in many extra database queries if a ``ForeignKey`` - field was tracked. In order to avoid breaking API backwards-compatibility, - ``ModelTracker`` retains the previous behavior but is deprecated, and - ``FieldTracker`` has been introduced to provide better ``ForeignKey`` - handling. All uses of ``ModelTracker`` should be replaced by - ``FieldTracker``. - - Summary of differences between ``ModelTracker`` and ``FieldTracker``: - - * The previous value returned for a tracked ``ForeignKey`` field will now - be the raw ID rather than the full object (avoiding extra database - queries). (GH-43) - - * The ``changed()`` method no longer returns the empty dictionary for all - unsaved instances; rather, ``None`` is considered to be the initial value - of all fields if the model has never been saved, thus ``changed()`` on an - unsaved instance will return a dictionary containing all fields whose - current value is not ``None``. - - * The ``has_changed()`` method no longer crashes after an object's first - save. (GH-53). - - -Accessing a field tracker -------------------------- - -There are multiple methods available for checking for changes in model fields. - - -previous -~~~~~~~~ -Returns the value of the given field during the last save: - -.. code-block:: pycon - - >>> a = Post.objects.create(title='First Post') - >>> a.title = 'Welcome' - >>> a.tracker.previous('title') - u'First Post' - -Returns ``None`` when the model instance isn't saved yet. - - -has_changed -~~~~~~~~~~~ -Returns ``True`` if the given field has changed since the last save: - -.. code-block:: pycon - - >>> a = Post.objects.create(title='First Post') - >>> a.title = 'Welcome' - >>> a.tracker.has_changed('title') - True - >>> a.tracker.has_changed('body') - False - -The ``has_changed`` method relies on ``previous`` to determine whether a -field's values has changed. - - -changed -~~~~~~~ -Returns a dictionary of all fields that have been changed since the last save -and the values of the fields during the last save: - -.. code-block:: pycon - - >>> a = Post.objects.create(title='First Post') - >>> a.title = 'Welcome' - >>> a.body = 'First post!' - >>> a.tracker.changed() - {'title': 'First Post', 'body': ''} - -The ``changed`` method relies on ``has_changed`` to determine which fields -have changed. - - -Tracking specific fields ------------------------- - -A fields parameter can be given to ``FieldTracker`` to limit tracking to -specific fields: - -.. code-block:: python - - from django.db import models - from model_utils import FieldTracker - - class Post(models.Model): - title = models.CharField(max_length=100) - body = models.TextField() - - title_tracker = FieldTracker(fields=['title']) - -An example using the model specified above: - -.. code-block:: pycon - - >>> a = Post.objects.create(title='First Post') - >>> a.body = 'First post!' - >>> a.title_tracker.changed() - {'title': None} - From 176f557b13d164e25768f94e5298a5189ba3e03a Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 31 Jul 2013 22:58:15 -0700 Subject: [PATCH 16/33] Add setup.cfg for ``python setup.py build_sphinx`` --- setup.cfg | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 setup.cfg diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..269a021 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,4 @@ +[build_sphinx] +source-dir = docs/ +build-dir = docs/_build +all_files = 1 From f89369f9acff505aaf92ff0e8389f6544a325703 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Fri, 2 Aug 2013 12:23:48 +0100 Subject: [PATCH 17/33] Implement __contains__ ('x' in Choices('x')) for Choices objects. In Choices, `_choice_dict` appears to be a definitive location of all internal/DB representations, so it seems the best target for finding out if the given item is part of the sequences. --- model_utils/choices.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/model_utils/choices.py b/model_utils/choices.py index b5177cd..570f1af 100644 --- a/model_utils/choices.py +++ b/model_utils/choices.py @@ -76,3 +76,7 @@ class Choices(object): def __repr__(self): return '%s(%s)' % (self.__class__.__name__, ', '.join(("%s" % repr(i) for i in self._full))) + + def __contains__(self, item): + if item in self._choice_dict.values(): + return True From 6f0cf2a96cf9cec7cf1a0441b7d7d74fe07bf4c2 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Fri, 2 Aug 2013 12:24:44 +0100 Subject: [PATCH 18/33] first pass at tests for the __contains__ functionality just implemented. --- model_utils/tests/tests.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index f1fe411..ab60a62 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -223,6 +223,12 @@ class ChoicesTests(TestCase): def test_wrong_length_tuple(self): self.assertRaises(ValueError, Choices, ('a',)) + def test_contains_value(self): + self.assertTrue('PUBLISHED' in self.STATUS) + self.assertTrue('DRAFT' in self.STATUS) + + def test_doesnt_contain_value(self): + self.assertFalse('UNPUBLISHED' in self.STATUS) class LabelChoicesTests(ChoicesTests): @@ -265,6 +271,19 @@ class LabelChoicesTests(ChoicesTests): ('DELETED', 'DELETED', 'DELETED'), ))) + def test_contains_value(self): + self.assertTrue('PUBLISHED' in self.STATUS) + self.assertTrue('DRAFT' in self.STATUS) + # This should be True, because both the display value + # and the internal representation are both DELETED. + self.assertTrue('DELETED' in self.STATUS) + + def test_doesnt_contain_value(self): + self.assertFalse('UNPUBLISHED' in self.STATUS) + + def test_doesnt_contain_display_value(self): + self.assertFalse('is draft' in self.STATUS) + class IdentifierChoicesTests(ChoicesTests): @@ -301,6 +320,19 @@ class IdentifierChoicesTests(ChoicesTests): (2, 'DELETED', 'is deleted'), ))) + def test_contains_value(self): + self.assertTrue(0 in self.STATUS) + self.assertTrue(1 in self.STATUS) + self.assertTrue(2 in self.STATUS) + + def test_doesnt_contain_value(self): + self.assertFalse(3 in self.STATUS) + + def test_doesnt_contain_display_value(self): + self.assertFalse('is draft' in self.STATUS) + + def test_doesnt_contain_python_attr(self): + self.assertFalse('PUBLISHED' in self.STATUS) class InheritanceManagerTests(TestCase): def setUp(self): From 64a47ad86192f658c885922fddb75b53baf62b27 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Mon, 5 Aug 2013 19:49:21 -0600 Subject: [PATCH 19/33] Update AUTHORS and changelog for Choices.__contains__ addition. --- AUTHORS.rst | 1 + CHANGES.rst | 3 +++ 2 files changed, 4 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 33495d6..3a33840 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -10,6 +10,7 @@ James Oakley Jannis Leidel Javier García Sogo Jeff Elmore +Keryn Knight ivirabyan Paul McLanahan Rinat Shigapov diff --git a/CHANGES.rst b/CHANGES.rst index 0b2998f..9b3bb75 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,9 @@ CHANGES master (unreleased) ------------------- +* `Choices` now `__contains__` its Python identifier values. Thanks Keryn + Knight. (Merge of GH-69). + 1.4.0 (2013.06.03) ------------------ From b78ff6e4b8d18deda01cf95d9bf192094c4915e6 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Mon, 5 Aug 2013 23:29:42 -0700 Subject: [PATCH 20/33] Add some missing blank lines above headers in docs --- docs/index.rst | 2 ++ docs/setup.rst | 1 + docs/usage.rst | 6 ++++++ 3 files changed, 9 insertions(+) diff --git a/docs/index.rst b/docs/index.rst index 2dd94ed..a77a6d2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -4,6 +4,7 @@ django-model-utils Django model mixins and utilities. + Contents ======== @@ -13,6 +14,7 @@ Contents setup usage + Contributing ============ diff --git a/docs/setup.rst b/docs/setup.rst index c2ff096..5621649 100644 --- a/docs/setup.rst +++ b/docs/setup.rst @@ -13,6 +13,7 @@ To use ``django-model-utils`` in your Django project, just import and use the utility classes described in this documentation; there is no need to modify your ``INSTALLED_APPS`` setting. + Dependencies ============ diff --git a/docs/usage.rst b/docs/usage.rst index f358ce9..9462975 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -105,6 +105,7 @@ field changes: (A ``MonitorField`` can monitor any type of field for changes, not only a ``StatusField``.) + SplitField ---------- @@ -128,6 +129,7 @@ A ``SplitField`` is easy to add to any model definition: ``_body_excerpt`` to store the excerpt. This field doesn't need to be accessed directly; see below. + Accessing a SplitField on a model ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -179,6 +181,7 @@ paragraphs are blocks of text separated by a blank line) are taken to be the excerpt. This number can be customized by setting the ``SPLIT_DEFAULT_PARAGRAPHS`` setting. + Models ====== @@ -190,6 +193,7 @@ An abstract base class for any model that expresses a time-range. Adds manager that returns only objects for whom the current date-time lies within their time range. + StatusModel ----------- @@ -221,6 +225,7 @@ returns objects with that status only: # this query will only return published articles: Article.published.all() + Model Managers ============== @@ -302,6 +307,7 @@ TimeStampedModel This abstract base class just provides self-updating ``created`` and ``modified`` fields on any model that inherits from it. + QueryManager ------------ From 85330bc1065f11e259d9ec10c10d633ce45028eb Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Tue, 6 Aug 2013 18:52:18 -0700 Subject: [PATCH 21/33] Split usage docs into multiple files --- docs/fields.rst | 126 +++++++++++ docs/index.rst | 5 +- docs/managers.rst | 158 ++++++++++++++ docs/models.rst | 42 ++++ docs/usage.rst | 514 --------------------------------------------- docs/utilities.rst | 181 ++++++++++++++++ 6 files changed, 511 insertions(+), 515 deletions(-) create mode 100644 docs/fields.rst create mode 100644 docs/managers.rst create mode 100644 docs/models.rst delete mode 100644 docs/usage.rst create mode 100644 docs/utilities.rst diff --git a/docs/fields.rst b/docs/fields.rst new file mode 100644 index 0000000..c03c9fb --- /dev/null +++ b/docs/fields.rst @@ -0,0 +1,126 @@ +Fields +====== + +StatusField +----------- + +A simple convenience for giving a model a set of "states." +``StatusField`` is a ``CharField`` subclass that expects to find a +``STATUS`` class attribute on its model, and uses that as its +``choices``. Also sets a default ``max_length`` of 100, and sets its +default value to the first item in the ``STATUS`` choices: + +.. code-block:: python + + from model_utils.fields import StatusField + from model_utils import Choices + + class Article(models.Model): + STATUS = Choices('draft', 'published') + # ... + status = StatusField() + +(The ``STATUS`` class attribute does not have to be a `Choices`_ +instance, it can be an ordinary list of two-tuples). + +``StatusField`` does not set ``db_index=True`` automatically; if you +expect to frequently filter on your status field (and it will have +enough selectivity to make an index worthwhile) you may want to add this +yourself. + + +MonitorField +------------ + +A ``DateTimeField`` subclass that monitors another field on the model, +and updates itself to the current date-time whenever the monitored +field changes: + +.. code-block:: python + + from model_utils.fields import MonitorField, StatusField + + class Article(models.Model): + STATUS = Choices('draft', 'published') + + status = StatusField() + status_changed = MonitorField(monitor='status') + +(A ``MonitorField`` can monitor any type of field for changes, not only a +``StatusField``.) + + +SplitField +---------- + +A ``TextField`` subclass that automatically pulls an excerpt out of +its content (based on a "split here" marker or a default number of +initial paragraphs) and stores both its content and excerpt values in +the database. + +A ``SplitField`` is easy to add to any model definition: + +.. code-block:: python + + from django.db import models + from model_utils.fields import SplitField + + class Article(models.Model): + title = models.CharField(max_length=100) + body = SplitField() + +``SplitField`` automatically creates an extra non-editable field +``_body_excerpt`` to store the excerpt. This field doesn't need to be +accessed directly; see below. + + +Accessing a SplitField on a model +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +When accessing an attribute of a model that was declared as a +``SplitField``, a ``SplitText`` object is returned. The ``SplitText`` +object has three attributes: + +``content``: + The full field contents. +``excerpt``: + The excerpt of ``content`` (read-only). +``has_more``: + True if the excerpt and content are different, False otherwise. + +This object also has a ``__unicode__`` method that returns the full +content, allowing ``SplitField`` attributes to appear in templates +without having to access ``content`` directly. + +Assuming the ``Article`` model above: + +.. code-block:: pycon + + >>> a = Article.objects.all()[0] + >>> a.body.content + u'some text\n\n\n\nmore text' + >>> a.body.excerpt + u'some text\n' + >>> unicode(a.body) + u'some text\n\n\n\nmore text' + +Assignment to ``a.body`` is equivalent to assignment to +``a.body.content``. + +.. note:: + + a.body.excerpt is only updated when a.save() is called + + +Customized excerpting +~~~~~~~~~~~~~~~~~~~~~ + +By default, ``SplitField`` looks for the marker ```` +alone on a line and takes everything before that marker as the +excerpt. This marker can be customized by setting the ``SPLIT_MARKER`` +setting. + +If no marker is found in the content, the first two paragraphs (where +paragraphs are blocks of text separated by a blank line) are taken to +be the excerpt. This number can be customized by setting the +``SPLIT_DEFAULT_PARAGRAPHS`` setting. diff --git a/docs/index.rst b/docs/index.rst index a77a6d2..9b6d2bb 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -12,7 +12,10 @@ Contents :maxdepth: 3 setup - usage + fields + models + managers + utilities Contributing diff --git a/docs/managers.rst b/docs/managers.rst new file mode 100644 index 0000000..257a878 --- /dev/null +++ b/docs/managers.rst @@ -0,0 +1,158 @@ +Model Managers +============== + +InheritanceManager +------------------ + +This manager (`contributed by Jeff Elmore`_) should be attached to a base model +class in a model-inheritance tree. It allows queries on that base model to +return heterogenous results of the actual proper subtypes, without any +additional queries. + +For instance, if you have a ``Place`` model with subclasses ``Restaurant`` and +``Bar``, you may want to query all Places: + +.. code-block:: python + + nearby_places = Place.objects.filter(location='here') + +But when you iterate over ``nearby_places``, you'll get only ``Place`` +instances back, even for objects that are "really" ``Restaurant`` or ``Bar``. +If you attach an ``InheritanceManager`` to ``Place``, you can just call the +``select_subclasses()`` method on the ``InheritanceManager`` or any +``QuerySet`` from it, and the resulting objects will be instances of +``Restaurant`` or ``Bar``: + +.. code-block:: python + + from model_utils.managers import InheritanceManager + + class Place(models.Model): + # ... + objects = InheritanceManager() + + class Restaurant(Place): + # ... + + class Bar(Place): + # ... + + nearby_places = Place.objects.filter(location='here').select_subclasses() + for place in nearby_places: + # "place" will automatically be an instance of Place, Restaurant, or Bar + +The database query performed will have an extra join for each subclass; if you +want to reduce the number of joins and you only need particular subclasses to +be returned as their actual type, you can pass subclass names to +``select_subclasses()``, much like the built-in ``select_related()`` method: + +.. code-block:: python + + nearby_places = Place.objects.select_subclasses("restaurant") + # restaurants will be Restaurant instances, bars will still be Place instances + +``InheritanceManager`` also provides a subclass-fetching alternative to the +``get()`` method: + +.. code-block:: python + + place = Place.objects.get_subclass(id=some_id) + # "place" will automatically be an instance of Place, Restaurant, or Bar + +If you don't explicitly call ``select_subclasses()`` or ``get_subclass()``, +an ``InheritanceManager`` behaves identically to a normal ``Manager``; so +it's safe to use as your default manager for the model. + +.. note:: + + Due to `Django bug #16572`_, on Django versions prior to 1.6 + ``InheritanceManager`` only supports a single level of model inheritance; + it won't work for grandchild models. + +.. _contributed by Jeff Elmore: http://jeffelmore.org/2010/11/11/automatic-downcasting-of-inherited-models-in-django/ +.. _Django bug #16572: https://code.djangoproject.com/ticket/16572 + + +TimeStampedModel +---------------- + +This abstract base class just provides self-updating ``created`` and +``modified`` fields on any model that inherits from it. + + +QueryManager +------------ + +Many custom model managers do nothing more than return a QuerySet that +is filtered in some way. ``QueryManager`` allows you to express this +pattern with a minimum of boilerplate: + +.. code-block:: python + + from django.db import models + from model_utils.managers import QueryManager + + class Post(models.Model): + ... + published = models.BooleanField() + pub_date = models.DateField() + ... + + objects = models.Manager() + public = QueryManager(published=True).order_by('-pub_date') + +The kwargs passed to ``QueryManager`` will be passed as-is to the +``QuerySet.filter()`` method. You can also pass a ``Q`` object to +``QueryManager`` to express more complex conditions. Note that you can +set the ordering of the ``QuerySet`` returned by the ``QueryManager`` +by chaining a call to ``.order_by()`` on the ``QueryManager`` (this is +not required). + + +PassThroughManager +------------------ + +A common "gotcha" when defining methods on a custom manager class is that those +same methods are not automatically also available on the QuerySets returned by +that manager, so are not "chainable". This can be counterintuitive, as most of +the public QuerySet API is mirrored on managers. It is possible to create a +custom Manager that returns QuerySets that have the same additional methods, +but this requires boilerplate code. The ``PassThroughManager`` class +(`contributed by Paul McLanahan`_) removes this boilerplate. + +.. _contributed by Paul McLanahan: http://paulm.us/post/3717466639/passthroughmanager-for-django + +To use ``PassThroughManager``, rather than defining a custom manager with +additional methods, define a custom ``QuerySet`` subclass with the additional +methods you want, and pass that ``QuerySet`` subclass to the +``PassThroughManager.for_queryset_class()`` class method. The returned +``PassThroughManager`` subclass will always return instances of your custom +``QuerySet``, and you can also call methods of your custom ``QuerySet`` +directly on the manager: + +.. code-block:: python + + from datetime import datetime + from django.db import models + from django.db.models.query import QuerySet + from model_utils.managers import PassThroughManager + + class PostQuerySet(QuerySet): + def by_author(self, user): + return self.filter(user=user) + + def published(self): + return self.filter(published__lte=datetime.now()) + + def unpublished(self): + return self.filter(published__gte=datetime.now()) + + + class Post(models.Model): + user = models.ForeignKey(User) + published = models.DateTimeField() + + objects = PassThroughManager.for_queryset_class(PostQuerySet)() + + Post.objects.published() + Post.objects.by_author(user=request.user).unpublished() diff --git a/docs/models.rst b/docs/models.rst new file mode 100644 index 0000000..0627e8e --- /dev/null +++ b/docs/models.rst @@ -0,0 +1,42 @@ +Models +====== + +TimeFramedModel +--------------- + +An abstract base class for any model that expresses a time-range. Adds +``start`` and ``end`` nullable DateTimeFields, and a ``timeframed`` +manager that returns only objects for whom the current date-time lies +within their time range. + + +StatusModel +----------- + +Pulls together `StatusField`_, `MonitorField`_ and `QueryManager`_ +into an abstract base class for any model with a "status." + +Just provide a ``STATUS`` class-attribute (a `Choices`_ object or a +list of two-tuples), and your model will have a ``status`` field with +those choices, a ``status_changed`` field containing the date-time the +``status`` was last changed, and a manager for each status that +returns objects with that status only: + +.. code-block:: python + + from model_utils.models import StatusModel + from model_utils import Choices + + class Article(StatusModel): + STATUS = Choices('draft', 'published') + + # ... + + a = Article() + a.status = Article.STATUS.published + + # this save will update a.status_changed + a.save() + + # this query will only return published articles: + Article.published.all() diff --git a/docs/usage.rst b/docs/usage.rst deleted file mode 100644 index 9462975..0000000 --- a/docs/usage.rst +++ /dev/null @@ -1,514 +0,0 @@ -===== -Usage -===== - -Fields -====== - -Choices -------- - -``Choices`` provides some conveniences for setting ``choices`` on a Django model field: - -.. code-block:: python - - from model_utils import Choices - - class Article(models.Model): - STATUS = Choices('draft', 'published') - # ... - status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20) - -A ``Choices`` object is initialized with any number of choices. In the -simplest case, each choice is a string; that string will be used both -as the database representation of the choice, and the human-readable -representation. Note that you can access options as attributes on the -``Choices`` object: ``STATUS.draft``. - -But you may want your human-readable versions translated, in which -case you need to separate the human-readable version from the DB -representation. In this case you can provide choices as two-tuples: - -.. code-block:: python - - from model_utils import Choices - - class Article(models.Model): - STATUS = Choices(('draft', _('draft')), ('published', _('published'))) - # ... - status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20) - -But what if your database representation of choices is constrained in -a way that would hinder readability of your code? For instance, you -may need to use an ``IntegerField`` rather than a ``CharField``, or -you may want the database to order the values in your field in some -specific way. In this case, you can provide your choices as triples, -where the first element is the database representation, the second is -a valid Python identifier you will use in your code as a constant, and -the third is the human-readable version: - -.. code-block:: python - - from model_utils import Choices - - class Article(models.Model): - STATUS = Choices((0, 'draft', _('draft')), (1, 'published', _('published'))) - # ... - status = models.IntegerField(choices=STATUS, default=STATUS.draft) - - -StatusField ------------ - -A simple convenience for giving a model a set of "states." -``StatusField`` is a ``CharField`` subclass that expects to find a -``STATUS`` class attribute on its model, and uses that as its -``choices``. Also sets a default ``max_length`` of 100, and sets its -default value to the first item in the ``STATUS`` choices: - -.. code-block:: python - - from model_utils.fields import StatusField - from model_utils import Choices - - class Article(models.Model): - STATUS = Choices('draft', 'published') - # ... - status = StatusField() - -(The ``STATUS`` class attribute does not have to be a `Choices`_ -instance, it can be an ordinary list of two-tuples). - -``StatusField`` does not set ``db_index=True`` automatically; if you -expect to frequently filter on your status field (and it will have -enough selectivity to make an index worthwhile) you may want to add this -yourself. - - -MonitorField ------------- - -A ``DateTimeField`` subclass that monitors another field on the model, -and updates itself to the current date-time whenever the monitored -field changes: - -.. code-block:: python - - from model_utils.fields import MonitorField, StatusField - - class Article(models.Model): - STATUS = Choices('draft', 'published') - - status = StatusField() - status_changed = MonitorField(monitor='status') - -(A ``MonitorField`` can monitor any type of field for changes, not only a -``StatusField``.) - - -SplitField ----------- - -A ``TextField`` subclass that automatically pulls an excerpt out of -its content (based on a "split here" marker or a default number of -initial paragraphs) and stores both its content and excerpt values in -the database. - -A ``SplitField`` is easy to add to any model definition: - -.. code-block:: python - - from django.db import models - from model_utils.fields import SplitField - - class Article(models.Model): - title = models.CharField(max_length=100) - body = SplitField() - -``SplitField`` automatically creates an extra non-editable field -``_body_excerpt`` to store the excerpt. This field doesn't need to be -accessed directly; see below. - - -Accessing a SplitField on a model -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -When accessing an attribute of a model that was declared as a -``SplitField``, a ``SplitText`` object is returned. The ``SplitText`` -object has three attributes: - -``content``: - The full field contents. -``excerpt``: - The excerpt of ``content`` (read-only). -``has_more``: - True if the excerpt and content are different, False otherwise. - -This object also has a ``__unicode__`` method that returns the full -content, allowing ``SplitField`` attributes to appear in templates -without having to access ``content`` directly. - -Assuming the ``Article`` model above: - -.. code-block:: pycon - - >>> a = Article.objects.all()[0] - >>> a.body.content - u'some text\n\n\n\nmore text' - >>> a.body.excerpt - u'some text\n' - >>> unicode(a.body) - u'some text\n\n\n\nmore text' - -Assignment to ``a.body`` is equivalent to assignment to -``a.body.content``. - -.. note:: - - a.body.excerpt is only updated when a.save() is called - - -Customized excerpting -~~~~~~~~~~~~~~~~~~~~~ - -By default, ``SplitField`` looks for the marker ```` -alone on a line and takes everything before that marker as the -excerpt. This marker can be customized by setting the ``SPLIT_MARKER`` -setting. - -If no marker is found in the content, the first two paragraphs (where -paragraphs are blocks of text separated by a blank line) are taken to -be the excerpt. This number can be customized by setting the -``SPLIT_DEFAULT_PARAGRAPHS`` setting. - - -Models -====== - -TimeFramedModel ---------------- - -An abstract base class for any model that expresses a time-range. Adds -``start`` and ``end`` nullable DateTimeFields, and a ``timeframed`` -manager that returns only objects for whom the current date-time lies -within their time range. - - -StatusModel ------------ - -Pulls together `StatusField`_, `MonitorField`_ and `QueryManager`_ -into an abstract base class for any model with a "status." - -Just provide a ``STATUS`` class-attribute (a `Choices`_ object or a -list of two-tuples), and your model will have a ``status`` field with -those choices, a ``status_changed`` field containing the date-time the -``status`` was last changed, and a manager for each status that -returns objects with that status only: - -.. code-block:: python - - from model_utils.models import StatusModel - from model_utils import Choices - - class Article(StatusModel): - STATUS = Choices('draft', 'published') - - # ... - - a = Article() - a.status = Article.STATUS.published - - # this save will update a.status_changed - a.save() - - # this query will only return published articles: - Article.published.all() - - -Model Managers -============== - -InheritanceManager ------------------- - -This manager (`contributed by Jeff Elmore`_) should be attached to a base model -class in a model-inheritance tree. It allows queries on that base model to -return heterogenous results of the actual proper subtypes, without any -additional queries. - -For instance, if you have a ``Place`` model with subclasses ``Restaurant`` and -``Bar``, you may want to query all Places: - -.. code-block:: python - - nearby_places = Place.objects.filter(location='here') - -But when you iterate over ``nearby_places``, you'll get only ``Place`` -instances back, even for objects that are "really" ``Restaurant`` or ``Bar``. -If you attach an ``InheritanceManager`` to ``Place``, you can just call the -``select_subclasses()`` method on the ``InheritanceManager`` or any -``QuerySet`` from it, and the resulting objects will be instances of -``Restaurant`` or ``Bar``: - -.. code-block:: python - - from model_utils.managers import InheritanceManager - - class Place(models.Model): - # ... - objects = InheritanceManager() - - class Restaurant(Place): - # ... - - class Bar(Place): - # ... - - nearby_places = Place.objects.filter(location='here').select_subclasses() - for place in nearby_places: - # "place" will automatically be an instance of Place, Restaurant, or Bar - -The database query performed will have an extra join for each subclass; if you -want to reduce the number of joins and you only need particular subclasses to -be returned as their actual type, you can pass subclass names to -``select_subclasses()``, much like the built-in ``select_related()`` method: - -.. code-block:: python - - nearby_places = Place.objects.select_subclasses("restaurant") - # restaurants will be Restaurant instances, bars will still be Place instances - -``InheritanceManager`` also provides a subclass-fetching alternative to the -``get()`` method: - -.. code-block:: python - - place = Place.objects.get_subclass(id=some_id) - # "place" will automatically be an instance of Place, Restaurant, or Bar - -If you don't explicitly call ``select_subclasses()`` or ``get_subclass()``, -an ``InheritanceManager`` behaves identically to a normal ``Manager``; so -it's safe to use as your default manager for the model. - -.. note:: - - Due to `Django bug #16572`_, on Django versions prior to 1.6 - ``InheritanceManager`` only supports a single level of model inheritance; - it won't work for grandchild models. - -.. _contributed by Jeff Elmore: http://jeffelmore.org/2010/11/11/automatic-downcasting-of-inherited-models-in-django/ -.. _Django bug #16572: https://code.djangoproject.com/ticket/16572 - - -TimeStampedModel ----------------- - -This abstract base class just provides self-updating ``created`` and -``modified`` fields on any model that inherits from it. - - -QueryManager ------------- - -Many custom model managers do nothing more than return a QuerySet that -is filtered in some way. ``QueryManager`` allows you to express this -pattern with a minimum of boilerplate: - -.. code-block:: python - - from django.db import models - from model_utils.managers import QueryManager - - class Post(models.Model): - ... - published = models.BooleanField() - pub_date = models.DateField() - ... - - objects = models.Manager() - public = QueryManager(published=True).order_by('-pub_date') - -The kwargs passed to ``QueryManager`` will be passed as-is to the -``QuerySet.filter()`` method. You can also pass a ``Q`` object to -``QueryManager`` to express more complex conditions. Note that you can -set the ordering of the ``QuerySet`` returned by the ``QueryManager`` -by chaining a call to ``.order_by()`` on the ``QueryManager`` (this is -not required). - - -PassThroughManager ------------------- - -A common "gotcha" when defining methods on a custom manager class is that those -same methods are not automatically also available on the QuerySets returned by -that manager, so are not "chainable". This can be counterintuitive, as most of -the public QuerySet API is mirrored on managers. It is possible to create a -custom Manager that returns QuerySets that have the same additional methods, -but this requires boilerplate code. The ``PassThroughManager`` class -(`contributed by Paul McLanahan`_) removes this boilerplate. - -.. _contributed by Paul McLanahan: http://paulm.us/post/3717466639/passthroughmanager-for-django - -To use ``PassThroughManager``, rather than defining a custom manager with -additional methods, define a custom ``QuerySet`` subclass with the additional -methods you want, and pass that ``QuerySet`` subclass to the -``PassThroughManager.for_queryset_class()`` class method. The returned -``PassThroughManager`` subclass will always return instances of your custom -``QuerySet``, and you can also call methods of your custom ``QuerySet`` -directly on the manager: - -.. code-block:: python - - from datetime import datetime - from django.db import models - from django.db.models.query import QuerySet - from model_utils.managers import PassThroughManager - - class PostQuerySet(QuerySet): - def by_author(self, user): - return self.filter(user=user) - - def published(self): - return self.filter(published__lte=datetime.now()) - - def unpublished(self): - return self.filter(published__gte=datetime.now()) - - - class Post(models.Model): - user = models.ForeignKey(User) - published = models.DateTimeField() - - objects = PassThroughManager.for_queryset_class(PostQuerySet)() - - Post.objects.published() - Post.objects.by_author(user=request.user).unpublished() - - -Field Tracker -============= - -A ``FieldTracker`` can be added to a model to track changes in model fields. A -``FieldTracker`` allows querying for field changes since a model instance was -last saved. An example of applying ``FieldTracker`` to a model: - -.. code-block:: python - - from django.db import models - from model_utils import FieldTracker - - class Post(models.Model): - title = models.CharField(max_length=100) - body = models.TextField() - - tracker = FieldTracker() - -.. note:: - - ``django-model-utils`` 1.3.0 introduced the ``ModelTracker`` object for - tracking changes to model field values. Unfortunately ``ModelTracker`` - suffered from some serious flaws in its handling of ``ForeignKey`` fields, - potentially resulting in many extra database queries if a ``ForeignKey`` - field was tracked. In order to avoid breaking API backwards-compatibility, - ``ModelTracker`` retains the previous behavior but is deprecated, and - ``FieldTracker`` has been introduced to provide better ``ForeignKey`` - handling. All uses of ``ModelTracker`` should be replaced by - ``FieldTracker``. - - Summary of differences between ``ModelTracker`` and ``FieldTracker``: - - * The previous value returned for a tracked ``ForeignKey`` field will now - be the raw ID rather than the full object (avoiding extra database - queries). (GH-43) - - * The ``changed()`` method no longer returns the empty dictionary for all - unsaved instances; rather, ``None`` is considered to be the initial value - of all fields if the model has never been saved, thus ``changed()`` on an - unsaved instance will return a dictionary containing all fields whose - current value is not ``None``. - - * The ``has_changed()`` method no longer crashes after an object's first - save. (GH-53). - - -Accessing a field tracker -------------------------- - -There are multiple methods available for checking for changes in model fields. - - -previous -~~~~~~~~ -Returns the value of the given field during the last save: - -.. code-block:: pycon - - >>> a = Post.objects.create(title='First Post') - >>> a.title = 'Welcome' - >>> a.tracker.previous('title') - u'First Post' - -Returns ``None`` when the model instance isn't saved yet. - - -has_changed -~~~~~~~~~~~ -Returns ``True`` if the given field has changed since the last save: - -.. code-block:: pycon - - >>> a = Post.objects.create(title='First Post') - >>> a.title = 'Welcome' - >>> a.tracker.has_changed('title') - True - >>> a.tracker.has_changed('body') - False - -The ``has_changed`` method relies on ``previous`` to determine whether a -field's values has changed. - - -changed -~~~~~~~ -Returns a dictionary of all fields that have been changed since the last save -and the values of the fields during the last save: - -.. code-block:: pycon - - >>> a = Post.objects.create(title='First Post') - >>> a.title = 'Welcome' - >>> a.body = 'First post!' - >>> a.tracker.changed() - {'title': 'First Post', 'body': ''} - -The ``changed`` method relies on ``has_changed`` to determine which fields -have changed. - - -Tracking specific fields ------------------------- - -A fields parameter can be given to ``FieldTracker`` to limit tracking to -specific fields: - -.. code-block:: python - - from django.db import models - from model_utils import FieldTracker - - class Post(models.Model): - title = models.CharField(max_length=100) - body = models.TextField() - - title_tracker = FieldTracker(fields=['title']) - -An example using the model specified above: - -.. code-block:: pycon - - >>> a = Post.objects.create(title='First Post') - >>> a.body = 'First post!' - >>> a.title_tracker.changed() - {'title': None} - diff --git a/docs/utilities.rst b/docs/utilities.rst new file mode 100644 index 0000000..fd8fc0c --- /dev/null +++ b/docs/utilities.rst @@ -0,0 +1,181 @@ +======================= +Miscellaneous Utilities +======================= + +Choices +======= + +``Choices`` provides some conveniences for setting ``choices`` on a Django model field: + +.. code-block:: python + + from model_utils import Choices + + class Article(models.Model): + STATUS = Choices('draft', 'published') + # ... + status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20) + +A ``Choices`` object is initialized with any number of choices. In the +simplest case, each choice is a string; that string will be used both +as the database representation of the choice, and the human-readable +representation. Note that you can access options as attributes on the +``Choices`` object: ``STATUS.draft``. + +But you may want your human-readable versions translated, in which +case you need to separate the human-readable version from the DB +representation. In this case you can provide choices as two-tuples: + +.. code-block:: python + + from model_utils import Choices + + class Article(models.Model): + STATUS = Choices(('draft', _('draft')), ('published', _('published'))) + # ... + status = models.CharField(choices=STATUS, default=STATUS.draft, max_length=20) + +But what if your database representation of choices is constrained in +a way that would hinder readability of your code? For instance, you +may need to use an ``IntegerField`` rather than a ``CharField``, or +you may want the database to order the values in your field in some +specific way. In this case, you can provide your choices as triples, +where the first element is the database representation, the second is +a valid Python identifier you will use in your code as a constant, and +the third is the human-readable version: + +.. code-block:: python + + from model_utils import Choices + + class Article(models.Model): + STATUS = Choices((0, 'draft', _('draft')), (1, 'published', _('published'))) + # ... + status = models.IntegerField(choices=STATUS, default=STATUS.draft) + + +Field Tracker +============= + +A ``FieldTracker`` can be added to a model to track changes in model fields. A +``FieldTracker`` allows querying for field changes since a model instance was +last saved. An example of applying ``FieldTracker`` to a model: + +.. code-block:: python + + from django.db import models + from model_utils import FieldTracker + + class Post(models.Model): + title = models.CharField(max_length=100) + body = models.TextField() + + tracker = FieldTracker() + +.. note:: + + ``django-model-utils`` 1.3.0 introduced the ``ModelTracker`` object for + tracking changes to model field values. Unfortunately ``ModelTracker`` + suffered from some serious flaws in its handling of ``ForeignKey`` fields, + potentially resulting in many extra database queries if a ``ForeignKey`` + field was tracked. In order to avoid breaking API backwards-compatibility, + ``ModelTracker`` retains the previous behavior but is deprecated, and + ``FieldTracker`` has been introduced to provide better ``ForeignKey`` + handling. All uses of ``ModelTracker`` should be replaced by + ``FieldTracker``. + + Summary of differences between ``ModelTracker`` and ``FieldTracker``: + + * The previous value returned for a tracked ``ForeignKey`` field will now + be the raw ID rather than the full object (avoiding extra database + queries). (GH-43) + + * The ``changed()`` method no longer returns the empty dictionary for all + unsaved instances; rather, ``None`` is considered to be the initial value + of all fields if the model has never been saved, thus ``changed()`` on an + unsaved instance will return a dictionary containing all fields whose + current value is not ``None``. + + * The ``has_changed()`` method no longer crashes after an object's first + save. (GH-53). + + +Accessing a field tracker +------------------------- + +There are multiple methods available for checking for changes in model fields. + + +previous +~~~~~~~~ +Returns the value of the given field during the last save: + +.. code-block:: pycon + + >>> a = Post.objects.create(title='First Post') + >>> a.title = 'Welcome' + >>> a.tracker.previous('title') + u'First Post' + +Returns ``None`` when the model instance isn't saved yet. + + +has_changed +~~~~~~~~~~~ +Returns ``True`` if the given field has changed since the last save: + +.. code-block:: pycon + + >>> a = Post.objects.create(title='First Post') + >>> a.title = 'Welcome' + >>> a.tracker.has_changed('title') + True + >>> a.tracker.has_changed('body') + False + +The ``has_changed`` method relies on ``previous`` to determine whether a +field's values has changed. + + +changed +~~~~~~~ +Returns a dictionary of all fields that have been changed since the last save +and the values of the fields during the last save: + +.. code-block:: pycon + + >>> a = Post.objects.create(title='First Post') + >>> a.title = 'Welcome' + >>> a.body = 'First post!' + >>> a.tracker.changed() + {'title': 'First Post', 'body': ''} + +The ``changed`` method relies on ``has_changed`` to determine which fields +have changed. + + +Tracking specific fields +------------------------ + +A fields parameter can be given to ``FieldTracker`` to limit tracking to +specific fields: + +.. code-block:: python + + from django.db import models + from model_utils import FieldTracker + + class Post(models.Model): + title = models.CharField(max_length=100) + body = models.TextField() + + title_tracker = FieldTracker(fields=['title']) + +An example using the model specified above: + +.. code-block:: pycon + + >>> a = Post.objects.create(title='First Post') + >>> a.body = 'First post!' + >>> a.title_tracker.changed() + {'title': None} From 1bdf90be20cefc4f731f8326b10f34b3b4208293 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Tue, 6 Aug 2013 20:47:22 -0700 Subject: [PATCH 22/33] Add explicit references for cross-doc file links --- docs/fields.rst | 6 +++++- docs/managers.rst | 2 ++ docs/models.rst | 4 ++-- docs/utilities.rst | 2 ++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/fields.rst b/docs/fields.rst index c03c9fb..9a0dbaf 100644 --- a/docs/fields.rst +++ b/docs/fields.rst @@ -1,6 +1,8 @@ Fields ====== +.. _StatusField: + StatusField ----------- @@ -20,7 +22,7 @@ default value to the first item in the ``STATUS`` choices: # ... status = StatusField() -(The ``STATUS`` class attribute does not have to be a `Choices`_ +(The ``STATUS`` class attribute does not have to be a :ref:`Choices` instance, it can be an ordinary list of two-tuples). ``StatusField`` does not set ``db_index=True`` automatically; if you @@ -29,6 +31,8 @@ enough selectivity to make an index worthwhile) you may want to add this yourself. +.. _MonitorField: + MonitorField ------------ diff --git a/docs/managers.rst b/docs/managers.rst index 257a878..d2c074c 100644 --- a/docs/managers.rst +++ b/docs/managers.rst @@ -80,6 +80,8 @@ This abstract base class just provides self-updating ``created`` and ``modified`` fields on any model that inherits from it. +.. _QueryManager: + QueryManager ------------ diff --git a/docs/models.rst b/docs/models.rst index 0627e8e..b087ee0 100644 --- a/docs/models.rst +++ b/docs/models.rst @@ -13,10 +13,10 @@ within their time range. StatusModel ----------- -Pulls together `StatusField`_, `MonitorField`_ and `QueryManager`_ +Pulls together :ref:`StatusField`, :ref:`MonitorField` and :ref:`QueryManager` into an abstract base class for any model with a "status." -Just provide a ``STATUS`` class-attribute (a `Choices`_ object or a +Just provide a ``STATUS`` class-attribute (a :ref:`Choices` object or a list of two-tuples), and your model will have a ``status`` field with those choices, a ``status_changed`` field containing the date-time the ``status`` was last changed, and a manager for each status that diff --git a/docs/utilities.rst b/docs/utilities.rst index fd8fc0c..0c3ca61 100644 --- a/docs/utilities.rst +++ b/docs/utilities.rst @@ -2,6 +2,8 @@ Miscellaneous Utilities ======================= +.. _Choices: + Choices ======= From 0197c9e0f8023254c1ed9b5d4258a38e7bf58100 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 7 Aug 2013 00:28:05 -0700 Subject: [PATCH 23/33] Link to documentation and PyPI in README --- README.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.rst b/README.rst index 64e17ae..ccc554c 100644 --- a/README.rst +++ b/README.rst @@ -11,6 +11,21 @@ django-model-utils Django model mixins and utilities. +``django-model-utils`` supports `Django`_ 1.4.2 and later on Python 2.6, 2.7, +3.2, and 3.3. + +.. _Django: http://www.djangoproject.com/ + + +Getting Help +============ + +Documentation for django-model-utils is available at https://django-model-utils.readthedocs.org/ + +This app is available on `PyPI`_. + +.. _PyPI: https://pypi.python.org/pypi/django-model-utils/ + Contributing ============ From ed011eaeaa302b085d776de8679328a1ae8484c5 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 7 Aug 2013 00:33:13 -0700 Subject: [PATCH 24/33] Add Makefile with init, test, and docs tasks --- Makefile | 15 +++++++++++++++ runtests.sh | 5 ----- 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 Makefile delete mode 100755 runtests.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c736206 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +all: init docs test + +init: + python setup.py develop + pip install tox coverage Sphinx + +test: + coverage erase + tox + coverage html + +docs: documentation + +documentation: + python setup.py build_sphinx diff --git a/runtests.sh b/runtests.sh deleted file mode 100755 index a105e34..0000000 --- a/runtests.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -coverage erase -tox -coverage html From a7c6ff939291dc33463a17a6813d7772b00a9311 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 7 Aug 2013 01:07:31 -0700 Subject: [PATCH 25/33] Set version in setup.py docs automatically --- docs/conf.py | 14 +++++++++++--- model_utils/__init__.py | 2 ++ setup.py | 10 +++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 15eaebf..d79adff 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -43,14 +43,22 @@ master_doc = 'index' project = u'django-model-utils' copyright = u'2013, Carl Meyer' +parent_dir = os.path.dirname(os.path.dirname(__file__)) + +def get_version(): + with open(os.path.join(parent_dir, 'model_utils', '__init__.py')) as f: + for line in f: + if line.startswith('__version__ ='): + return line.split('=')[1].strip().strip('"\'') + # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # -# The short X.Y version. -version = '1.4' # The full version, including alpha/beta/rc tags. -release = '1.4.0' +release = get_version() +# The short X.Y version. +version = release # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/model_utils/__init__.py b/model_utils/__init__.py index 731e9b7..36586ec 100644 --- a/model_utils/__init__.py +++ b/model_utils/__init__.py @@ -1,2 +1,4 @@ from .choices import Choices from .tracker import FieldTracker, ModelTracker + +__version__ = '1.4.0.post1' diff --git a/setup.py b/setup.py index 2405f90..a7ceb1c 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,4 @@ +from os.path import join from setuptools import setup, find_packages @@ -6,9 +7,16 @@ long_description = (open('README.rst').read() + open('TODO.rst').read()) +def get_version(): + with open(join('model_utils', '__init__.py')) as f: + for line in f: + if line.startswith('__version__ ='): + return line.split('=')[1].strip().strip('"\'') + + setup( name='django-model-utils', - version='1.4.0.post1', + version=get_version(), description='Django model mixins and utilities', long_description=long_description, author='Carl Meyer', From 6e397303f842c3470321720ef8d6ec7b3315d69d Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 7 Aug 2013 01:19:11 -0700 Subject: [PATCH 26/33] Reference `make test` instead of `./runtests.sh` --- CONTRIBUTING.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 70d48bb..fad38ba 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -42,7 +42,7 @@ First install tox:: To run tox and generate a coverage report (in ``htmlcov`` directory):: - ./runtests.sh + make test **Please note**: Before a pull request can be merged, all tests must pass and code/branch coverage in tests must be 100%. From b9f954074c4988b4f6ffc02991a92e9286c469c6 Mon Sep 17 00:00:00 2001 From: Mikhail Silonov Date: Thu, 8 Aug 2013 13:18:33 +0400 Subject: [PATCH 27/33] Fixed a bug causing `KeyError` when saving with the parameter `update_fields` in which there are untracked fields. --- AUTHORS.rst | 1 + CHANGES.rst | 3 +++ model_utils/tests/tests.py | 12 +++++++++++- model_utils/tracker.py | 16 ++++++++++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 3a33840..5ea4eba 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -19,3 +19,4 @@ Simon Meers sayane Trey Hunner zyegfryed +Mikhail Silonov diff --git a/CHANGES.rst b/CHANGES.rst index 9b3bb75..44d3959 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,9 @@ master (unreleased) * `Choices` now `__contains__` its Python identifier values. Thanks Keryn Knight. (Merge of GH-69). +* Fixed a bug causing ``KeyError`` when saving with the parameter + ``update_fields`` in which there are untracked fields. + 1.4.0 (2013.06.03) ------------------ diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index ab60a62..b85b08d 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -923,6 +923,16 @@ class FieldTrackedModelCustomTests(FieldTrackerTestCase, self.instance.save() self.assertCurrent(name='new age') + def test_update_fields(self): + # Django 1.4 doesn't have update_fields + if django.VERSION >= (1, 5, 0): + self.update_instance(name='retro', number=4) + self.assertChanged() + self.instance.name = 'new age' + self.instance.number = 8 + self.instance.save(update_fields=['name', 'number']) + self.assertChanged() + class FieldTrackedModelAttributeTests(FieldTrackerTestCase): @@ -976,7 +986,7 @@ class FieldTrackedModelAttributeTests(FieldTrackerTestCase): class FieldTrackedModelMultiTests(FieldTrackerTestCase, - FieldTrackerCommonTests): + FieldTrackerCommonTests): tracked_class = TrackedMultiple diff --git a/model_utils/tracker.py b/model_utils/tracker.py index 31fd0f2..aecf27c 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -68,7 +68,8 @@ class FieldTracker(object): def finalize_class(self, sender, **kwargs): if self.fields is None: - self.fields = [field.attname for field in sender._meta.local_fields] + self.fields = (field.attname for field in sender._meta.local_fields) + self.fields = set(self.fields) self.field_map = self.get_field_map(sender) models.signals.post_init.connect(self.initialize_tracker, sender=sender) setattr(sender, self.name, self) @@ -83,8 +84,19 @@ class FieldTracker(object): original_save = instance.save def save(**kwargs): ret = original_save(**kwargs) + update_fields = kwargs.get('update_fields') + if not update_fields and update_fields is not None: # () or [] + fields = update_fields + elif update_fields is None: + fields = None + else: + fields = ( + field for field in update_fields if + field in self.fields + ) getattr(instance, self.attname).set_saved_fields( - fields=kwargs.get('update_fields')) + fields=fields + ) return ret instance.save = save From 8c6f3431112f3a2a7c9868cfa70603b5c29877f0 Mon Sep 17 00:00:00 2001 From: Mikhail Silonov Date: Thu, 8 Aug 2013 18:02:12 +0400 Subject: [PATCH 28/33] Added JSON Fields support --- AUTHORS.rst | 1 + CHANGES.rst | 2 + model_utils/tests/fields.py | 30 +++++++ model_utils/tests/models.py | 10 +++ model_utils/tests/tests.py | 171 +++++++++++++++++++++++++++++++++++- model_utils/tracker.py | 12 ++- 6 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 model_utils/tests/fields.py diff --git a/AUTHORS.rst b/AUTHORS.rst index 3a33840..5ea4eba 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -19,3 +19,4 @@ Simon Meers sayane Trey Hunner zyegfryed +Mikhail Silonov diff --git a/CHANGES.rst b/CHANGES.rst index 9b3bb75..6f9defa 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,8 @@ master (unreleased) * `Choices` now `__contains__` its Python identifier values. Thanks Keryn Knight. (Merge of GH-69). +* Added JSON Fields support. + 1.4.0 (2013.06.03) ------------------ diff --git a/model_utils/tests/fields.py b/model_utils/tests/fields.py new file mode 100644 index 0000000..b0cdcad --- /dev/null +++ b/model_utils/tests/fields.py @@ -0,0 +1,30 @@ +import json + +from django.db import models +from django.core.serializers.json import DjangoJSONEncoder + + +class SimpleJSONField(models.TextField): + + __metaclass__ = models.SubfieldBase + + def to_python(self, value): + if value == "": + return None + + try: + if isinstance(value, basestring): + return json.loads(value) + except ValueError: + pass + + return value + + def get_db_prep_save(self, value, connection): + if value == "": + return None + + if isinstance(value, dict): + value = json.dumps(value, cls=DjangoJSONEncoder) + + return super(SimpleJSONField, self).get_db_prep_save(value, connection) diff --git a/model_utils/tests/models.py b/model_utils/tests/models.py index 99d33ad..af8ff95 100644 --- a/model_utils/tests/models.py +++ b/model_utils/tests/models.py @@ -5,6 +5,7 @@ from model_utils.models import TimeStampedModel, StatusModel, TimeFramedModel from model_utils.tracker import FieldTracker, ModelTracker from model_utils.managers import QueryManager, InheritanceManager, PassThroughManager from model_utils.fields import SplitField, MonitorField, StatusField +from model_utils.tests.fields import SimpleJSONField from model_utils import Choices @@ -271,6 +272,15 @@ class TrackedMultiple(models.Model): number_tracker = FieldTracker(fields=['number']) +class TrackedWithJsonField(models.Model): + name = models.CharField(max_length=20) + number = models.IntegerField() + + props = SimpleJSONField() + + tracker = FieldTracker() + + class ModelTracked(models.Model): name = models.CharField(max_length=20) number = models.IntegerField() diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index ab60a62..779209c 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -26,9 +26,9 @@ from model_utils.tests.models import ( StatusPlainTuple, TimeFrame, Monitored, StatusManagerAdded, TimeFrameManagerAdded, Dude, SplitFieldAbstractParent, Car, Spot, ModelTracked, ModelTrackedFK, ModelTrackedNotDefault, ModelTrackedMultiple, - Tracked, TrackedFK, TrackedNotDefault, TrackedNonFieldAttr, - TrackedMultiple, StatusFieldDefaultFilled, StatusFieldDefaultNotFilled) - + Tracked, TrackedFK, TrackedNotDefault, TrackedWithJsonField, + TrackedNonFieldAttr, TrackedMultiple, StatusFieldDefaultFilled, + StatusFieldDefaultNotFilled) class GetExcerptTests(TestCase): @@ -924,6 +924,171 @@ class FieldTrackedModelCustomTests(FieldTrackerTestCase, self.assertCurrent(name='new age') +class JSONFieldTrackedModelTests(FieldTrackerTestCase): + + tracked_class = TrackedWithJsonField + + def setUp(self): + self.instance = self.tracked_class() + self.tracker = self.instance.tracker + + def test_pre_save_changed(self): + self.assertChanged(name=None) + self.instance.name = 'new age' + self.assertChanged(name=None) + self.instance.number = 8 + self.assertChanged(name=None, number=None) + self.instance.name = '' + self.assertChanged(name=None, number=None) + self.instance.props = {'attr': 1} + self.assertChanged(name=None, number=None, props=None) + + def test_first_save(self): + self.assertHasChanged(name=True) + self.assertPrevious(name=None, number=None, props=None) + self.assertCurrent(name='', number=None, props=None, id=None) + self.assertChanged(name=None) + self.instance.name = 'retro' + self.instance.number = 4 + self.instance.props = {'vodka': True} + self.assertHasChanged(name=True, number=True, props=True) + self.assertPrevious(name=None, number=None, props=None) + self.assertCurrent(name='retro', number=4, + props={'vodka': True}, id=None) + self.assertChanged(name=None, number=None, props=None) + + def test_pre_save_has_changed(self): + self.assertHasChanged(name=True) + self.instance.name = 'new age' + self.assertHasChanged(name=True) + self.instance.number = 7 + self.assertHasChanged(name=True, number=True) + self.instance.props = {'bears_on_red_square': False} + self.assertChanged(name=None, number=None, props=None) + + def test_post_save_has_changed(self): + self.update_instance( + name='retro', number=4, + props={ + 'goodies': { + 'balalaika': True, + 'Topol-M': True + } + } + ) + self.assertHasChanged(name=False, number=False, props=False) + self.instance.name = 'new age' + self.assertHasChanged(name=True, number=False, props=False) + self.instance.number = 8 + self.assertHasChanged(name=True, number=True) + self.instance.name = 'retro' + self.assertHasChanged(name=False, number=True) + self.instance.props = { + 'goodies': { + 'balalaika': False, + 'Topol-M': True + } + } + self.assertHasChanged(name=False, number=True, props=True) + + def test_post_save_previous(self): + self.update_instance( + name='retro', number=4, + props={ + 'goodies': { + 'balalaika': True, + 'Topol-M': True + } + } + ) + self.instance.name = 'new age' + self.instance.props = { + 'goodies': { + 'balalaika': False, + 'Topol-M': True + } + } + self.assertPrevious( + name='retro', + number=4, + props={ + 'goodies': { + 'balalaika': True, + 'Topol-M': True + } + } + ) + + def test_post_save_changed(self): + self.update_instance( + name='retro', number=4, + props={ + 'goodies': { + 'balalaika': True, + 'Topol-M': True + } + } + ) + self.assertChanged() + self.instance.name = 'new age' + self.assertChanged(name='retro') + self.instance.number = 8 + self.assertChanged(name='retro', number=4) + self.instance.name = 'retro' + self.assertChanged(number=4) + self.instance.props = { + 'goodies': { + 'balalaika': False, + 'Topol-M': True + } + } + self.assertChanged( + number=4, + props={ + 'goodies': { + 'balalaika': True, + 'Topol-M': True + } + } + ) + + def test_current(self): + self.assertCurrent(name='', number=None, props=None, id=None) + self.instance.name = 'new age' + self.assertCurrent(name='new age', number=None, props=None, id=None) + self.instance.number = 8 + self.assertCurrent(name='new age', number=8, props=None, id=None) + self.instance.props = { + 'goodies': { + 'balalaika': False, + 'Topol-M': True + } + } + self.assertCurrent( + name='new age', + number=8, + props={ + 'goodies': { + 'balalaika': False, + 'Topol-M': True + } + }, + id=None + ) + self.instance.save() + self.assertCurrent( + name='new age', + number=8, + props={ + 'goodies': { + 'balalaika': False, + 'Topol-M': True + } + }, + id=self.instance.id + ) + + class FieldTrackedModelAttributeTests(FieldTrackerTestCase): tracked_class = TrackedNonFieldAttr diff --git a/model_utils/tracker.py b/model_utils/tracker.py index 31fd0f2..a302c99 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -1,4 +1,7 @@ from __future__ import unicode_literals + +from copy import deepcopy + from django.db import models from django.core.exceptions import FieldError @@ -19,6 +22,7 @@ class FieldInstanceTracker(object): self.saved_data = self.current() else: self.saved_data.update(**self.current(fields=fields)) + return self.saved_data def current(self, fields=None): """Return dict of current values for all tracked fields""" @@ -76,7 +80,8 @@ class FieldTracker(object): def initialize_tracker(self, sender, instance, **kwargs): tracker = self.tracker_class(instance, self.fields, self.field_map) setattr(instance, self.attname, tracker) - tracker.set_saved_fields() + saved_data = tracker.set_saved_fields() + self.prevent_side_effects(saved_data) self.patch_save(instance) def patch_save(self, instance): @@ -88,6 +93,11 @@ class FieldTracker(object): return ret instance.save = save + def prevent_side_effects(self, saved_data): + for field, field_value in saved_data.items(): + if isinstance(field_value, dict): + saved_data[field] = deepcopy(field_value) + def __get__(self, instance, owner): if instance is None: return self From 9a3abce7f54ec0792524db4cd2162220288d9843 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 8 Aug 2013 09:43:46 -0600 Subject: [PATCH 29/33] Update testing idioms. --- model_utils/tests/tests.py | 135 ++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 70 deletions(-) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index b85b08d..01827b8 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -106,15 +106,13 @@ class SplitFieldTests(TestCase): def test_assign_to_excerpt(self): - def _invalid_assignment(): + with self.assertRaises(AttributeError): self.post.body.excerpt = 'this should fail' - self.assertRaises(AttributeError, _invalid_assignment) def test_access_via_class(self): - def _invalid_access(): + with self.assertRaises(AttributeError): Article.body - self.assertRaises(AttributeError, _invalid_access) def test_none(self): @@ -169,7 +167,8 @@ class MonitorFieldTests(TestCase): def test_no_monitor_arg(self): - self.assertRaises(TypeError, MonitorField) + with self.assertRaises(TypeError): + MonitorField() class StatusFieldTests(TestCase): @@ -221,7 +220,8 @@ class ChoicesTests(TestCase): def test_wrong_length_tuple(self): - self.assertRaises(ValueError, Choices, ('a',)) + with self.assertRaises(ValueError): + Choices(('a',)) def test_contains_value(self): self.assertTrue('PUBLISHED' in self.STATUS) @@ -385,23 +385,23 @@ class InheritanceManagerTests(TestCase): ) + @skipUnless(django.VERSION >= (1, 6, 0), "test only applies to Django 1.6+") def test_select_specific_grandchildren(self): - if django.VERSION >= (1, 6, 0): - children = set([ - InheritanceManagerTestParent(pk=self.child1.pk), - InheritanceManagerTestParent(pk=self.child2.pk), - self.grandchild1, - InheritanceManagerTestParent(pk=self.grandchild1_2.pk), - ]) - self.assertEqual( - set( - self.get_manager().select_subclasses( - "inheritancemanagertestchild1__" - "inheritancemanagertestgrandchild1" - ) - ), - children, - ) + children = set([ + InheritanceManagerTestParent(pk=self.child1.pk), + InheritanceManagerTestParent(pk=self.child2.pk), + self.grandchild1, + InheritanceManagerTestParent(pk=self.grandchild1_2.pk), + ]) + self.assertEqual( + set( + self.get_manager().select_subclasses( + "inheritancemanagertestchild1__" + "inheritancemanagertestgrandchild1" + ) + ), + children, + ) def test_get_subclass(self): @@ -411,13 +411,11 @@ class InheritanceManagerTests(TestCase): def test_prior_select_related(self): - # Django 1.2 doesn't have assertNumQueries - if django.VERSION >= (1, 3): - with self.assertNumQueries(1): - obj = self.get_manager().select_related( - "inheritancemanagertestchild1").select_subclasses( - "inheritancemanagertestchild2").get(pk=self.child1.pk) - obj.inheritancemanagertestchild1 + with self.assertNumQueries(1): + obj = self.get_manager().select_related( + "inheritancemanagertestchild1").select_subclasses( + "inheritancemanagertestchild2").get(pk=self.child1.pk) + obj.inheritancemanagertestchild1 @@ -521,10 +519,9 @@ class TimeFrameManagerAddedTests(TestCase): def test_conflict_error(self): - def _run(): + with self.assertRaises(ImproperlyConfigured): class ErrorModel(TimeFramedModel): timeframed = models.BooleanField() - self.assertRaises(ImproperlyConfigured, _run) @@ -575,14 +572,13 @@ class StatusManagerAddedTests(TestCase): def test_conflict_error(self): - def _run(): + with self.assertRaises(ImproperlyConfigured): class ErrorModel(StatusModel): STATUS = ( ('active', 'active'), ('deleted', 'deleted'), ) active = models.BooleanField() - self.assertRaises(ImproperlyConfigured, _run) @@ -629,9 +625,8 @@ class SouthFreezingTests(TestCase): def test_no_excerpt_field_works(self): from .models import NoRendered - self.assertRaises(FieldDoesNotExist, - NoRendered._meta.get_field, - '_body_excerpt') + with self.assertRaises(FieldDoesNotExist): + NoRendered._meta.get_field('_body_excerpt') def test_status_field_no_check_for_status(self): sf = StatusFieldDefaultFilled._meta.get_field('status') @@ -658,9 +653,8 @@ class PassThroughManagerTests(TestCase): def test_manager_only_methods(self): stats = Dude.abiders.get_stats() self.assertEqual(stats['rug_count'], 1) - def notonqs(): + with self.assertRaises(AttributeError): Dude.abiders.all().get_stats() - self.assertRaises(AttributeError, notonqs) def test_queryset_pickling(self): @@ -716,7 +710,8 @@ class FieldTrackerTestCase(TestCase): tracker = kwargs.pop('tracker', self.tracker) for field, value in kwargs.items(): if value is None: - self.assertRaises(FieldError, tracker.has_changed, field) + with self.assertRaises(FieldError): + tracker.has_changed(field) else: self.assertEqual(tracker.has_changed(field), value) @@ -793,8 +788,8 @@ class FieldTrackerTests(FieldTrackerTestCase, FieldTrackerCommonTests): self.assertPrevious(name=None, number=None) self.assertCurrent(name='retro', number=4, id=None) self.assertChanged(name=None, number=None) - self.assertRaises(ValueError, self.instance.save, - update_fields=['number']) + with self.assertRaises(ValueError): + self.instance.save(update_fields=['number']) def test_post_save_has_changed(self): self.update_instance(name='retro', number=4) @@ -830,26 +825,26 @@ class FieldTrackerTests(FieldTrackerTestCase, FieldTrackerCommonTests): self.instance.save() self.assertCurrent(id=self.instance.id, name='new age', number=8) + @skipUnless( + django.VERSION >= (1, 5, 0), "Django 1.4 doesn't have update_fields") def test_update_fields(self): - # Django 1.4 doesn't have update_fields - if django.VERSION >= (1, 5, 0): - self.update_instance(name='retro', number=4) - self.assertChanged() - self.instance.name = 'new age' - self.instance.number = 8 - self.assertChanged(name='retro', number=4) - self.instance.save(update_fields=[]) - self.assertChanged(name='retro', number=4) - self.instance.save(update_fields=['name']) - in_db = self.tracked_class.objects.get(id=self.instance.id) - self.assertEqual(in_db.name, self.instance.name) - self.assertNotEqual(in_db.number, self.instance.number) - self.assertChanged(number=4) - self.instance.save(update_fields=['number']) - self.assertChanged() - in_db = self.tracked_class.objects.get(id=self.instance.id) - self.assertEqual(in_db.name, self.instance.name) - self.assertEqual(in_db.number, self.instance.number) + self.update_instance(name='retro', number=4) + self.assertChanged() + self.instance.name = 'new age' + self.instance.number = 8 + self.assertChanged(name='retro', number=4) + self.instance.save(update_fields=[]) + self.assertChanged(name='retro', number=4) + self.instance.save(update_fields=['name']) + in_db = self.tracked_class.objects.get(id=self.instance.id) + self.assertEqual(in_db.name, self.instance.name) + self.assertNotEqual(in_db.number, self.instance.number) + self.assertChanged(number=4) + self.instance.save(update_fields=['number']) + self.assertChanged() + in_db = self.tracked_class.objects.get(id=self.instance.id) + self.assertEqual(in_db.name, self.instance.name) + self.assertEqual(in_db.number, self.instance.number) class FieldTrackedModelCustomTests(FieldTrackerTestCase, @@ -923,15 +918,15 @@ class FieldTrackedModelCustomTests(FieldTrackerTestCase, self.instance.save() self.assertCurrent(name='new age') + @skipUnless( + django.VERSION >= (1, 5, 0), "Django 1.4 doesn't have update_fields") def test_update_fields(self): - # Django 1.4 doesn't have update_fields - if django.VERSION >= (1, 5, 0): - self.update_instance(name='retro', number=4) - self.assertChanged() - self.instance.name = 'new age' - self.instance.number = 8 - self.instance.save(update_fields=['name', 'number']) - self.assertChanged() + self.update_instance(name='retro', number=4) + self.assertChanged() + self.instance.name = 'new age' + self.instance.number = 8 + self.instance.save(update_fields=['name', 'number']) + self.assertChanged() class FieldTrackedModelAttributeTests(FieldTrackerTestCase): @@ -1150,8 +1145,8 @@ class ModelTrackerTests(FieldTrackerTests): self.assertPrevious(name=None, number=None) self.assertCurrent(name='retro', number=4, id=None) self.assertChanged() - self.assertRaises(ValueError, self.instance.save, - update_fields=['number']) + with self.assertRaises(ValueError): + self.instance.save(update_fields=['number']) def test_pre_save_has_changed(self): self.assertHasChanged(name=True, number=True) From 2eacb11b45af5a56a7e5054c17acd45f62a54f4e Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 8 Aug 2013 09:45:49 -0600 Subject: [PATCH 30/33] Tweak AUTHOR list and changelog. --- AUTHORS.rst | 4 ++-- CHANGES.rst | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 5ea4eba..f1a3ea3 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -6,12 +6,13 @@ Donald Stufft Facundo Gaich Felipe Prenholato Gregor Müllegger +ivirabyan James Oakley Jannis Leidel Javier García Sogo Jeff Elmore Keryn Knight -ivirabyan +Mikhail Silonov Paul McLanahan Rinat Shigapov Ryan Kaskel @@ -19,4 +20,3 @@ Simon Meers sayane Trey Hunner zyegfryed -Mikhail Silonov diff --git a/CHANGES.rst b/CHANGES.rst index 44d3959..b6759da 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,7 +8,8 @@ master (unreleased) Knight. (Merge of GH-69). * Fixed a bug causing ``KeyError`` when saving with the parameter - ``update_fields`` in which there are untracked fields. + ``update_fields`` in which there are untracked fields. Thanks Mikhail + Silonov. (Merge of GH-70, fixes GH-71). 1.4.0 (2013.06.03) From f20e723952fd9c2aefb0f6ca69c6528d08681a1d Mon Sep 17 00:00:00 2001 From: silonov Date: Sun, 11 Aug 2013 19:32:45 +0400 Subject: [PATCH 31/33] Added more general mutable fields support instead of json-specific --- CHANGES.rst | 2 +- model_utils/tests/fields.py | 32 +++-- model_utils/tests/models.py | 13 +- model_utils/tests/tests.py | 267 +++++++++--------------------------- model_utils/tracker.py | 19 +-- 5 files changed, 90 insertions(+), 243 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index c961a88..5dbc02a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,7 +12,7 @@ master (unreleased) ``update_fields`` in which there are untracked fields. Thanks Mikhail Silonov. (Merge of GH-70, fixes GH-71). -* Added JSON Fields support. +* Added support for mutable fields (Merge of GH-73, fixes GH-74) 1.4.0 (2013.06.03) diff --git a/model_utils/tests/fields.py b/model_utils/tests/fields.py index b0cdcad..0c95815 100644 --- a/model_utils/tests/fields.py +++ b/model_utils/tests/fields.py @@ -1,30 +1,34 @@ -import json - from django.db import models -from django.core.serializers.json import DjangoJSONEncoder + +try: + unicode() + str_class = basestring +except NameError: + str_class = str + +def with_metaclass(meta, base=object): + return meta("NewBase", (base,), {}) -class SimpleJSONField(models.TextField): - - __metaclass__ = models.SubfieldBase +class MutableField(with_metaclass(models.SubfieldBase, models.TextField)): def to_python(self, value): - if value == "": + if value == '': return None try: - if isinstance(value, basestring): - return json.loads(value) + if isinstance(value, str_class): + return [int(i) for i in value.split(',')] except ValueError: pass return value def get_db_prep_save(self, value, connection): - if value == "": - return None + if value is None: + return '' - if isinstance(value, dict): - value = json.dumps(value, cls=DjangoJSONEncoder) + if isinstance(value, list): + value = ','.join((str(i) for i in value)) - return super(SimpleJSONField, self).get_db_prep_save(value, connection) + return super(MutableField, self).get_db_prep_save(value, connection) diff --git a/model_utils/tests/models.py b/model_utils/tests/models.py index af8ff95..a41cce7 100644 --- a/model_utils/tests/models.py +++ b/model_utils/tests/models.py @@ -5,7 +5,7 @@ from model_utils.models import TimeStampedModel, StatusModel, TimeFramedModel from model_utils.tracker import FieldTracker, ModelTracker from model_utils.managers import QueryManager, InheritanceManager, PassThroughManager from model_utils.fields import SplitField, MonitorField, StatusField -from model_utils.tests.fields import SimpleJSONField +from model_utils.tests.fields import MutableField from model_utils import Choices @@ -235,6 +235,7 @@ class Spot(models.Model): class Tracked(models.Model): name = models.CharField(max_length=20) number = models.IntegerField() + mutable = MutableField() tracker = FieldTracker() @@ -272,18 +273,10 @@ class TrackedMultiple(models.Model): number_tracker = FieldTracker(fields=['number']) -class TrackedWithJsonField(models.Model): - name = models.CharField(max_length=20) - number = models.IntegerField() - - props = SimpleJSONField() - - tracker = FieldTracker() - - class ModelTracked(models.Model): name = models.CharField(max_length=20) number = models.IntegerField() + mutable = MutableField() tracker = ModelTracker() diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index daeaa5d..2c1c538 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -26,9 +26,9 @@ from model_utils.tests.models import ( StatusPlainTuple, TimeFrame, Monitored, StatusManagerAdded, TimeFrameManagerAdded, Dude, SplitFieldAbstractParent, Car, Spot, ModelTracked, ModelTrackedFK, ModelTrackedNotDefault, ModelTrackedMultiple, - Tracked, TrackedFK, TrackedNotDefault, TrackedWithJsonField, - TrackedNonFieldAttr, TrackedMultiple, StatusFieldDefaultFilled, - StatusFieldDefaultNotFilled) + Tracked, TrackedFK, TrackedNotDefault,TrackedNonFieldAttr, TrackedMultiple, + StatusFieldDefaultFilled,StatusFieldDefaultNotFilled + ) class GetExcerptTests(TestCase): @@ -762,52 +762,61 @@ class FieldTrackerTests(FieldTrackerTestCase, FieldTrackerCommonTests): self.assertChanged(name=None, number=None) self.instance.name = '' self.assertChanged(name=None, number=None) + self.instance.mutable = [1,2,3] + self.assertChanged(name=None, number=None, mutable=None) def test_pre_save_has_changed(self): - self.assertHasChanged(name=True, number=False) + self.assertHasChanged(name=True, number=False, mutable=False) self.instance.name = 'new age' - self.assertHasChanged(name=True, number=False) + self.assertHasChanged(name=True, number=False, mutable=False) self.instance.number = 7 self.assertHasChanged(name=True, number=True) + self.instance.mutable = [1,2,3] + self.assertHasChanged(name=True, number=True, mutable=True) def test_first_save(self): - self.assertHasChanged(name=True, number=False) - self.assertPrevious(name=None, number=None) - self.assertCurrent(name='', number=None, id=None) + self.assertHasChanged(name=True, number=False, mutable=False) + self.assertPrevious(name=None, number=None, mutable=None) + self.assertCurrent(name='', number=None, id=None, mutable=None) self.assertChanged(name=None) self.instance.name = 'retro' self.instance.number = 4 - self.assertHasChanged(name=True, number=True) - self.assertPrevious(name=None, number=None) - self.assertCurrent(name='retro', number=4, id=None) - self.assertChanged(name=None, number=None) + self.instance.mutable = [1,2,3] + self.assertHasChanged(name=True, number=True, mutable=True) + self.assertPrevious(name=None, number=None, mutable=None) + self.assertCurrent(name='retro', number=4, id=None, mutable=[1,2,3]) + self.assertChanged(name=None, number=None, mutable=None) # Django 1.4 doesn't have update_fields if django.VERSION >= (1, 5, 0): self.instance.save(update_fields=[]) - self.assertHasChanged(name=True, number=True) - self.assertPrevious(name=None, number=None) - self.assertCurrent(name='retro', number=4, id=None) - self.assertChanged(name=None, number=None) + self.assertHasChanged(name=True, number=True, mutable=True) + self.assertPrevious(name=None, number=None, mutable=None) + self.assertCurrent(name='retro', number=4, id=None, mutable=[1,2,3]) + self.assertChanged(name=None, number=None, mutable=None) with self.assertRaises(ValueError): self.instance.save(update_fields=['number']) def test_post_save_has_changed(self): - self.update_instance(name='retro', number=4) - self.assertHasChanged(name=False, number=False) + self.update_instance(name='retro', number=4, mutable=[1,2,3]) + self.assertHasChanged(name=False, number=False, mutable=False) self.instance.name = 'new age' self.assertHasChanged(name=True, number=False) self.instance.number = 8 self.assertHasChanged(name=True, number=True) + self.instance.mutable[1] = 4 + self.assertHasChanged(name=True, number=True, mutable=True) self.instance.name = 'retro' - self.assertHasChanged(name=False, number=True) + self.assertHasChanged(name=False, number=True, mutable=True) def test_post_save_previous(self): - self.update_instance(name='retro', number=4) + self.update_instance(name='retro', number=4, mutable=[1,2,3]) self.instance.name = 'new age' - self.assertPrevious(name='retro', number=4) + self.assertPrevious(name='retro', number=4, mutable=[1,2,3]) + self.instance.mutable[1] = 4 + self.assertPrevious(name='retro', number=4, mutable=[1,2,3]) def test_post_save_changed(self): - self.update_instance(name='retro', number=4) + self.update_instance(name='retro', number=4, mutable=[1,2,3]) self.assertChanged() self.instance.name = 'new age' self.assertChanged(name='retro') @@ -815,36 +824,48 @@ class FieldTrackerTests(FieldTrackerTestCase, FieldTrackerCommonTests): self.assertChanged(name='retro', number=4) self.instance.name = 'retro' self.assertChanged(number=4) + self.instance.mutable[1] = 4 + self.assertChanged(number=4, mutable=[1,2,3]) + self.instance.mutable = [1,2,3] + self.assertChanged(number=4) def test_current(self): - self.assertCurrent(id=None, name='', number=None) + self.assertCurrent(id=None, name='', number=None, mutable=None) self.instance.name = 'new age' - self.assertCurrent(id=None, name='new age', number=None) + self.assertCurrent(id=None, name='new age', number=None, mutable=None) self.instance.number = 8 - self.assertCurrent(id=None, name='new age', number=8) + self.assertCurrent(id=None, name='new age', number=8, mutable=None) + self.instance.mutable = [1,2,3] + self.assertCurrent(id=None, name='new age', number=8, mutable=[1,2,3]) + self.instance.mutable[1] = 4 + self.assertCurrent(id=None, name='new age', number=8, mutable=[1,4,3]) self.instance.save() - self.assertCurrent(id=self.instance.id, name='new age', number=8) + self.assertCurrent(id=self.instance.id, name='new age', number=8, mutable=[1,4,3]) @skipUnless( django.VERSION >= (1, 5, 0), "Django 1.4 doesn't have update_fields") def test_update_fields(self): - self.update_instance(name='retro', number=4) + self.update_instance(name='retro', number=4, mutable=[1,2,3]) self.assertChanged() self.instance.name = 'new age' self.instance.number = 8 - self.assertChanged(name='retro', number=4) + self.instance.mutable = [4,5,6] + self.assertChanged(name='retro', number=4, mutable=[1,2,3]) self.instance.save(update_fields=[]) - self.assertChanged(name='retro', number=4) + self.assertChanged(name='retro', number=4, mutable=[1,2,3]) self.instance.save(update_fields=['name']) in_db = self.tracked_class.objects.get(id=self.instance.id) self.assertEqual(in_db.name, self.instance.name) self.assertNotEqual(in_db.number, self.instance.number) - self.assertChanged(number=4) + self.assertChanged(number=4, mutable=[1,2,3]) self.instance.save(update_fields=['number']) + self.assertChanged(mutable=[1,2,3]) + self.instance.save(update_fields=['mutable']) self.assertChanged() in_db = self.tracked_class.objects.get(id=self.instance.id) self.assertEqual(in_db.name, self.instance.name) self.assertEqual(in_db.number, self.instance.number) + self.assertEqual(in_db.mutable, self.instance.mutable) class FieldTrackedModelCustomTests(FieldTrackerTestCase, @@ -929,171 +950,6 @@ class FieldTrackedModelCustomTests(FieldTrackerTestCase, self.assertChanged() -class JSONFieldTrackedModelTests(FieldTrackerTestCase): - - tracked_class = TrackedWithJsonField - - def setUp(self): - self.instance = self.tracked_class() - self.tracker = self.instance.tracker - - def test_pre_save_changed(self): - self.assertChanged(name=None) - self.instance.name = 'new age' - self.assertChanged(name=None) - self.instance.number = 8 - self.assertChanged(name=None, number=None) - self.instance.name = '' - self.assertChanged(name=None, number=None) - self.instance.props = {'attr': 1} - self.assertChanged(name=None, number=None, props=None) - - def test_first_save(self): - self.assertHasChanged(name=True) - self.assertPrevious(name=None, number=None, props=None) - self.assertCurrent(name='', number=None, props=None, id=None) - self.assertChanged(name=None) - self.instance.name = 'retro' - self.instance.number = 4 - self.instance.props = {'vodka': True} - self.assertHasChanged(name=True, number=True, props=True) - self.assertPrevious(name=None, number=None, props=None) - self.assertCurrent(name='retro', number=4, - props={'vodka': True}, id=None) - self.assertChanged(name=None, number=None, props=None) - - def test_pre_save_has_changed(self): - self.assertHasChanged(name=True) - self.instance.name = 'new age' - self.assertHasChanged(name=True) - self.instance.number = 7 - self.assertHasChanged(name=True, number=True) - self.instance.props = {'bears_on_red_square': False} - self.assertChanged(name=None, number=None, props=None) - - def test_post_save_has_changed(self): - self.update_instance( - name='retro', number=4, - props={ - 'goodies': { - 'balalaika': True, - 'Topol-M': True - } - } - ) - self.assertHasChanged(name=False, number=False, props=False) - self.instance.name = 'new age' - self.assertHasChanged(name=True, number=False, props=False) - self.instance.number = 8 - self.assertHasChanged(name=True, number=True) - self.instance.name = 'retro' - self.assertHasChanged(name=False, number=True) - self.instance.props = { - 'goodies': { - 'balalaika': False, - 'Topol-M': True - } - } - self.assertHasChanged(name=False, number=True, props=True) - - def test_post_save_previous(self): - self.update_instance( - name='retro', number=4, - props={ - 'goodies': { - 'balalaika': True, - 'Topol-M': True - } - } - ) - self.instance.name = 'new age' - self.instance.props = { - 'goodies': { - 'balalaika': False, - 'Topol-M': True - } - } - self.assertPrevious( - name='retro', - number=4, - props={ - 'goodies': { - 'balalaika': True, - 'Topol-M': True - } - } - ) - - def test_post_save_changed(self): - self.update_instance( - name='retro', number=4, - props={ - 'goodies': { - 'balalaika': True, - 'Topol-M': True - } - } - ) - self.assertChanged() - self.instance.name = 'new age' - self.assertChanged(name='retro') - self.instance.number = 8 - self.assertChanged(name='retro', number=4) - self.instance.name = 'retro' - self.assertChanged(number=4) - self.instance.props = { - 'goodies': { - 'balalaika': False, - 'Topol-M': True - } - } - self.assertChanged( - number=4, - props={ - 'goodies': { - 'balalaika': True, - 'Topol-M': True - } - } - ) - - def test_current(self): - self.assertCurrent(name='', number=None, props=None, id=None) - self.instance.name = 'new age' - self.assertCurrent(name='new age', number=None, props=None, id=None) - self.instance.number = 8 - self.assertCurrent(name='new age', number=8, props=None, id=None) - self.instance.props = { - 'goodies': { - 'balalaika': False, - 'Topol-M': True - } - } - self.assertCurrent( - name='new age', - number=8, - props={ - 'goodies': { - 'balalaika': False, - 'Topol-M': True - } - }, - id=None - ) - self.instance.save() - self.assertCurrent( - name='new age', - number=8, - props={ - 'goodies': { - 'balalaika': False, - 'Topol-M': True - } - }, - id=self.instance.id - ) - - class FieldTrackedModelAttributeTests(FieldTrackerTestCase): tracked_class = TrackedNonFieldAttr @@ -1291,24 +1147,27 @@ class ModelTrackerTests(FieldTrackerTests): self.assertChanged() self.instance.name = '' self.assertChanged() + self.instance.mutable = [1,2,3] + self.assertChanged() def test_first_save(self): - self.assertHasChanged(name=True, number=True) - self.assertPrevious(name=None, number=None) - self.assertCurrent(name='', number=None, id=None) + self.assertHasChanged(name=True, number=True, mutable=True) + self.assertPrevious(name=None, number=None, mutable=None) + self.assertCurrent(name='', number=None, id=None, mutable=None) self.assertChanged() self.instance.name = 'retro' self.instance.number = 4 - self.assertHasChanged(name=True, number=True) - self.assertPrevious(name=None, number=None) - self.assertCurrent(name='retro', number=4, id=None) + self.instance.mutable = [1,2,3] + self.assertHasChanged(name=True, number=True, mutable=True) + self.assertPrevious(name=None, number=None, mutable=None) + self.assertCurrent(name='retro', number=4, id=None, mutable=[1,2,3]) self.assertChanged() # Django 1.4 doesn't have update_fields if django.VERSION >= (1, 5, 0): self.instance.save(update_fields=[]) - self.assertHasChanged(name=True, number=True) - self.assertPrevious(name=None, number=None) - self.assertCurrent(name='retro', number=4, id=None) + self.assertHasChanged(name=True, number=True, mutable=True) + self.assertPrevious(name=None, number=None, mutable=None) + self.assertCurrent(name='retro', number=4, id=None, mutable=[1,2,3]) self.assertChanged() with self.assertRaises(ValueError): self.instance.save(update_fields=['number']) diff --git a/model_utils/tracker.py b/model_utils/tracker.py index a08a958..fa43c57 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals from copy import deepcopy -from json import JSONEncoder from django.db import models from django.core.exceptions import FieldError @@ -23,7 +22,10 @@ class FieldInstanceTracker(object): self.saved_data = self.current() else: self.saved_data.update(**self.current(fields=fields)) - return self.saved_data + + # preventing mutable fields side effects + for field, field_value in self.saved_data.items(): + self.saved_data[field] = deepcopy(field_value) def current(self, fields=None): """Return dict of current values for all tracked fields""" @@ -82,8 +84,7 @@ class FieldTracker(object): def initialize_tracker(self, sender, instance, **kwargs): tracker = self.tracker_class(instance, self.fields, self.field_map) setattr(instance, self.attname, tracker) - saved_data = tracker.set_saved_fields() - self.prevent_json_fields_side_effects(saved_data) + tracker.set_saved_fields() self.patch_save(instance) def patch_save(self, instance): @@ -106,16 +107,6 @@ class FieldTracker(object): return ret instance.save = save - def prevent_json_fields_side_effects(self, saved_data): - for field, field_value in saved_data.items(): - if isinstance(field_value, (dict, list, tuple)): - try: - JSONEncoder().encode(field_value) - except TypeError: - pass - else: - saved_data[field] = deepcopy(field_value) - def __get__(self, instance, owner): if instance is None: return self From ef510d53dd2a5b5369a2314155457c34b8b47b00 Mon Sep 17 00:00:00 2001 From: Mikhail Silonov Date: Mon, 12 Aug 2013 13:22:34 +0400 Subject: [PATCH 32/33] Removed reinvented wheel --- CHANGES.rst | 1 - model_utils/tests/fields.py | 12 ++---------- model_utils/tracker.py | 6 +++--- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 5dbc02a..0289eb0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,7 +7,6 @@ master (unreleased) * `Choices` now `__contains__` its Python identifier values. Thanks Keryn Knight. (Merge of GH-69). -======= * Fixed a bug causing ``KeyError`` when saving with the parameter ``update_fields`` in which there are untracked fields. Thanks Mikhail Silonov. (Merge of GH-70, fixes GH-71). diff --git a/model_utils/tests/fields.py b/model_utils/tests/fields.py index 0c95815..3f1503a 100644 --- a/model_utils/tests/fields.py +++ b/model_utils/tests/fields.py @@ -1,13 +1,5 @@ from django.db import models - -try: - unicode() - str_class = basestring -except NameError: - str_class = str - -def with_metaclass(meta, base=object): - return meta("NewBase", (base,), {}) +from django.utils.six import with_metaclass, string_types class MutableField(with_metaclass(models.SubfieldBase, models.TextField)): @@ -17,7 +9,7 @@ class MutableField(with_metaclass(models.SubfieldBase, models.TextField)): return None try: - if isinstance(value, str_class): + if isinstance(value, string_types): return [int(i) for i in value.split(',')] except ValueError: pass diff --git a/model_utils/tracker.py b/model_utils/tracker.py index fa43c57..ea95acf 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -28,7 +28,7 @@ class FieldInstanceTracker(object): self.saved_data[field] = deepcopy(field_value) def current(self, fields=None): - """Return dict of current values for all tracked fields""" + """Returns dict of current values for all tracked fields""" if fields is None: fields = self.fields return dict((f, self.get_field_value(f)) for f in fields) @@ -41,7 +41,7 @@ class FieldInstanceTracker(object): raise FieldError('field "%s" not tracked' % field) def previous(self, field): - """Return currently saved value of given field""" + """Returns currently saved value of given field""" return self.saved_data.get(field) def changed(self): @@ -61,7 +61,7 @@ class FieldTracker(object): self.fields = fields def get_field_map(self, cls): - """Return dict mapping fields names to model attribute names""" + """Returns dict mapping fields names to model attribute names""" field_map = dict((field, field) for field in self.fields) all_fields = dict((f.name, f.attname) for f in cls._meta.local_fields) field_map.update(**dict((k, v) for (k, v) in all_fields.items() From 09278083e857de6a8c4e2e776bd10a6dcfd0ccc7 Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Sat, 17 Aug 2013 11:31:03 -0700 Subject: [PATCH 33/33] Fix typo noted by @silonov --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 397457e..2fac81a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -13,7 +13,7 @@ master (unreleased) * Fixed ``FieldTracker`` usage on inherited models. Fixes GH-57. -* Add mutable field support to ``FieldTracker`` (Merge of GH-73, fixes GH-74) +* Added mutable field support to ``FieldTracker`` (Merge of GH-73, fixes GH-74) 1.4.0 (2013.06.03)