Skip to content

Commit

Permalink
Merge branch 'master' into 807-custom-onetoonefields
Browse files Browse the repository at this point in the history
  • Loading branch information
jeking3 authored Oct 6, 2021
2 parents 7759125 + 353a4ab commit 2a5274c
Show file tree
Hide file tree
Showing 24 changed files with 495 additions and 114 deletions.
4 changes: 4 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ Authors
- Hernan Esteves (`sevetseh28 <https://github.com/sevetseh28>`_)
- Hielke Walinga (`hwalinga <https://github.com/hwalinga>`_)
- Jack Cushman (`jcushman <https://github.com/jcushman>`_)
- Jake Howard (`RealOrangeOne <https://github.com/realorangeone>`_)
- James Muranga (`jamesmura <https://github.com/jamesmura>`_)
- James Pulec
- Jesse Shapiro
- Jihoon Baek (`jihoon796 <https://github.com/jihoon796>`_)
- Jim Gomez
- Jim King (`jeking3 <https://github.com/jeking3>`_)
- Joao Junior (`joaojunior <https://github.com/joaojunior>`_)
- Joao Pedro Francese
- `jofusa <https://github.com/jofusa>`_
Expand Down Expand Up @@ -115,6 +117,8 @@ Authors
- `Alex Todorov <https://github.com/atodorov>`_
- David Smith (`smithdc1 <https://github.com/smithdc1>`_)
- Shi Han Ng (`shihanng <https://github.com/shihanng>`_)
- `ddusi <https://github.com/ddusi>`_
- `DanialErfanian <https://github.com/DanialErfanian>`_

Background
==========
Expand Down
20 changes: 17 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@ Changes
Unreleased
----------

- Fixed bug where serializer of djangorestframework crashed if used with ``OrderingFilter`` (gh-821)
- Added ``excluded_field_kwargs`` to support custom ``OneToOneField`` that have
additional arguments that don't exist on ``ForeignKey``. (gh-870)
Upgrade Implications:

- Run `makemigrations` after upgrading to realize the benefit of indexing changes.

Full list of changes:

- Added index on `history_date` column; opt-out with setting `SIMPLE_HISTORY_DATE_INDEX` (gh-565)
- Added ``excluded_field_kwargs`` to support custom ``OneToOneField`` that have
additional arguments that don't exist on ``ForeignKey``. (gh-870)
- Fixed ``prev_record`` and ``next_record`` performance when using ``excluded_fields`` (gh-791)
- Fixed `update_change_reason` in pk (gh-806)
- Fixed bug where serializer of djangorestframework crashed if used with ``OrderingFilter`` (gh-821)
- Fixed `make format` so it works by using tox (gh-859)
- Fixed bug where latest() is not idempotent for identical ``history_date`` records (gh-861)
- Support ``included_fields`` for ``history.diff_against`` (gh-776)
- Improve performance of ``history.diff_against`` by reducing number of queries to 0 in most cases (gh-776)
- Added Czech translations (gh-885)

3.0.0 (2021-04-16)
------------------
Expand Down
46 changes: 0 additions & 46 deletions CODE_OF_CONDUCT.md

This file was deleted.

4 changes: 1 addition & 3 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ running tests::
This will install:

- `tox`_: used for running the tests against all supported versions of Django
and Python
and Python as well as running tasks like lint, format, docs
- `coverage`_: used for analyzing test coverage for tests
- `Sphinx`_: used for generating documentation

If not using a virtualenv, the command should be prepended with ``sudo``.

.. _tox: http://testrun.org/tox/latest//
.. _coverage: http://nedbatchelder.com/code/coverage/
.. _sphinx: http://sphinx-doc.org/

Documentation
-------------
Expand Down
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ clean-pyc:
find . -name '*~' -exec rm -f {} +

init:
pip install "tox>=1.8" coverage Sphinx
pip install "tox>=1.8" coverage

test:
coverage erase
Expand All @@ -24,7 +24,7 @@ test:
docs: documentation

documentation:
sphinx-build -b html -d docs/_build/doctrees docs docs/_build/html
tox -e docs

dist: clean
pip install -U wheel
Expand All @@ -44,5 +44,4 @@ release: dist
twine upload dist/*

format:
isort docs simple_history runtests.py setup.py
black docs simple_history runtests.py setup.py
tox -e format
20 changes: 20 additions & 0 deletions docs/historical_model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ model, will work too.
my_poll.save()
Indexed ``history_date``
------------------------

Many queries use ``history_date`` as a filter. The as_of queries combine this with the
original model's promary key to extract point-in-time snapshots of history. By default
the ``history_date`` field is indexed. You can control this behavior using settings.py.

.. code-block:: python
# disable indexing on history_date
SIMPLE_HISTORY_DATE_INDEX = False
# enable indexing on history_date (default setting)
SIMPLE_HISTORY_DATE_INDEX = True
# enable composite indexing on history_date and model pk (to improve as_of queries)
# the string is case-insensitive
SIMPLE_HISTORY_DATE_INDEX = "Composite"
Custom history table name
-------------------------

Expand Down
2 changes: 2 additions & 0 deletions docs/history_diffing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ This may be useful when you want to construct timelines and need to get only the
delta = new_record.diff_against(old_record)
for change in delta.changes:
print("{} changed from {} to {}".format(change.field, change.old, change.new))
``diff_against`` also accepts 2 arguments ``excluded_fields`` and ``included_fields`` to either explicitly include or exclude fields from being diffed.
6 changes: 6 additions & 0 deletions docs/querying_history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ If you want to save a model without a historical record, you can use the followi
poll = Poll(question='something')
poll.save_without_historical_record()
Or disable history records for all models by putting following lines in your ``settings.py`` file:

.. code-block:: python
SIMPLE_HISTORY_ENABLED = False
Filtering data using a relationship to the model
------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion requirements/coverage.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
coverage==5.5
coverage==6.0
toml==0.10.2
2 changes: 1 addition & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Sphinx==4.1.0
Sphinx==4.2.0
4 changes: 2 additions & 2 deletions requirements/lint.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
black==21.6b0
black==21.9b0
flake8==3.9.2
isort==5.9.2
isort==5.9.3
4 changes: 2 additions & 2 deletions requirements/tox.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-r ./coverage.txt
tox==3.24.1
tox-gh-actions==2.6.0
tox==3.24.4
tox-gh-actions==2.8.1
Binary file added simple_history/locale/cs_CZ/LC_MESSAGES/django.mo
Binary file not shown.
134 changes: 134 additions & 0 deletions simple_history/locale/cs_CZ/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-09-18 21:54+0200\n"
"PO-Revision-Date: 2021-09-20 19:50+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: cs\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=4; plural=(n == 1 && n % 1 == 0) ? 0 : (n >= 2 && n "
"<= 4 && n % 1 == 0) ? 1: (n % 1 != 0 ) ? 2 : 3;\n"

#: simple_history/admin.py:102
#, python-format
msgid "View history: %s"
msgstr "Zobrazit historii: %s"

#: simple_history/admin.py:104
#, python-format
msgid "Change history: %s"
msgstr "Historie změn: %s"

#: simple_history/admin.py:110
#, python-format
msgid "The %(name)s \"%(obj)s\" was changed successfully."
msgstr "%(name)s \"%(obj)s\" bylo úspěšně změněno."

#: simple_history/admin.py:116
msgid "You may edit it again below"
msgstr "Níže jej můžete znovu upravit"

#: simple_history/admin.py:216
#, python-format
msgid "View %s"
msgstr "Zobrazit %s"

#: simple_history/admin.py:218
#, python-format
msgid "Revert %s"
msgstr "Vrátit změny: %s"

#: simple_history/models.py:433
msgid "Created"
msgstr "Vytvořeno"

#: simple_history/models.py:433
msgid "Changed"
msgstr "Změněno"

#: simple_history/models.py:433
msgid "Deleted"
msgstr "Smazáno"

#: simple_history/templates/simple_history/_object_history_list.html:9
msgid "Object"
msgstr "Objekt"

#: simple_history/templates/simple_history/_object_history_list.html:13
msgid "Date/time"
msgstr "Datum/čas"

#: simple_history/templates/simple_history/_object_history_list.html:14
msgid "Comment"
msgstr "Komentář"

#: simple_history/templates/simple_history/_object_history_list.html:15
msgid "Changed by"
msgstr "Změnil"

#: simple_history/templates/simple_history/_object_history_list.html:16
msgid "Change reason"
msgstr "Důvod změny"

#: simple_history/templates/simple_history/_object_history_list.html:37
msgid "None"
msgstr "Žádné"

#: simple_history/templates/simple_history/object_history.html:11
msgid ""
"Choose a date from the list below to revert to a previous version of this "
"object."
msgstr "Vyberte datum ze seznamu níže a vraťte se k předchozí verzi tohoto "
"objektu."

#: simple_history/templates/simple_history/object_history.html:16
msgid "This object doesn't have a change history."
msgstr "Tento objekt nemá historii změn."

#: simple_history/templates/simple_history/object_history_form.html:7
msgid "Home"
msgstr "Domů"

#: simple_history/templates/simple_history/object_history_form.html:11
msgid "History"
msgstr "Historie"

#: simple_history/templates/simple_history/object_history_form.html:12
#, python-format
msgid "View %(verbose_name)s"
msgstr "Zobrazit %(verbose_name)s"

#: simple_history/templates/simple_history/object_history_form.html:12
#, python-format
msgid "Revert %(verbose_name)s"
msgstr "Vrátit %(verbose_name)s"

#: simple_history/templates/simple_history/object_history_form.html:25
msgid ""
"Press the 'Revert' button below to revert to this version of the object. "
msgstr "Stisknutím tlačítka 'Vrátit změny' se vrátíte k této verzi objektu."

#: simple_history/templates/simple_history/object_history_form.html:25
msgid "Press the 'Change History' button below to edit the history."
msgstr "Chcete-li historii upravit, stiskněte tlačítko 'Změnit historii'"

#: simple_history/templates/simple_history/submit_line.html:4
msgid "Revert"
msgstr "Vrátit změny"

#: simple_history/templates/simple_history/submit_line.html:6
msgid "Change History"
msgstr "Historie změn"

#: simple_history/templates/simple_history/submit_line.html:7
msgid "Close"
msgstr "Zavřít"
13 changes: 8 additions & 5 deletions simple_history/manager.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.conf import settings
from django.db import connection, models
from django.db.models import OuterRef, Subquery
from django.utils import timezone

from simple_history.utils import get_change_reason_from_object
from simple_history.utils import (
get_app_model_primary_key_name,
get_change_reason_from_object,
)


class HistoryDescriptor:
Expand All @@ -29,10 +33,7 @@ def get_queryset(self):
if self.instance is None:
return qs

if isinstance(self.instance._meta.pk, models.ForeignKey):
key_name = self.instance._meta.pk.name + "_id"
else:
key_name = self.instance._meta.pk.name
key_name = get_app_model_primary_key_name(self.instance)
return self.get_super_queryset().filter(**{key_name: self.instance.pk})

def most_recent(self):
Expand Down Expand Up @@ -137,6 +138,8 @@ def bulk_history_create(
If called by bulk_update_with_history, use the update boolean and
save the history_type accordingly.
"""
if not getattr(settings, "SIMPLE_HISTORY_ENABLED", True):
return

history_type = "+"
if update:
Expand Down
Loading

0 comments on commit 2a5274c

Please sign in to comment.