def __init__(self, macro ): self.macro = macro self.page = macro.request.page self.user = macro.request.user self.page_name = macro.formatter.page.page_name self.msg = '' self.reset_comment() self.errors = [] try: passpartout_group = macro.request.groups[ get_cfg(macro, 'comment_passpartout_group', 'PasspartoutGroup' )] if self.user.name in passpartout_group: passpartout = True else: passpartout = False except GroupDoesNotExistError: passpartout = False self.passpartout = passpartout self.moderate = get_cfg(macro, 'comment_moderate', True) and not passpartout if macro.request.method == 'POST': self.save_comment()
def renderInPage(self): """ Render comments form in page context. """ _ = self.macro.request.getText html = u'''<div class="comments_form"> <form method="POST" action="%(page_uri)s"> <input type="hidden" name="do" value="comment_add"> ''' % { 'page_uri': self.macro.request.request.url } if self.msg: html += u'<div id="comment_message">' html += u'<p>%s</p>' % self.msg html += u'</div>' if self.errors: html += u'<div id="comment_error">' if len(self.errors) > 1: html += u'<p>%s</p><ul>' % _('Your comment has errors:') else: html += u'<p>%s</p><ul>' % _('Your comment has one error:') for error in self.errors: html += u'<li>%s</li>' % error html += u'</div>' html += ''' <ul> <li> <span class="name">%(name_label)s</span> <input type="text" id="name" maxlength=128 name="user_name" value="%(user_name)s" size=8> <span class="comment_body">%(comment_label)s</span> <input type="text" name="comment" value="%(comment)s" size=64> ''' % { 'name_label': u'名前: ', 'comment_label': u'コメント: ', 'page_name': self.page_name, 'user_name': self.comment['user_name'], 'comment': self.comment['comment'],} if get_cfg(self.macro, 'comment_recaptcha', False) and not self.passpartout: import captcha html += u""" <tr> <th>%(recaptcha_label)s</th> <td>%(recaptcha)s</td> </tr>""" % { 'recaptcha' : captcha.displayhtml( get_cfg(self.macro, 'comment_recaptcha_public_key')), 'recaptcha_label': _('Are you human?') } html += """ <input type="submit" value="%(label)s"> </form></div>""" % { 'label': _('Send comment') } try: return self.macro.formatter.rawHTML(html) except: return self.macro.formatter.escapedText('')
def errors_check(self): """ Check the form for errors. """ _ = self.macro.request.getText errors = [] if not self.comment['user_name']: #errors.append( _('You must enter your name.') ) self.comment['user_name'] = u'' if len(self.comment['user_name']) > 128: errors.append( _('Please use a shorter name.') ) if not self.comment['comment']: errors.append( _('You have yet to write your comment.') ) if 'http://' in self.comment['comment'] or 'https://' in self.comment['comment']: errors.append(u'URL in comment is not allowed for anti-spam.') if len(self.comment['comment']) > 10240: errors.append( _('Maximum number of characters is 10240.')) if ( get_cfg(self.macro, 'comment_recaptcha', False) and not self.passpartout and not self.captcha.is_valid): errors.append( _("I'm not sure you're human! Please fill in the captcha.")) return errors
def __init__(self, macro): self.macro = macro self.page_name = get_cfg(macro, "comment_approval_page", "CommentsApproval") self.msg = [] if self.page_name != macro.formatter.page.page_name: # It's mandatory to run the ApproveComments macro from the # comment_approval_page defined in the configuration return page = Page(macro.request, self.page_name) if not page.exists(): raise ApproveError("You have to create the approval page!") self.approval_dir = page.getPagePath("", check_create=0) if macro.request.method == "POST": if get_input(macro, "do") == u"comment_delete": self.delete_comment() if get_input(macro, "do") == u"comment_approve": self.approve_comment()
def render_in_page(self): def cmp_page_time(a, b): if a["page"] < b["page"]: return -1 elif a["page"] > b["page"]: return 1 else: if a["time"] < b["time"]: return -1 elif a["time"] > b["time"]: return 1 return 0 _ = self.macro.request.getText if self.page_name != self.macro.formatter.page.page_name: return self.macro.formatter.text( _( "Sorry, but the ApproveComments macro must be " "used on %(page_name)s page." % {"page_name": self.page_name} ) ) html = [] if self.msg: html.append('<div class="comment_message"><ul>') for m in self.msg: html.append("<li>%s</li>" % m) html.append("</ul></div>") files = glob.glob(os.path.join(self.approval_dir, "*.txt")) if not files: html.append(u"<p>%s</p>" % _("There's no comment awaiting for moderation.")) else: comments = [] # Read the comments: for file_name in files: comment = read_comment(file_name) comment["file_name"] = file_name comments.append(comment) # Sort the coments by page, then by time comments.sort(cmp_page_time) for comment in comments: html.append( u"""<div class="comment_approval"> <table> <tr><th colspan=2>%(intro)s %(page_name)s</th></tr> <tr><td>%(name)s</td><td>%(comment_name)s</td></tr> <tr><td>%(time)s</td><td>%(comment_time)s</td></tr>""" % { "intro": _("Comment to"), "page_name": comment["page"], "name": _("Name:"), "time": _("Time:"), "comment_time": comment["time"].strftime("%Y.%m.%d %H:%M"), "comment_name": comment["user_name"], } ) if get_cfg(self.macro, "comment_store_addr", False): html.append( u" <tr><td>%(remote_addr)s</td><td>%(comment_addr)s</td></tr>" % {"remote_addr": _("Remote address:"), "comment_addr": comment["remote_addr"]} ) html.append( u""" <tr><td colspan=2>%(comment_text)s</td></tr> <tr> <td colspan=2> <form method="POST" action="%(page_uri)s"> <input type="hidden" name="do" value="comment_delete"> <input type="submit" value="%(button_delete)s" id="delete"> <input type="hidden" name="file" value="%(comment_file)s"> </form> <form method="POST" action="%(page_uri)s"> <input type="hidden" name="do" value="comment_approve"> <input type="submit" value="%(button_accept)s" id="ok"> <input type="hidden" name="file" value="%(comment_file)s"> </form> </td> </tr> </table> </div><br />""" % { "comment_text": "<p>".join(comment["comment"].split("\n")), "comment_file": os.path.basename(comment["file_name"]), "page_uri": self.macro.request.request.url, "button_delete": _("Delete"), "button_accept": _("Accept"), } ) try: return self.macro.formatter.rawHTML("\n".join(html)) except: return self.macro.formatter.escapedText("")
def save_comment( self ): _ = self.macro.request.getText if get_input(self.macro, 'do' ) != u'comment_add': # This is not a comment post do nothing return if get_cfg(self.macro, 'comment_recaptcha', False ) and not self.passpartout: import captcha self.captcha = captcha.submit ( get_input(self.macro, 'recaptcha_challenge_field'), get_input(self.macro, 'recaptcha_response_field'), get_cfg(self.macro, 'comment_recaptcha_private_key'), self.macro.request.remote_addr ) self.get_comment() self.errors = self.errors_check() if not self.errors: # Save the comment # Find out where to save the comment: if self.moderate: # This commet will be added to the moderation queue page = Page(self.macro.request, get_cfg(self.macro, 'comment_approval_page', 'CommentsApproval')) comment_dir = page.getPagePath('', check_create=0) else: # The comment will be immediately posted page = Page(self.macro.request,self.page_name) comment_dir = page.getPagePath('comments', check_create=1) # Compose the comment structure and write it now = datetime.now() random_str = ''.join([choice(letters + digits) for i in range(20)]) comment_file = '%s-%s.txt' % (now.strftime("%s"), random_str) file_name = os.path.join(comment_dir, comment_file) comment = self.comment comment['page'] = self.page_name comment['time'] = now if get_cfg(self.macro, 'comment_store_addr', False): comment['remote_addr'] = self.macro.request.remote_addr if self.moderate: self.msg = _('Your comment awaits moderation. Thank you.') else: self.msg = _('Your comment has been posted. Thank you.') write_comment( file_name, comment ) if self.moderate: # If we have defined a list of moderators to notify and this user is # moderated then a message is sent to the moderator list moderators = get_cfg(self.macro, 'comment_moderators', None) if moderators: sendmail.sendmail( self.macro.request, moderators.split(','), _('New comment awaits moderation for page %(page)s' % self.comment ), _('New comment awaits moderation:\n\nPage: %(page)s\nFrom: %(user_name)s\nMessage:\n\n%(comment)s\n\n--' % self.comment )) else: # Send notification to page subscribers if the page notify_subscribers(self.macro, self.comment) # clean up the fields to display self.reset_comment()