Skip to content

Instantly share code, notes, and snippets.

@mpampols
Created March 31, 2012 14:32
Show Gist options
  • Save mpampols/2265553 to your computer and use it in GitHub Desktop.
Save mpampols/2265553 to your computer and use it in GitHub Desktop.
Add / Remove Plone object subscribers and send a notification email on state change
# Add a subscriber email to Plone object with Subscribers field
def add_subscriber(self,plone_object,email):
from Products.PythonScripts.standard import html_quote
from Products.CMFCore.utils import getToolByName
plone_utils = getToolByName(self, 'plone_utils', None)
workflowTool = getToolByName(self, "portal_workflow")
urltool = getToolByName(self, 'portal_url')
section_path = urltool.getPortalPath()+ '/'
obj_query = self.portal_catalog({'meta_type':{'query':['Content Type']},
'review_state':'published',
'id':object_id
})[:1]
obj_to_subscribe = obj_query.getObject()
actual_subscribers = list(obj_to_subscribe.getSubscribers())
if validateEmail(email):
if email in actual_subscribers:
message = "Email alreaxy exists."
else:
new_subscribers = actual_subscribers.append(email)
new_subscribers_tuple = tuple(actual_subscribers)
obj_to_subscribe.setSubscribers(new_subscribers_tuple)
obj_to_subscribe.reindexObject()
message = "Subscriber added to list"
else:
message = "Invalid Email."
return message
def validateEmail(email):
import re
if email==None: return False
return re.match(r"^[a-zA-Z0-9._%-+]+\@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,}$", email)!=None
# This function can be used to send a notification email when a object state changes.
# It also creates a specific user hash that can be used to unsubscribe from this notifications
# See: do_unsubscribe on unsubscribe.py
def sendEmail(self,state_change):
import sha
obj = state_change.object
mship = self.portal_membership
mhost = self.MailHost
adminEmail = self.email_from_address
administratorEmailAddress = 'put_yout@email.here'
# Get a list of subscribers on this particular object
subscribers = obj.getSubscribers()
for subscriber in subscribers:
to_string = subscriber
message = """
From: %s
To: %s
Subject: <SUBJECT LINE>
<EMAIL TEXT CONTENT>
--
To unsubscribe from this notification, click here:
http://<URL>/unsubscribe?op=%s&email=%s&s=%s
"""
string_secret = "<YOUR SECRET RANDOM STRING>"
mail_secret = sha.new(to_string + obj.getId() + string_secret).hexdigest()
msg = message % (
administratorEmailAddress,
to_string,
obj.absolute_url(),
obj.getId(),
to_string,
mail_secret
)
mh = self.MailHost
mh.send(msg,
mto=to_string,
mfrom=administratorEmailAddress,
subject="<SUBJECT LINE>")
print "Notification sent."
return True
# In this example, the subscribed object must have a field with a list of subscribed people (getSubscribers)
# See: sendEmail on send_email.py
def do_unsubscribe(self,object_id,email,s):
import sha
from Products.PythonScripts.standard import html_quote
from Products.CMFCore.utils import getToolByName
plone_utils = getToolByName(self, 'plone_utils', None)
workflowTool = getToolByName(self, "portal_workflow")
urltool = getToolByName(self, 'portal_url')
section_path = urltool.getPortalPath()+ '/'
# Search the object where user is subscribed to
obj_query = self.portal_catalog({'meta_type':{'query':['Content Type']},
'review_state':'published',
'id':object_id
})[:1]
obj_to_unsubscribe = obj_query.getObject()
if (obj_to_unsubscribe):
actual_subscribers = list(obj_to_unsubscribe.getSubscribers())
if email in actual_subscribers:
string_secret = "<YOUR SECRET RANDOM STRING>"
subscription_secret = sha.new(email + obj_to_unsubscribe.getId() + string_secret).hexdigest()
# Check if the secret is correct, then we can unsubscribe the user from our field list
# "s" is the secret we receive from the email URL
if (subscription_secret == s):
if (email in actual_subscribers):
actual_subscribers.remove(email)
new_subscribers_tuple = tuple(actual_subscribers)
obj_oposicion.setSubscribers(new_subscribers_tuple)
obj_oposicion.reindexObject()
message = 'Successfully unsubscribed from this object.'
else:
message = 'You are no longer subscribed.'
else:
# Object doesn't exist
message = 'The object you want to unsubscribe no longer exists.'
return message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment