From 6bbc056177d74e6c5abe6cbb06610da566581638 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 8 Mar 2014 12:11:29 -0500 Subject: [PATCH 1/8] added id_dict method to the manager methods. Tested it and updated documentation --- README.md | 15 +++++++++ manager_utils/manager_utils.py | 23 ++++++++++++++ test_project/tests/manager_utils_tests.py | 37 +++++++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/README.md b/README.md index c38ffb8..6bd9157 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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() +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: , 2: } + + print TestModel.objects.filter(int_field=2).id_dict() + {2: } + ## post_bulk_operation(providing_args=['model']) 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. diff --git a/manager_utils/manager_utils.py b/manager_utils/manager_utils.py index b0cc543..f79f950 100644 --- a/manager_utils/manager_utils.py +++ b/manager_utils/manager_utils.py @@ -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. @@ -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 diff --git a/test_project/tests/manager_utils_tests.py b/test_project/tests/manager_utils_tests.py index 08a4461..190aecb 100644 --- a/test_project/tests/manager_utils_tests.py +++ b/test_project/tests/manager_utils_tests.py @@ -77,6 +77,43 @@ 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}) + print (TestModel.objects.id_dict()) + self.assertEquals(0, 1) + + 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 From 511bed06641a7c42a9ec281185313f68308128b1 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 8 Mar 2014 12:11:58 -0500 Subject: [PATCH 2/8] bumped version --- manager_utils/VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manager_utils/VERSION b/manager_utils/VERSION index 49d5957..be58634 100644 --- a/manager_utils/VERSION +++ b/manager_utils/VERSION @@ -1 +1 @@ -0.1 +0.3 From 1e7dd8ecce99eadc829fded6b5d004ff393f06bf Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 8 Mar 2014 12:19:57 -0500 Subject: [PATCH 3/8] left print in tests --- test_project/tests/manager_utils_tests.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/test_project/tests/manager_utils_tests.py b/test_project/tests/manager_utils_tests.py index 190aecb..8d5c144 100644 --- a/test_project/tests/manager_utils_tests.py +++ b/test_project/tests/manager_utils_tests.py @@ -94,8 +94,6 @@ def test_objects_manager(self): 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}) - print (TestModel.objects.id_dict()) - self.assertEquals(0, 1) def test_no_objects_queryset(self): """ From f87c4077de3829d9379c70f61b2a12cdd0456944 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 8 Mar 2014 14:57:26 -0500 Subject: [PATCH 4/8] updated setup.py to properly install dependencies --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 020a435..8110121 100644 --- a/setup.py +++ b/setup.py @@ -20,10 +20,11 @@ 'Framework :: Django', ], dependency_links=[ - 'git+https://github.com/wesokes/django-query-builder.git@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, ) From 46691185fff75f52ba31ad106a47d01d1c9b42fe Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 8 Mar 2014 14:59:02 -0500 Subject: [PATCH 5/8] travis file verifies that package can be installed --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 83874d8..0d50071 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,5 +11,6 @@ before_script: - find . | grep .py$ | grep -v /migrations | grep -v __init__.py | xargs pyflakes - psql -c 'CREATE DATABASE manager_utils;' -U postgres script: + - python setup.py install - coverage run --source='manager_utils' --branch manage.py test - coverage report --fail-under=100 From 57abeb15794af3f1395d3269f6c7489cd444925c Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 8 Mar 2014 15:00:59 -0500 Subject: [PATCH 6/8] verifying that travis fails when trying to setup package --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 8110121..376143e 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ 'Framework :: Django', ], dependency_links=[ - 'http://github.com/wesokes/django-query-builder/tarball/0.5#egg=django-query-builder-0.5.2', + 'http://github.com/wesokes/django-query-builder/tarball/master#egg=django-query-builder-0.5.2', ], install_requires=[ 'django>=1.6', From 434cb5f9b47c868159c7ce4108786208b4cab5a0 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 8 Mar 2014 15:02:49 -0500 Subject: [PATCH 7/8] verifying that travis fails when trying to setup package --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0d50071..a7c22fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: @@ -11,6 +12,5 @@ before_script: - find . | grep .py$ | grep -v /migrations | grep -v __init__.py | xargs pyflakes - psql -c 'CREATE DATABASE manager_utils;' -U postgres script: - - python setup.py install - coverage run --source='manager_utils' --branch manage.py test - coverage report --fail-under=100 From 95452a711d09ca5fb576b1356f82ae75c89d2549 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 8 Mar 2014 15:05:03 -0500 Subject: [PATCH 8/8] fixed installation --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 376143e..8110121 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ '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',