[ckan-dev] what are context and data_dicts?

George Sattler George.sattler at xvt.com.au
Thu May 23 22:17:38 UTC 2013


Hi Adrià
Thanks very much for your response and the detail. I get it!
Cheers
George

-----Original Message-----
From: ckan-dev-bounces at lists.okfn.org [mailto:ckan-dev-bounces at lists.okfn.org] On Behalf Of Adrià Mercader
Sent: Tuesday, 21 May 2013 6:52 PM
To: CKAN Development Discussions
Subject: Re: [ckan-dev] what are context and data_dicts?

Hi George,

As you will have noticed, all functions from the logic layer accept the context and data_dict parameters.
The context provides information about the environment in which a function is executed (ie the context). Generally the more important pieces of information that you need are the model and the user that is performing the action, so the authorization checks can be performed.
It can also have other keys specific to each function.

data_dict is the actual data that the function will use. For functions that eg interact with datasets like package_create or package_update this is the actual dataset dict that will get added or updated on the database. In others like the "current_package_list_with_resources"
case they hold parameters that affect the returned value. The keys shown on the docs (eg [1]) are the ones that go on package dict.

The good news is that if you use get_action, the context object is automatically populated for you with the model and user keys, so you don't need to worry about it. BTW, always use get_action from the plugins toolkit when writing extensions, that way your extension will keep working if we change the implementation on CKAN core.

So, for example if you only want 10 datasets starting from page 2:

import ckan.plugins as p
result = p.toolkit.get_action('current_package_list_with_resources')({},{'limit':
10, 'page': 2})


Hope this helps,

Adrià



[1] http://docs.ckan.org/en/latest/ckan.logic.action.get.html#ckan.logic.action.get.current_package_list_with_resources

On 16 May 2013 03:56, George Sattler <George.sattler at xvt.com.au> wrote:
> Hello Everyone.
>
>
>
> I am writing an extension and I want to use:
>
> result = 
> ckan.logic.get_action('current_package_list_with_resources')()
>
>
>
> Or even just on command line (without the recommended use of 'get_action'):
>
> result = ckan.logic.action.get.current_package_list_with_resources()
>
>
>
> However this function requires that you pass it 'context' and a 'data_dict'
> objects. I have no idea what exactly these objects are or how I can 
> generate them and the documentation here 
> http://docs.ckan.org/en/latest/ckan.logic.action.get.html#ckan.logic.a
> ction.get.current_package_list_with_resources
> doesn't seem clear enough (to me, anyway).
>
>
>
> Would someone please be able to explain what params I should be supplying.
>
>
>
> Thanks
>
> George
>
>
>
>
>
>
> ________________________________
> This email and any files transmitted with it is confidential and 
> intended solely for the use of the addressee. The unauthorised use, 
> dissemination, forwarding, printing or copying of this communication 
> is strictly prohibited. If you have received this communication in 
> error please notify us immediately by reply email and destroy this 
> communication. Any views and opinions presented in this email are 
> solely those of the author and do not necessarily represent the views 
> of XVT Solutions. The recipient should check this email and any 
> attachments for viruses. XVT Solutions accepts no liability for the 
> content of this email, and any damage caused by any viruses that could potentially be transmitted through this email.
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: http://lists.okfn.org/mailman/options/ckan-dev
>

_______________________________________________
ckan-dev mailing list
ckan-dev at lists.okfn.org
http://lists.okfn.org/mailman/listinfo/ckan-dev
Unsubscribe: http://lists.okfn.org/mailman/options/ckan-dev




More information about the ckan-dev mailing list