Esempio n. 1
0
    def approve_comment(self):
        _ = self.macro.request.getText

        # Source
        origin = os.path.join(self.approval_dir, get_input(self.macro, "file"))
        comment = read_comment(origin)

        # Destination
        page = Page(self.macro.request, comment["page"])
        if not page.exists():
            self.msg.append(_("The page this comment was written for don't exist any more"))
            return

        dest_dir = page.getPagePath("comments", check_create=1)
        destination = os.path.join(dest_dir, get_input(self.macro, "file"))

        # Rename the file:
        os.rename(origin, destination)
        self.msg.append(_("Comment approved"))

        # Notify page subscribers:
        notify_subscribers(self.macro, comment)
Esempio n. 2
0
    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("")