Skip to main content

Utility functions for forking, resetting and diffing model objects

Project description

Overview
========
Django-Forkit is composed of a set of utility functions for _forking_,
_resetting_, and _diffing_ model objects. Below are a list of the current
utility functions:

forkit.tools.fork
-----------------
Creates and returns a new object that is identical to ``reference``.

- ``fields`` - A list of fields to fork. If a falsy value, the fields
will be inferred depending on the value of ``deep``.
- ``exclude`` - A list of fields to not fork (not applicable if ``fields``
is defined)
- ``deep`` - If ``True``, traversing all related objects and creates forks
of them as well, effectively creating a new _tree_ of objects.
- ``commit`` - If ``True``, all forks (including related objects) will be saved
in the order of dependency. If ``False``, all commits are stashed away until
the root fork is committed.
- ``**kwargs`` - Any additional keyword arguments are passed along to all signal
receivers. Useful for altering runtime behavior in signal receivers.

```python
fork(reference, [fields=None], [exclude=('pk',)], [deep=False], [commit=True], [**kwargs])
```

forkit.tools.reset
------------------
Same parameters as above, except that an explicit ``instance`` is rquired and
will result in an in-place update of ``instance``. For shallow resets, only the
local non-relational fields will be updated. For deep resets, _direct_
foreign keys will be traversed and reset. _Many-to-many and reverse foreign keys
are not attempted to be reset because the comparison between the related objects
for ``reference`` and the related objects for ``instance`` becomes ambiguous._

```python
reset(reference, instance, [fields=None], [exclude=('pk',)], [deep=False], [commit=True], [**kwargs])
```

forkit.tools.commit
-------------------
Commits any unsaved changes to a forked or reset object.

```python
commit(reference, [**kwargs])
```

forkit.tools.diff
-----------------
Performs a _diff_ between two model objects of the same type. The output is a
``dict`` of differing values relative to ``reference``. Thus, if
``reference.foo`` is ``bar`` and ``instance.foo`` is ``baz``, the output will
be ``{'foo': 'baz'}``. _Note: deep diffs only work for simple non-circular
relationships. Improved functionality is scheduled for a future release._

```python
diff(reference, instance, [fields=None], [exclude=('pk',)], [deep=False], [**kwargs])
```

ForkableModel
-------------
Also included is a ``Model`` subclass which has implements the above functions
as methods.

```python
from forkit.models import ForkableModel

class Author(ForkableModel):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
```

Let's create starting object:

```python
author = Author(first_name='Byron', last_name='Ruth')
author.save()
```

To create copy, simply call the ``fork`` method.

```python
author_fork = author.fork()
```

When an object is forked, it immediately inherits it's data including
related objects.

```python
author_fork.first_name # Byron
author_fork.last_name # Ruth
```

Let us change something on the fork and use the ``diff`` method to compare it
against the original ``author``. It returns a dictionary of the differences
between itself and the passed in object.

```python
author_fork.first_name = 'Edward'
author_fork.diff(author) # {'first_name': 'Edward'}
```

Once satisfied with the changes, simply call ``commit``.

```python
author_fork.commit()
```

Signals
=======
For each of the utility function above, ``pre_FOO`` and ``post_FOO`` signals
are sent allowing for a decoupled approached for customizing behavior, especially
when performing deep operations.

forkit.signals.pre_fork
-----------------------

- ``sender`` - the model class of the instance
- ``reference`` - the reference object the fork is being created from
- ``instance`` - the forked object itself
- ``config`` - a ``dict`` of the keyword arguments passed into ``forkit.tools.fork``

forkit.signals.post_fork
-----------------------

- ``sender`` - the model class of the instance
- ``reference`` - the reference object the fork is being created from
- ``instance`` - the forked object itself

forkit.signals.pre_reset
-----------------------

- ``sender`` - the model class of the instance
- ``reference`` - the reference object the instance is being reset relative to
- ``instance`` - the object being reset
- ``config`` - a ``dict`` of the keyword arguments passed into ``forkit.tools.reset``

forkit.signals.post_reset
-----------------------

- ``sender`` - the model class of the instance
- ``reference`` - the reference object the instance is being reset relative to
- ``instance`` - the object being reset

forkit.signals.pre_commit
-----------------------

- ``sender`` - the model class of the instance
- ``reference`` - the reference object the instance has been derived
- ``instance`` - the object to be committed

forkit.signals.post_commit
-----------------------

- ``sender`` - the model class of the instance
- ``reference`` - the reference object the instance has been derived
- ``instance`` - the object that has been committed

forkit.signals.pre_diff
-----------------------

- ``sender`` - the model class of the instance
- ``reference`` - the reference object the instance is being diffed against
- ``instance`` - the object being diffed with
- ``config`` - a ``dict`` of the keyword arguments passed into ``forkit.tools.diff``

forkit.signals.post_diff
-----------------------

- ``sender`` - the model class of the instance
- ``reference`` - the reference object the instance is being diffed against
- ``instance`` - the object being diffed with
- ``diff`` - the diff between the ``reference`` and ``instance``

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-forkit-0.9.5.tar.gz (9.3 kB view details)

Uploaded Source

File details

Details for the file django-forkit-0.9.5.tar.gz.

File metadata

  • Download URL: django-forkit-0.9.5.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-forkit-0.9.5.tar.gz
Algorithm Hash digest
SHA256 28a550a487e091f8287203e266046e028173fe7048c8c28847b9a9709ef148a1
MD5 1da511d78dd4b1a47811192bbfd83aab
BLAKE2b-256 79744da6e5dd97e00e449a25b18d6f3f54eece51af582eaf9920d1595378f1b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page