Repository: ckan/ckan

Issue:
package_create keeps re-creating the same package
This is on CKAN 2.5.2:

``` python
    package_create = toolkit.get_action('package_create')
    context = {'ignore_auth':  True}
    for i in range(10):
        pkg_dict = package_create(context, {
            'name': 'my-package-{}'.format(i),
            'owner_org': 'my-org',
        })
        print('{}: {}'.format(i, pkg_dict['id']))
```

prints

```
0: 1d6d50df-3c55-434f-ba8f-375e95938830
1: 1d6d50df-3c55-434f-ba8f-375e95938830
2: 1d6d50df-3c55-434f-ba8f-375e95938830
3: 1d6d50df-3c55-434f-ba8f-375e95938830
4: 1d6d50df-3c55-434f-ba8f-375e95938830
5: 1d6d50df-3c55-434f-ba8f-375e95938830
6: 1d6d50df-3c55-434f-ba8f-375e95938830
7: 1d6d50df-3c55-434f-ba8f-375e95938830
8: 1d6d50df-3c55-434f-ba8f-375e95938830
9: 1d6d50df-3c55-434f-ba8f-375e95938830
```

I was expecting 10 separate packages instead.

package_create keeps re-creating the same package
This is on CKAN 2.5.2:

``` python
    package_create = toolkit.get_action('package_create')
    context = {'ignore_auth':  True}
    for i in range(10):
        pkg_dict = package_create(context, {
            'name': 'my-package-{}'.format(i),
            'owner_org': 'my-org',
        })
        print('{}: {}'.format(i, pkg_dict['id']))
```

prints

```
0: 1d6d50df-3c55-434f-ba8f-375e95938830
1: 1d6d50df-3c55-434f-ba8f-375e95938830
2: 1d6d50df-3c55-434f-ba8f-375e95938830
3: 1d6d50df-3c55-434f-ba8f-375e95938830
4: 1d6d50df-3c55-434f-ba8f-375e95938830
5: 1d6d50df-3c55-434f-ba8f-375e95938830
6: 1d6d50df-3c55-434f-ba8f-375e95938830
7: 1d6d50df-3c55-434f-ba8f-375e95938830
8: 1d6d50df-3c55-434f-ba8f-375e95938830
9: 1d6d50df-3c55-434f-ba8f-375e95938830
```

I was expecting 10 separate packages instead.



Discussion:
Never reuse the context dict. There are side effects in the action calls that are carried from one call to the next (that's why ckanapi.LocalCKAN creates a new context for you on every call)

Ah, makes a lot of sense, but I never thought about it. My code works fine if I pass a new context dict for each call.

I'll prepare a PR for updating the documentation of [`get_action`](http://docs.ckan.org/en/latest/extensions/plugins-toolkit.html#ckan.plugins.toolkit.get_action) to mention this.

Never reuse the context dict. There are side effects in the action calls that are carried from one call to the next (that's why ckanapi.LocalCKAN creates a new context for you on every call)

Ah, makes a lot of sense, but I never thought about it. My code works fine if I pass a new context dict for each call.

I'll prepare a PR for updating the documentation of [`get_action`](http://docs.ckan.org/en/latest/extensions/plugins-toolkit.html#ckan.plugins.toolkit.get_action) to mention this.


Resolving patch:
diff --git a/ckan/logic/__init__.py b/ckan/logic/__init__.py
--- a/ckan/logic/__init__.py
+++ b/ckan/logic/__init__.py
@@ -343,6 +343,12 @@ def get_action(action):
     If a ``context`` of ``None`` is passed to the action function then the
     default context dict will be created.
 
+    .. note::
+
+        Many action functions modify the context dict. It can therefore
+        not be reused for multiple calls of the same or different action
+        functions.
+
     :param action: name of the action function to return,
         eg. ``'package_create'``
     :type action: string


Test patch: