mitsuhiko  committed 93619da

Improved the pingback system to not ping links that where already pinged in the past.

  • Participants
  • Parent commits d06f242

Comments (0)

Files changed (3)

File zine/

         forms.Form.__init__(self, initial)
+        # if we have have an old post and the parser is not missing and
+        # it was published when the form was created we collect the old
+        # posts so that we don't have to ping them another time.
+        self._old_links = set()
+        if is not None and not and \
+            self._old_links.update(
+    def find_new_links(self):
+        """Return a list of all new links."""
+        for link in
+            if not link in self._old_links:
+                yield link
     def validate_slug(self, value):
         """Make sure the slug is unique."""
         query = Post.query.filter_by(slug=value)
+ = post
         return post
     def save_changes(self):
             raise ValidationError(_('This slug is already in use'))
     def make_category(self):
-        """A helper function taht creates a category object from the data."""
-        return Category(['name'],['description'],
-              ['slug'] or None)
+        """A helper function that creates a category object from the data."""
+        category = Category(['name'],['description'],
+                  ['slug'] or None)
+        self.category = category
+        return category
     def save_changes(self):
         """Save the changes back to the database.  This also adds a redirect
         """A helper function that creates a new group object."""
         group = Group(['groupname'])
+ = group
         return group
     def save_changes(self):
         user = User(['username'],['password'],
+        self.user = user
         return user
     def save_changes(self):

File zine/

     def find_urls(self):
         """Iterate over all urls in the text.  This will only work if the
-        parser for this post is available.  If it's not the behavior is
-        undefined.  The urls returned are absolute urls.
+        parser for this post is available, otherwise an exception is raised.
+        The URLs returned are absolute URLs.
         from zine.parsers import parse
+        if self.parser_missing:
+            raise TypeError('parser is missing, urls cannot be looked up.')
         found = set()
         this_url = url_for(self, _external=True)
         tree = parse(self.text, self.parser, 'linksearch')

File zine/views/

-from zine.i18n import _
+from zine.i18n import _, ngettext
 from zine.application import get_request, url_for, emit_event, \
      render_response, get_application
 from zine.models import User, Group, Post, Category, Comment, \
     return render_response(template_name, **values)
-def ping_post_links(request, post):
+def ping_post_links(form):
     """A helper that pings the links in a post."""
-    if['maintenance_mode'] or not post.is_published:
+    pinged_successfully = []
+    if['maintenance_mode'] or \
+       not
         flash(_(u'No URLs pinged so far because the post is not '
                 u'publicly available'))
-    elif post.parser_missing:
+    elif
         flash(_(u'Could not ping URLs because the parser for the '
                 u'post is not available any longer.'), 'error')
-        this_url = url_for(post, _external=True)
-        for url in post.find_urls():
+        this_url = url_for(, _external=True)
+        for url in form.find_new_links():
             host = urlparse(url)[1].decode('utf-8', 'ignore')
             html_url = '<a href="%s">%s</a>' % (
                 escape(url, True),
                         'error': e.message
                     }, 'error')
-                flash(_(u'%s was pinged successfully.') %
-                        html_url)
+                pinged_successfully.append(html_url)
+    if pinged_successfully:
+        flash(ngettext(u'The following link was pinged successfully: %s',
+                       u'The following links where pinged successfully: %s',
+                       len(pinged_successfully)) %
+              u', '.join(pinged_successfully))
             emit_event('after-post-saved', post)
             if form['ping_links']:
-                ping_post_links(request, post)
+                ping_post_links(form)
             if 'save_and_continue' in request.form:
                 return redirect_to('admin/edit_post',
             return form.redirect('admin/new_entry')