Skip to content

Commit

Permalink
fixed shippable collection (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
eikichi18 authored Oct 18, 2024
1 parent 5b39f47 commit 0e8a57f
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Changelog
3.1.1 (unreleased)
------------------

- Nothing changed yet.
- Fixed shippable collection
[eikichi18]


3.1.0 (2024-06-13)
Expand Down
147 changes: 146 additions & 1 deletion src/rer/newsletter/content/shippable_collection.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,154 @@
# -*- coding: utf-8 -*-
from Acquisition import aq_chain
from plone import api
from plone.app.contenttypes.content import Collection
from plone.app.layout.viewlets.content import ContentHistoryView
from rer.newsletter.adapter.sender import IChannelSender
from rer.newsletter.interfaces import IShippableCollection
from rer.newsletter.content.channel import Channel
from rer.newsletter.utils import compose_sender
from rer.newsletter.utils import get_site_title
from zope.interface import implementer
from zope.component import getMultiAdapter
from zope.component import queryUtility

import re

try:
from collective.taskqueue.interfaces import ITaskQueue
from rer.newsletter.queue.handler import QUEUE_NAME
from rer.newsletter.queue.interfaces import IMessageQueue

HAS_TASKQUEUE = True
except ImportError:
HAS_TASKQUEUE = False


@implementer(IShippableCollection)
class ShippableCollection(Collection):
pass
def get_channel(self):
for parent in aq_chain(self):
if isinstance(parent, Channel):
return parent
return None

def can_manage_newsletter(self):
if not self.get_channel():
return False

is_editor = "Editor" in api.user.get_roles(obj=self)
can_manage = api.user.get_permissions(obj=self).get(
"rer.newsletter: Manage Newsletter"
) and "Gestore Newsletter" not in api.user.get_roles(obj=self)
if is_editor or can_manage:
return True
return False

def message_already_sent(self):
history = ContentHistoryView(self, self.REQUEST).fullHistory()
if not history:
return False
send_history = [x for x in history if x["action"] == "Invio"]
return len(send_history) > 0

def can_send_message(self):
if not self.get_channel():
return False

if api.content.get_state(obj=self) != "published":
return False
return api.user.get_permissions(obj=self).get("rer.newsletter: Send Newsletter")

def send_syncronous(self):
channel = self.get_channel()
adapter = getMultiAdapter((channel, self.REQUEST), IChannelSender)
return adapter.sendMessage(message=self)

def send_message(self):
if HAS_TASKQUEUE:
message_queue = queryUtility(IMessageQueue)
is_queue_present = queryUtility(ITaskQueue, name=QUEUE_NAME)
if is_queue_present is not None and message_queue is not None:
# se non riesce a connettersi con redis allora si spacca
message_queue.start(self)
else:
# invio sincrono del messaggio
status = self.send_syncronous()
else:
# invio sincrono del messaggio
status = self.send_syncronous()
return {"status": status}

def active_subscriptions(self):
channel = self.get_channel()
if not channel:
return 0
return channel.active_subscriptions()

def compose_message(self, channel):
unsubscribe_footer_template = self.restrictedTraverse(
"@@unsubscribe_channel_template"
)
parameters = {
"portal_name": get_site_title(),
"channel_name": channel.title,
"unsubscribe_link": channel.absolute_url(),
"enabled": channel.standard_unsubscribe,
}

message_template = self.restrictedTraverse("@@messagepreview_view")
parameters = {
"message_subheader": f"""
<tr>
<td align="left" colspan="2">
<div class="newsletterTitle">
<h1>{self.title}</h1>
</div>
</td>
</tr>""",
"message_unsubscribe_default": f"""
<tr>
<td align="left" colspan="2">
<div class="newsletter-unsubscribe">
{unsubscribe_footer_template(**parameters)}
</div>
</td>
</tr>
""",
}

body = message_template(**parameters)

# passo la mail per il transform
portal = api.portal.get()
body = portal.portal_transforms.convertTo("text/mail", body)

return body

def send_preview(self, email):
emails = re.compile("[,|;]").split(email)
channel = self.get_channel()
if not channel:
# non riesco a recuperare le info di un channel
return

body = self.compose_message(channel=channel)

sender = compose_sender(channel=channel)

nl_subject = " - " + channel.subject_email if channel.subject_email else ""

subject = "Messaggio di prova - " + self.title + nl_subject
# per mandare la mail non passo per l'utility
# in ogni caso questa mail viene mandata da plone
mail_host = api.portal.get_tool(name="MailHost")
for email in emails:
mail_host.send(
body.getData(),
mto=email.strip(),
mfrom=sender,
subject=subject,
charset="utf-8",
msg_type="text/html",
immediate=True,
)

0 comments on commit 0e8a57f

Please sign in to comment.