Commits

Anonymous committed 0447f1b

added feeds.

Comments (0)

Files changed (9)

 
 
 __all__ = ['get_site', 'get_page', 'get_pageof',
-'all_pages', 'get_diff', 'LEXERS_CHOICE']
+'all_pages', 'get_diff', 'LEXERS_CHOICE', 'get_changes']
 
 def _get_lexers():
     lexers = get_all_lexers()
     return list(iter(rows))
     
     
+def get_changes(db, siteid):
+    if siteid is None:
+        return []
+    rows = Page.view(db, '_view/page/all_pages', key=siteid, count=50)
+    result = list(iter(rows))
+    if result: # order revisions
+        result.sort(lambda a,b: cmp(a.updated, b.updated))
+        result.reverse()
+    return result
+    
 def get_diff(db, page, rev1, rev2):
     a = int(rev1)
     b = int(rev2)

couchit/models.py

             return []
         rows = self.view(db, '_view/page/revisions', key=self.id)
         result = list(iter(rows))
-        print result
         if result: # order revisions
             result.sort(lambda a,b: cmp(a.updated, b.updated))
             result.reverse()
     'history_page': views.history_page,
     'revision_page': views.revision_page,
     'diff_page': views.diff_page,
+    'revisions_feed': views.revisions_feed,
+    'site_changes': views.site_changes,
     'site_design': views.site_design,
     'site_settings': views.site_settings,
     'site_claim': views.site_claim,
     Rule('/<cname>/site/design', endpoint='site_design'),
     Rule('/<cname>/site/claim', endpoint='site_claim'),
     Rule('/<cname>/site/settings', endpoint='site_settings'),
+    Rule('/<cname>/site/changes', endpoint='site_changes'),
+    Rule('/<cname>/site/changes.<feedtype>', endpoint='site_changes'),
+    Rule('/<cname>/<pagename>/revisions.<feedtype>', endpoint='revisions_feed'),
     Rule('/<cname>/<pagename>/revision/<nb_revision>', endpoint='revision_page'),
     Rule('/<cname>/<pagename>/history', endpoint='history_page'),
     Rule('/<cname>/<pagename>/edit', endpoint='edit_page'),
 
 import re
 import urllib2
+from jinja2.filters import do_truncate, do_striptags
 from werkzeug import redirect
+from werkzeug.contrib.atom import AtomFeed
 from werkzeug.routing import NotFound
 from werkzeug.utils import url_unquote
 from couchit import settings
 from couchit.api import *
 from couchit.http import BCResponse
 from couchit.template import render_response, url_for, render_template, send_json
-from couchit.utils import local, make_hash
+from couchit.utils import local, make_hash, datetime_tojson
 
 
 FORBIDDEN_PAGES = ['site', 'delete', 'edit', 'create', 'history', 'changes']
     if pagename is None:
         pagename ='Home'
     page = get_page(local.db, request.site.id, pagename)
-    if not Page:
+    if not page:
         if request.is_xhr:
             return send_json({'ok': False, 'reason': 'not found'})
         return NotFound
 
     return render_response('page/diff.html', page=page, pages=pages, diff=diff, rev1=rev1, 
     rev2=rev2, revisions=all_revisions)
+
+    
+@site_required  
+def revisions_feed(request, cname=None, pagename=None, feedtype="atom"):
+    if pagename is None:
+        pagename ='Home'
+    page = get_page(local.db, request.site.id, pagename)
+    if not page:
+        return NotFound
+    all_revisions = [page] + page.revisions(local.db)
+    if feedtype == "atom":
+        feed = AtomFeed(
+                    title="%s: Latest revisions of %s" % (request.site.cname, page.title),
+                    subtitle=request.site.subtitle,
+                    updated = page.updated,
+                    id = page.title.replace(" ", "_")
+        )
+        for rev in all_revisions:
+            for change in rev.changes:
+                if change['type'] != "unmod":
+                    title = "\n".join(change['changed']['lines'])
+                    title = do_truncate(do_striptags(title), 60)
+            title = title and title or "Edited."
+            feed.add(title, rev.content, 
+                updated=rev.updated,
+                url=url_for("revision_page", 
+                    cname=request.site.cname, pagename=pagename, 
+                    nb_revision=rev.nb_revision
+                ),
+                id=str(rev.nb_revision)
+            )
+        return feed.get_response()
+    else:
+        json = {
+            'title': "%s: Latest revisions of %s" % (request.site.cname, page.title),
+            'subtitle': request.site.subtitle,
+            'updated':datetime_tojson(page.updated),
+            'revisions': []
+        }
+        for rev in all_revisions:
+            title = ''
+            for change in rev.changes:
+                if change['type'] != "unmod":
+                    title = "\n".join(change['changed']['lines'])
+                    title = do_truncate(do_striptags(title), 60)
+                    
+            title = title and title or "Edited."
+            url = url_for("revision_page", 
+                        cname=request.site.cname, pagename=pagename, 
+                        nb_revision=rev.nb_revision
+            )
+            json['revisions'].append({
+                'title': title,
+                'content': rev.content,
+                'url':  url,
+                'updated':datetime_tojson(rev.updated),
+                'id':rev.nb_revision
+            })
+        return send_json(json)
+    
+@site_required
+def site_changes(request, cname, feedtype=None):
+    
+    pages = all_pages(local.db, request.site.id)
+    changes = get_changes(local.db, request.site.id)
+
+    if feedtype == "atom":
+        feed = AtomFeed(
+                    title="%s: Latest changes" % request.site.title and request.site.title or request.site.cname,
+                    subtitle=request.site.subtitle,
+                    updated = changes[0].updated,
+                    id = request.site.cname
+        )
+        for rev in changes:
+            feed.add(rev.title, rev.content, 
+                updated=rev.updated,
+                url=url_for("show_page", 
+                    cname=request.site.cname, pagename=rev.title.replace(' ', '_')
+                ),
+                id=rev.title.replace(' ', '_')
+            )
+        return feed.get_response()
+    elif feedtype == 'json':
+        json = {
+                'title': "%s: Latest changes" % request.site.title and request.site.title or request.site.cname,
+                'subtitle': request.site.subtitle,
+                'updated':datetime_tojson(changes[0].updated),
+                'pages': []
+            }
+        for rev in changes:
+            url = url_for("show_page", 
+                        cname=request.site.cname, pagename=rev.title.replace(' ', '_')
+            )
+            json['pages'].append({
+                'title': rev.title,
+                'content': rev.content,
+                'url':  url,
+                'updated':datetime_tojson(rev.updated),
+                'id':rev.title.replace(' ', '_')
+            })
+        return send_json(json)
+
+    return render_response('site/changes.html', changes=changes, pages=pages)
+        
     
 @site_required
 def site_claim(request, cname):
     
 @site_required
 def site_settings(request, cname):
-    print request.site
     return render_response('site/settings.html', site=request.site)
     
 @site_required

static/css/layout.css

 ul.page_infos  { 
     list-style: none;
     border-top: 1px solid #ccc;
+    border-bottom: 1px solid #ccc;
 }
 ul.page_infos li {
     display: inline;

templates/page/diff.html

     Compare
 {% endblock %}
 
+{% block head %}
+    <link rel="alternate" type="application/atom+xml" href="{{ url_for('revisions_feed', cname=site.cname, pagename=page.title|replace(" ", "_"), feedtype='atom') }}" title="Atom Revisions Feed" />
+{% endblock %}
 
 {% block page %}
 <section id="pdiff">

templates/page/history.html

 {% extends "base.html" %}
 {% block title %}Revision history of {{ page.title }}{% endblock %}
+{% block head %}
+    <link rel="alternate" type="application/atom+xml" href="{{ url_for('revisions_feed', cname=site.cname, pagename=page.title|replace(" ", "_"), feedtype='atom') }}" title="Atom Revisions Feed" />
+{% endblock %}
 {% block page %}
 <section id="phistory">
     <h2>Revision history of <a href="{{ url_for('show_page', cname=site.cname, pagename=page.title|replace(" ", "_")) }}">{{ page.title }}</a></h2>

templates/page/show.html

 {% block title %}{{ page.title }} (revision {{ page.nb_revision }}){% endblock %}
 
 {% block head %}
+{% if page.title == "Home" %}
+    <link rel="alternate" type="application/atom+xml" href="{{ url_for('site_changes', cname=site.cname, feedtype='atom') }}" title="Atom Recent Changes Feed" />
+{% endif %}
+
+<link rel="alternate" type="application/atom+xml" href="{{ url_for('revisions_feed', cname=site.cname, pagename=page.title|replace(" ", "_"), feedtype='atom') }}" title="Atom Revisions Feed" />
+
 <link rel="stylesheet" type="text/css" href="/static/css/textarea.css" />
 <script src="/static/js/dragdrop.js" type="text/javascript"></script>
 <script src="/static/js/tabs.js" type="text/javascript"></script>

templates/sidebar.html

 
 <nav>
     <ul>
-        <li><a href="">recent changes</a></li>
+        <li><a href="{{ url_for('site_changes', cname=site.cname) }}">recent changes</a></li>
         <li><a href="{{ url_for('site_settings', cname=site.cname) }}">settings</a></li>
         <li><a href="{{ url_for('site_design', cname=site.cname) }}">design</a></li>
     </ul>
         <li><a href="{{ url_for('show_page', cname=site.cname, pagename=p.title|replace(" ", "_"))  }}">{{ p.title }}</a></li>
         {% endfor %}
     </ul>
-</div>
-
-
-
+</div>