plone.app.discussion/plone/app/discussion/contentrules.py

100 lines
2.3 KiB
Python
Raw Permalink Normal View History

""" Content rules handlers
"""
from plone.app.discussion import _
try:
from plone.stringinterp.adapters import BaseSubstitution
except ImportError:
2022-05-01 23:14:09 +02:00
class BaseSubstitution:
2022-05-01 23:14:09 +02:00
"""Fallback class if plone.stringinterp is not available"""
def __init__(self, context, **kwargs):
self.context = context
2022-05-01 23:14:09 +02:00
try:
from plone.app.contentrules.handlers import execute
except ImportError:
2022-05-01 23:14:09 +02:00
def execute(context, event):
return False
def execute_comment(event):
2022-05-01 23:14:09 +02:00
"""Execute comment content rules"""
execute(event.object, event)
class CommentSubstitution(BaseSubstitution):
2022-05-01 23:14:09 +02:00
"""Comment string substitution"""
def __init__(self, context, **kwargs):
super().__init__(context, **kwargs)
@property
def event(self):
2022-05-01 23:14:09 +02:00
"""event that triggered the content rule"""
return self.context.REQUEST.get("event")
@property
def comment(self):
2022-05-01 23:14:09 +02:00
"""Get changed inline comment"""
return self.event.comment
class Id(CommentSubstitution):
2022-05-01 23:14:09 +02:00
"""Comment id string substitution"""
category = _("Comments")
description = _("Comment id")
def safe_call(self):
2022-05-01 23:14:09 +02:00
"""Safe call"""
return getattr(self.comment, "comment_id", "")
class Text(CommentSubstitution):
2022-05-01 23:14:09 +02:00
"""Comment text"""
category = _("Comments")
description = _("Comment text")
def safe_call(self):
2022-05-01 23:14:09 +02:00
"""Safe call"""
return getattr(self.comment, "text", "")
class AuthorUserName(CommentSubstitution):
2022-05-01 23:14:09 +02:00
"""Comment author user name string substitution"""
category = _("Comments")
description = _("Comment author user name")
def safe_call(self):
2022-05-01 23:14:09 +02:00
"""Safe call"""
return getattr(self.comment, "author_username", "")
class AuthorFullName(CommentSubstitution):
2022-05-01 23:14:09 +02:00
"""Comment author full name string substitution"""
category = _("Comments")
description = _("Comment author full name")
def safe_call(self):
2022-05-01 23:14:09 +02:00
"""Safe call"""
return getattr(self.comment, "author_name", "")
class AuthorEmail(CommentSubstitution):
2022-05-01 23:14:09 +02:00
"""Comment author email string substitution"""
category = _("Comments")
description = _("Comment author email")
def safe_call(self):
2022-05-01 23:14:09 +02:00
"""Safe call"""
return getattr(self.comment, "author_email", "")