[ckan-dev] Setting default values within create / new dataset page

Harald von Waldow harald.vonwaldow at eawag.ch
Wed Jun 29 10:26:47 UTC 2016


I am doing something like that with ckanext-scheming.

Here is a copy & paste of the relevant parts; hope that helps.

Cheers,
Harald


# Template helper function in plugin: #################################

import ckan.plugins as plugins
import ckantoolkit as toolkit


def eaw_schema_set_default(values, default_value):
    ## only set default value if current value is empty string or None
    ## or list thereof
    if isinstance(values, basestring):
        if values not in ['', None]:
            return values
    elif isinstance(values, list):
        if not all([x in ['', None] for x in values]):
            return values
        islist = True
    else:
        return values

    if default_value == "context_fullname_email":
        val = "{} <{}>".format(toolkit.c.userobj.fullname,
                               toolkit.c.userobj.fullname)
        if islist:
            values[0] = val
        else:
            values = val
    ## insert elif clauses for other defaults
    #
    #
    #
    #
    ##
    return values


class Eaw_SchemaPlugin(plugins.SingletonPlugin):
    .....
    plugins.implements(plugins.ITemplateHelpers)


    # ITemplateHelpers
    def get_helpers(self):
        return {'eaw_schema_set_default': eaw_schema_set_default}

######################################################################


Field definition in schema_default.json ("default_value" is the
relevant property)
##################################################################
{
   "label": "Author",
   "preset": "eaw_schema_repeating_text_js",
   "default_value": "context_fullname_email",
   "field_name": "author",
   "display_property": "dc:creator",
   "form_blanks": 3
}

###################################################################

In the form_snippet ##############################################


{# before this part, values has been set #}

{% if field.default_value %}
  {% set values = h.eaw_schema_set_default(values, field.default_value) %}
{% endif %}

##################################################################

On 2016-06-29 10:31, Timothy Giles wrote:
> Hi,
> 
>  
> 
> I am trying to set default values for several metadata fields. I thought
> about adding code to after create - but this does not seem to get
> triggered. Is there a better way for me to be able to set (for example)
> Creator Name to the current logged in user? So when the New Dataset
> button is pushed, this is pre filled in, but can be altered.
> 
>  
> 
> I have it working for resources, so after files are uploaded I calculate
> hashes, file name, various other metadata elements.
> 
>  
> 
> Thanks for your time
> 
>  
> 
> Tim
> 
> 
> 
> _______________________________________________
> 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
> 

-- 
Harald von Waldow
Eawag
ICT Services
Ueberlandstrasse 133
8600 Duebendorf
http://www.eawag.ch

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20160629/8cbb8876/attachment-0003.sig>


More information about the ckan-dev mailing list