Package: ckanext-twitter
ckanext-twitterA CKAN extension that enables users to post a tweet every time a dataset is created or updated.OverviewThis extension connects a CKAN instance to a Twitter account so that when a dataset is updated or created (i.e. theafter_updatehook is called), the user has the option to post a tweet about the activity.InstallationPath variables used below:$INSTALL_FOLDER(i.e. where CKAN is installed), e.g./usr/lib/ckan/default$CONFIG_FILE, e.g./etc/ckan/default/development.iniInstalling from PyPIpipinstallckanext-twitterInstalling from sourceClone the repository into thesrcfolder:cd$INSTALL_FOLDER/src
gitclonehttps://github.com/NaturalHistoryMuseum/ckanext-twitter.gitActivate the virtual env:.$INSTALL_FOLDER/bin/activateInstall via pip:pipinstall$INSTALL_FOLDER/src/ckanext-twitterInstalling in editable modeInstalling from apyproject.tomlin editable mode (i.e.pip install -e) requiressetuptools>=64; however, CKAN 2.9 requiressetuptools==44.1.0. Seeour CKAN forkfor a version of v2.9 that uses an updated setuptools if this functionality is something you need.Post-install setupAdd 'twitter' to the list of plugins in your$CONFIG_FILE:ckan.plugins=... twitterAdd a 'tweet' block toread_base.html:{% block tweet %}
{{ super() }}
{% endblock %}Optionally, override the styling of the block by creating anajax_snippets/edit_tweet.htmlfile.ConfigurationThese are the options that can be specified in your .ini config file. The onlyrequiredoptions are the twitter credentials. Everything else has a sensible default set.[REQUIRED]NameDescriptionOptionsckanext.twitter.consumer_keyYour Twitter consumer keyckanext.twitter.consumer_secretYour Twitter consumer secretckanext.twitter.token_keyYour Twitter token keyckanext.twitter.token_secretYour Twitter token secretAll of these can be obtained by creating a single-user app atapps.twitter.com. They can be found on the "keys and access tokens" tab when viewing your app.Tweet TemplatesTweets are generated usingJinja2and use tokens derived from the package dictionary. SeeUsagefor more detail.NameDescriptionDefaultckanext.twitter.newTemplate for tweets about new datasetsNew dataset: "{{ title }}" by {{ author }} ({%- if records != 0 -%} {{ records }} records {%- else -%} {{ resources }} resource {%- endif -%}).ckanext.twitter.updatedTemplate for tweets about updated datasetsUpdated dataset: "{{ title }}" by {{ author }} ({%- if records != 0 -%} {{ records }} records {%- else -%} {{ resources }} resource {%- endif -%}).If your config is created dynamically using Jinja2, you will have to wrap any custom template in{% raw %}{% endraw %}tags andadd a newline after it, e.g.:ckanext.twitter.new={% raw %}{{ title }} by {{ author }} ({{ records }} records) has just been published!{% endraw %}ckanext.twitter.consumer_key={{ twitter.consumer_key }}ckanext.twitter...Other optionsNameDescriptionOptionsDefaultckanext.twitter.debugIs in debug mode; overrides global debug flag if specifiedTrue, FalseFalseckanext.twitter.hours_between_tweetsNumber of hours between tweets about thesame dataset(to prevent spamming)24ckanext.twitter.disable_editIf true, users will not be able to edit the tweet about their dataset before it is posted (though they can still decide not to post it)True, FalseFalseUsageTweet TemplatesToken values for the tweet templates will come from a simplified package dictionary. In these, any collection values (i.e. lists and dictionaries) have been replaced with the number of items, the author list has been significantly shortened, and any long strings will be shortened to fit into the tweet character limit (currently set at 140).For example, if the package dictionary is:{'author':'Dippy Diplodocus, Sophie Stegosaurus','author_email':None,'dataset_category':['Citizen Science'],'doi':'DOI_VALUE','license_title':'Creative Commons Attribution','organization':{'description':'','name':'nhm','is_organization':True,'state':'active'},'resources':[{'mimetype':'image/jpeg','name':'resource_1.jpg','format':'JPEG'},{'mimetype':'image/jpeg','name':'resource_2.jpg','format':'JPEG'}],'title':'Dataset Name'}Then the tokenised dictionary would be:{'author':'Diplodocus et al.',# just the surname of the first author# author_email was None so it's excluded'dataset_category':1,# lists are counted'doi':'DOI_VALUE',# simple string values stay the same'license_title':'Creative Commons Attribution','organization':4,# dicts are also counted'resources':2,'title':'Dataset Name'}And if you had defined the tweet template as:New dataset: "{{title}}" by{{author}}({{resources}}resources).Your tweet would then read:New dataset: "Dataset Name" by Diplodocus et al. (2 resources)TestingThere is a Docker compose configuration available in this repository to make it easier to run tests. The ckan image uses the Dockerfile in thedocker/folder.To run the tests against ckan 2.9.x on Python3:Build the required images:docker-composebuildThen run the tests.
The root of the repository is mounted into the ckan container as a volume by the Docker compose
configuration, so you should only need to rebuild the ckan image if you change the extension's
dependencies.docker-composerunckanNote that the tests shouldn't make any calls to Twitter's API and won't post any tweets.