collective / uwosh.snippets

Adds dynamically-updated rich text snippets to Plone. Update a snippet to display the change everywhere the snippet is used (pages, news items, events, anywhere rich text and TinyMCE appear).
4 stars 3 forks source link

Is it possible for uwosh snippets to be used in Python scripts? #14

Open dbitouze opened 7 years ago

dbitouze commented 7 years ago

Suppose you created a Plone 5 Web site for an annual meeting. The date of the meeting changes each year but is supposed to appear in many pages of the site and you consider to use an uwosh.snippet for it (as a replacement of easytemplate which is not designed for Plone 5).

The problem is, you also want to make this (changing) date to appear in mails automatically sent to people who registered and for this you want this snippet to be used in Python scripts.

This is possible for easytemplate snippets. Is it also the case for uwosh.snippet ones?

tkimnguyen commented 7 years ago

Can you show the code you use to send the emails?

dbitouze commented 7 years ago

With a (string) property, say meeting_date defined in the "Properties" tab in the ZMI at the root of the site, it is used in a Python script as:

context.meeting_date

and in easytemplate's document template as:

{{ context.meeting_date.decode('utf-8') }}
tkimnguyen commented 7 years ago

Can you paste the code of the script that sends emails?

dbitouze commented 7 years ago

Can you paste the code of the script that sends emails?

context.MailHost.send(msg, mto=email, mfrom=context.email_from_address, subject=mSubj, charset = context.email_charset)

where, for instance, the mSubj string is defined by something like the following:

mSubj = "[Our nice meeting] Save the date " + context.meeting_date.decode('utf-8')

tkimnguyen commented 7 years ago

You can read the contents of a snippet programmatically by going to the ".snippets" folder, e.g. site['.snippets'][snippetid].text.raw

In my case below the site name is 'Snippets' and I have stored the snippets in a folder named '.snippets':


$ bin/instance -OSnippets debug
Starting debugger (the name "app" is bound to the top-level Zope object)
>>> from zope.component.hooks import getSite
>>> site = getSite()
>>> site
<PloneSite at /Snippets>
>>> snippet=site['.snippets']['snippet-1']
>>> snippet
<Document at /Snippets/.snippets/snippet-1>
>>> snippet.text.raw
u'<p>October 16, 2017</p>'
>>> snippet.text.output
u'<p>October 16, 2017</p>'
>>>  
tkimnguyen commented 7 years ago

You could also try changing the TAL expression in the Snippets Settings control panel. It defaults to context/text/output|context/getText|nothing but you might be able to write an expression that strips the p or other tags that wrap the content you really want. screen shot 2017-08-27 at 10 26 19 am

tkimnguyen commented 7 years ago

See https://docs.plone.org/develop/plone/functionality/expressions.html