[ckan-dev] Function called on resource update

lucia.espona at wsl.ch lucia.espona at wsl.ch
Fri Apr 28 20:34:31 UTC 2017


Dear Stefan

Thanks for your helpful answer.

I tried the IResourceController option because it sounded easier 0:)

Now I have the issue of getting the previous value of the field, with after_update I only get the new value.

I used before_update where I can get the previous and the modified value. I do not want to trigger the mail notification from there because if some other validation fails it maybe the user cancels the update or  the before_update is called again when the user tries to fix the validation error. I would rather avoid spamming the users :D

So I tried to put the old value in the context in before_update so that it can be read in the after_update:
    # IResourceController
    def before_update(self, context, current, resource):
        context['__restricted_previous_value'] = current.get('restricted')

    def after_update(self, context, resource):
        log.debug("OLD  restricted= {0}".format(context.get('__restricted_previous_value')))
        log.debug("NEW restricted = {0}".format(resource.get('restricted')))

It works but I am not sure if this a good idea or rather a hack. Should I delete the field from the context in after_update? Is there a better way? 

Thanks in advance and have a nice weekend,
Lucia

  


     

_________________________________________________________
Dr. Lucia Espona Pernas

Swiss Federal Institute for Forest, Snow and Landscape Research WSL
Hauptgebäaude Labortrakt (HL C21)
Zürcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 739 28 71 phone direct
+41 44 739 21 11 reception

www.wsl.ch

-----"ckan-dev" <ckan-dev-bounces at lists.okfn.org> wrote: -----
To: CKAN Development Discussions <ckan-dev at lists.okfn.org>
From: Stefan Oderbolz 
Sent by: "ckan-dev" 
Date: 27.04.2017 17:22
Subject: Re: [ckan-dev] Function called on resource update

Hi Lucia,

this seems like a perfect case for the IDomainObjectModification interface (http://docs.ckan.org/en/latest/extensions/plugin-interfaces.html#ckan.plugins.interfaces.IDomainObjectModification).

You can implement that in your extension and every time a resource is updated, the notify() method is called. E.g. the datastore part in CKAN core uses that: https://github.com/ckan/ckan/blob/af2f061fdd08219d5070415be118d6063d38341f/ckanext/datastore/plugin.py#L130
You then have to check that the entity is acutally a resource with something like isinstance(entity, model.Resource)
and that the operation is "changed" (operation == model.domain_object.DomainObjectOperation.changed).

Once that is clear, then you'll have to check if the allowed_users field has changed (I guess the value should be in entity.allowed_users).

If this seems like overkill, then maybe using the IResourceController interface with the after_update method is the better choice (http://docs.ckan.org/en/latest/extensions/plugin-interfaces.html#ckan.plugins.interfaces.IResourceController.after_update)

Hope this helps.

- Stefan 
On Thu, Apr 27, 2017 at 3:33 PM,  <lucia.espona at wsl.ch> wrote:
 Dear all

I would like to know how to define a function that will be called when a resource is updated. The function will need to know which information (field, new value, old value) was changed.
What would be the best way to do this? I am using scheming and was thinking of using a validator.

Here is the context: in my ckanext-restricted extension, I would like that the user is notified by mail when he/she is granted access to access a restricted resource. This happens when the owner updates a field called "allowed_users" in the resource metadata. It is important that only the new added users get a mail.

Any help will be appreciated :)

Best regards,
Lucia


_________________________________________________________
Dr. Lucia Espona Pernas

Swiss Federal Institute for Forest, Snow and Landscape Research WSL
Hauptgebäaude Labortrakt (HL C21)
Zürcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 739 28 71 phone direct
+41 44 739 21 11 reception

www.wsl.ch
_______________________________________________
 ckan-dev mailing list
 ckan-dev at lists.okfn.org
 https://lists.okfn.org/mailman/listinfo/ckan-dev
 Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
 



-- 
Liip AG  // Limmatstrasse 183 //  CH-8005 Zürich
Tel +41 43 500 39 80 // GnuPG 0x7B588C67 // www.liip.ch
  
_______________________________________________
ckan-dev mailing list
ckan-dev at lists.okfn.org
https://lists.okfn.org/mailman/listinfo/ckan-dev
Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20170428/31c458e6/attachment-0003.html>


More information about the ckan-dev mailing list