1. psviderski
  2. moin-2.0

Commits

psviderski  committed 34975bf

Add the +addcomment view and the input form to add new comments.

  • Participants
  • Parent commits e3405fd
  • Branches comments

Comments (0)

Files changed (7)

File MoinMoin/apps/frontend/__init__.py

View file
  • Ignore whitespace
 from flask import Blueprint
 frontend = Blueprint('frontend', __name__)
 import MoinMoin.apps.frontend.views
+import MoinMoin.apps.frontend.comment_views

File MoinMoin/apps/frontend/comment_views.py

View file
  • Ignore whitespace
+# Copyright: 2013 MoinMoin:PavelSviderski
+
+"""
+    MoinMoin - frontend views for the comment system
+"""
+
+
+from datetime import datetime
+
+from flask import abort, redirect, request, url_for
+from flask import g as flaskg
+
+from MoinMoin import log
+logging = log.getLogger(__name__)
+
+from MoinMoin.apps.frontend import frontend
+from MoinMoin.constants.itemtypes import ITEMTYPE_COMMENT
+from MoinMoin.constants.keys import ITEMID
+from MoinMoin.items import Item, NonExistent
+from MoinMoin.items.content import NonExistentContent
+from MoinMoin.storage.middleware.protecting import AccessDenied
+from MoinMoin.util.crypto import make_uuid
+
+
+@frontend.route('/+addcomment/<itemname:item_name>', methods=['POST'])
+def addcomment(item_name):
+    """Add a comment for the wiki item item_name."""
+    try:
+        item = Item.create(item_name)
+    except AccessDenied:
+        abort(403)
+    if isinstance(item, NonExistent):
+        abort(404, item_name)
+    comment_contenttype = request.values.get('meta_form_contenttype')
+    # TODO: Create a nameless comment item.
+    comment_name = u'Comment_{0}'.format(make_uuid())
+    # TODO: Check permissions for creation of comment items for the given content item.
+    try:
+        comment_item = Item.create(comment_name,
+                                   itemtype=ITEMTYPE_COMMENT,
+                                   contenttype=comment_contenttype)
+    except AccessDenied:
+        abort(403)
+    logging.debug("Comment item got a content class: {0!r}"
+                  .format(type(comment_item.content)))
+    if not flaskg.user.may.write(comment_name):
+        abort(403)
+    if isinstance(comment_item.content, NonExistentContent):
+        # The requested contenttype is invalid
+        abort(403)
+    form = comment_item.ModifyForm.from_request(request)
+    form['meta_form']['itemtype'].set(ITEMTYPE_COMMENT)
+    form['meta_form']['comment_for'].set(item.meta[ITEMID])
+    # Preload PTIME with the current datetime
+    form['meta_form']['ptime'].set(datetime.utcnow())
+    if not form.validate():
+        abort(403)
+    meta, data, _, _ = form._dump(comment_item)
+    logging.debug("Comment item dump: meta = {0!r}, data = '{1}'".format(meta, data))
+    comment_item.modify(meta, data)
+    comment_item = Item.create(comment_name)
+    return redirect('{0}#comment_{1}'.format(url_for('.show_item', item_name=item.name),
+                                             comment_item.meta[ITEMID]))

File MoinMoin/apps/frontend/views.py

View file
  • Ignore whitespace
 Disallow: /+search
 Disallow: /+dispatch/
 Disallow: /+admin/
+Disallow: /+addcomment/
 Allow: /
 """, mimetype='text/plain')
 

File MoinMoin/items/__init__.py

View file
  • Ignore whitespace
 from MoinMoin.util.interwiki import url_for_item
 from MoinMoin.util.registry import RegistryBase
 from MoinMoin.util.clock import timed
+from MoinMoin.util.crypto import make_uuid
 from MoinMoin.forms import RequiredText, OptionalText, JSON, Tags
 from MoinMoin.constants.keys import (
     NAME, NAME_OLD, NAME_EXACT, WIKINAME, MTIME, ITEMTYPE,
         # TODO: filter out protected comments
         comments = [flaskg.storage.document(itemid=itemid).name
                     for itemid in self.get_comment_item_ids()]
+        # TODO: Create a nameless comment item.
+        comment_name = u'Comment_{0}'.format(make_uuid())
+        try:
+            comment_item = Item.create(comment_name,
+                                       itemtype=ITEMTYPE_COMMENT,
+                                       contenttype=u'text/x.moin.wiki;charset=utf-8')
+        except AccessDenied:
+            comment_form = None
+        else:
+            comment_form = comment_item.ModifyForm.from_item(comment_item)
         return render_template(self.show_template,
                                item=self, item_name=self.name,
                                rev=self.rev,
                                show_revision=show_revision,
                                show_navigation=show_navigation,
                                comments=comments,
+                               comment_form=comment_form,
                                )
 
     def do_modify(self):

File MoinMoin/items/comment.py

View file
  • Ignore whitespace
     class _ModifyForm(Default._ModifyForm):
         meta_form = CommentMetaForm
         meta_template = 'comment/modify_meta.html'
+        submit_label = L_('Comment')

File MoinMoin/templates/comment/utils.html

View file
  • Ignore whitespace
+{% import "forms.html" as forms %}
+
 {% macro show_comment(comment_name) %}
     {% set comment_item = Item.create(comment_name) %}
     {# TODO: check the item isn't nonexistent #}
     {% endfor %}
     </ol>
 {% endmacro %}
+
+{% macro add_comment(item_name, form) %}
+    <h2 id="moin-comment-form-title">{{ _("Add comment") }}</h2>
+    {{ gen.form.open(form, method='post', action=url_for('frontend.addcomment', item_name=item_name)) }}
+    <dl>
+        {% for e in [
+            'contenttype',
+            ] %}
+            {{ forms.render(form['meta_form'][e]) }}
+        {% endfor %}
+        {{ gen.textarea(form['content_form']['data_text']) }}
+        {{ forms.render_textcha(gen, form) }}
+        {{ forms.render_submit(form, id='moin-save-text-button', ) }}
+    </dl>
+    {{ gen.form.close() }}
+{% endmacro %}

File MoinMoin/themes/foobar/templates/show.html

View file
  • Ignore whitespace
         <h2 id="moin-comments-title">{{ _("Comments (%(num)d)", num=comments|count) }}</h2>
         {{ comment_utils.show_comments_list(comments) }}
     </div>
+    {% if comment_form %}
+        {{ comment_utils.add_comment(item_name, comment_form) }}
+    {% endif %}
     {% endif %}
     {% endblock %}
 {% endblock %}