Commits

David Larlet committed 6197e4f

Added the ability to delete an indexed article

Comments (0)

Files changed (6)

             Rule('/', endpoint='new_url'),
             Rule('/r', endpoint='render_content'),
             Rule('/s', endpoint='search_content'),
+            Rule('/d', endpoint='delete_content'),
             Rule('/doc', endpoint='documentation'),
         ])
 
             title=title,
             error=error,
             theme=CSS_THEME,
-            content=content
+            content=content,
+            quoted_url=quoted_url
         )
 
     def on_search_content(self, request):
                 theme=CSS_THEME
             )
 
+    def on_delete_content(self, request):
+        """View that deletes data related to the `url` GET argument."""
+        # Retrieves terms from GET arguments and unquote it
+        # Retrieves the url from GET arguments and unquote it
+        quoted_url = request.args.get('url')
+        if quoted_url is None:
+            return redirect('/')
+        url = url_unquote_plus(quoted_url)
+        self.storage.delete_content(url)
+
+        return redirect('/')
+
     def on_documentation(self, request):
         """View that displays documentation."""
         return self.render_template('documentation.html',

src/static/themes/default.css

 body        { background: beige; margin: 0; padding: 0;
               font-family: 'Palatino', Arial,
               sans-serif; font-weight: 300; font-size: 18px; }
-.box        { width: 800px; margin: 20px auto; padding: 1em 2em;
+.box        { width: 800px; margin: 20px auto; padding: 0.5em 2em;
               font-size: 150%; line-height: 1.4em;
               background: white; box-shadow: 0 1px 4px #BED1D4;
               border-radius: 2px; }
 .content h3 { font-weight: normal; font-family: 'Georgia'; }
 .content a  { color: #11557C; }
 .content img{ float: left; margin: 1em; }
-.match      { font-weight: bold; }
+.match,
+strong      { font-weight: bold; }

src/static/themes/werkzeug.css

 .content h3 { font-weight: normal; }
 .content a  { color: #11557C; }
 .content img{ float: left; margin: 1em; }
-.match      { font-weight: bold; }
+.match,
+strong      { font-weight: bold; }

src/static/trash.png

Added
New image
 
         `title` and `content` are cached in Redis.
         """
-        search_writer = self.search_index.writer()
-
         # Remove existing content from Redis and Whoosh
         if FORCE_REFRESH:
-            self.redis.delete('title:' + url)
-            self.redis.delete('content:' + url)
-            query = Term('url', url)
-            search_writer.delete_by_query(query)
+            self.delete_content(url)
 
         # Checks from Redis' cache
         if self.redis.exists('title:' + url):
         self.redis.set('content:' + url, content.encode('utf-8'))
 
         # Indexes the retrieved content in Whoosh
+        search_writer = self.search_index.writer()
         search_writer.add_document(url=url,
                                    title=unicode(title),
                                    content=unicode(strip_tags(content)))
         search_writer.commit()
         return title, content
 
+    def delete_content(self, url):
+        """Deletes all local data related to the given `url`."""
+        search_writer = self.search_index.writer()
+        self.redis.delete('title:' + url)
+        self.redis.delete('content:' + url)
+        search_writer.delete_by_query(Term('url', url))
+        search_writer.commit()
+
     def prepend_proxy_url(self, link):
         """From an URL, prepend the proxy URL to be able to browse
 

src/templates/render_content.html

 {% block title %}Browsing {{ url }}{% endblock %}
 {% block body %}
   <h3 class="source">Source: <a href="{{ url }}">{{ url|hostname }}</a></h3>
-  <h1 class="backhome"><a href=/>← <img src="/static/home.png" alt="Home" /></a></h1>
+  <h1 class="backhome"><a href="/">← <img src="/static/home.png" alt="Home" /></a></h1>
   <h2 class="title">{{ title }}</h2>
     {% if error %}
       <p class=error><strong>Error:</strong> {{ error }}
     {% endif %}
   <div class="content">
     {{ content|safe }}
+    <a href="/d?url={{ quoted_url }}" title="Delete article from local database"><img src="/static/trash.png" /></a>
   </div>
   <h3 class="source">Source: <a href="{{ url }}">{{ url|hostname }}</a></h3>
 {% endblock %}