Skip to content

Commit

Permalink
Merge pull request #6 from wesleykendall/develop
Browse files Browse the repository at this point in the history
Added id_dict method to manager utils
  • Loading branch information
wesleykendall committed Mar 8, 2014
2 parents b161b4c + 0528335 commit 33c2658
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
env:
- DJANGO=1.6.1 DB=postgres
install:
- python setup.py install
- pip install -q Django==$DJANGO
- pip install -r requirements.txt
before_script:
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ An overview of each util is below with links to more in-depth documentation and
- [get_or_none](#get_or_none): Performs a get on a queryset and returns None if the object does not exist.
- [upsert](#upsert): Performs an upsert (update or insert) to a model.
- [bulk_update](#bulk_update): Bulk updates a list of models and the fields that have been updated.
- [id_dict](#id_dict): Returns a dictionary of objects keyed on their ID.
- [post_bulk_operation](#post_bulk_operation): A signal that is fired when a bulk operation happens.

## single()<a name="single"></a>
Expand Down Expand Up @@ -130,6 +131,20 @@ Performs an bulk update on an list of objects. Any fields listed in the fields_t
print model_obj2.int_field, model_obj2.float_field
10, 20.0

## id_dict()<a name="id_dict"></a>
Returns a dictionary of the model objects keyed on their ID.

**Examples**

TestModel.objects.create(int_field=1)
TestModel.objects.create(int_field=2)

print TestModel.objects.id_dict()
{1: <TestModel: TestModel object>, 2: <TestModel: TestModel object>}

print TestModel.objects.filter(int_field=2).id_dict()
{2: <TestModel: TestModel object>}

## post_bulk_operation(providing_args=['model'])<a name="post_bulk_operation"></a>
A signal that is emitted at the end of a bulk operation. The current bulk operations are Django's update and bulk_create methods and this package's bulk_update method. The signal provides the model that was updated.

Expand Down
2 changes: 1 addition & 1 deletion manager_utils/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2
0.3
23 changes: 23 additions & 0 deletions manager_utils/manager_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class ManagerUtilsQuerySet(QuerySet):
"""
Defines the methods in the manager utils that can also be applied to querysets.
"""
def id_dict(self):
"""
Returns a dictionary of all the objects keyed on their id.
"""
return {obj.id: obj for obj in self}

def get_or_none(self, **query_params):
"""
Get an object or return None if it doesn't exist.
Expand Down Expand Up @@ -51,6 +57,23 @@ class ManagerUtilsMixin(object):
def get_queryset(self):
return ManagerUtilsQuerySet(self.model)

def id_dict(self):
"""
Returns a dictionary of all the objects keyed on their ID.
Returns:
A dictionary of objects from the queryset or manager that is keyed on the objects' IDs.
Examples:
TestModel.objects.create(int_field=1)
TestModel.objects.create(int_field=2)
print TestModel.objects.id_dict()
"""
return self.get_queryset().id_dict()

def bulk_create(self, objs, batch_size=None):
"""
Overrides Django's bulk_create function to emit a post_bulk_operation signal when bulk_create
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
'Framework :: Django',
],
dependency_links=[
'http://github.com/wesokes/django-query-builder/tarball/master#egg=django-query-builder-0.5.2',
'http://github.com/wesokes/django-query-builder/tarball/0.5#egg=django-query-builder-0.5.2',
],
install_requires=[
'django>=1.6',
'django-query-builder>=0.5.2',
],
include_package_data=True,
)
35 changes: 35 additions & 0 deletions test_project/tests/manager_utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,41 @@ def test_save_doesnt_emit_signal(self):
self.assertEquals(self.signal_handler.num_times_called, 0)


class IdDictTest(TestCase):
"""
Tests the id_dict function.
"""
def test_no_objects_manager(self):
"""
Tests the output when no objects are present in the manager.
"""
self.assertEquals(TestModel.objects.id_dict(), {})

def test_objects_manager(self):
"""
Tests retrieving a dict of objects keyed on their ID from the manager.
"""
model_obj1 = G(TestModel, int_field=1)
model_obj2 = G(TestModel, int_field=2)
self.assertEquals(TestModel.objects.id_dict(), {model_obj1.id: model_obj1, model_obj2.id: model_obj2})

def test_no_objects_queryset(self):
"""
Tests the case when no objects are returned via a queryset.
"""
G(TestModel, int_field=1)
G(TestModel, int_field=2)
self.assertEquals(TestModel.objects.filter(int_field__gte=3).id_dict(), {})

def test_objects_queryset(self):
"""
Tests the case when objects are returned via a queryset.
"""
G(TestModel, int_field=1)
model_obj = G(TestModel, int_field=2)
self.assertEquals(TestModel.objects.filter(int_field__gte=2).id_dict(), {model_obj.id: model_obj})


class GetOrNoneTests(TestCase):
"""
Tests the get_or_none function in the manager utils
Expand Down

0 comments on commit 33c2658

Please sign in to comment.