Commits

Anonymous committed 324b1e4

don't duplicate code...

Comments (0)

Files changed (1)

friendpaste/paste/views.py

         return redirect ('/%s' % s.id)
     return render_response('paste/edit.html', form=form, snippet=s) 
 
-def _get_snippet_revisions(id):
-    db = local.application.db
-    # we get only last 10 revisions
-    res = db.view('_view/paste/by_id', startkey=[str(id)], endkey=[str(id),10]) 
-    
-    results = []
-    for r in res.__iter__():
-        results.append(r)
-    return results
+
 
 def _get_theme(request):
     theme = request.cookies.get('theme', 'default')
     resp.headers['content-type'] = 'application/xml'
     return resp
 
+
+def _get_snippet_revisions(id):
+    db = local.application.db
+    # we get only last 10 revisions
+    res = db.view('_view/paste/by_id', startkey=[str(id)], endkey=[str(id),10]) 
+    
+    results = []
+    for r in res.__iter__():
+        results.append(r)
+    return results
+
+def get_snippet(id, rev=None):
+    revisions = None
+    res = _get_snippet_revisions(id)
+    if not res:
+        raise NotFound 
+    # set revisions
+    if len(res) > 1:
+        revisions = res[1:]
+        revisions.reverse()
+
+
+    # set snippet (maybe better to use a new view ?) 
+    data = {}
+    if rev and revisions and rev!=res[0].value['revid']:
+        try:
+            for r in revisions:
+                if r.value['revid']==rev:
+                    data = r.value
+                    data['_id']=r.value['parent']
+                    raise FoundRev
+        except FoundRev:
+            pass
+        if not data:
+            data = res[0].value
+            data['_id'] = res[0].id 
+    else:
+        data = res[0].value
+        data['_id'] = res[0].id
+    
+    snippet= Snippet.wrap(data)
+    return snippet, revisions
+
 def view_snippet(request, id):
     mimetypes = request.accept_mimetypes
     
             'url': '%s/%s' % (SITE_URI, s.id)
         })
 
-    revisions = None
-    res = _get_snippet_revisions(id)
-    if not res:
-        raise NotFound 
-    # set revisions
-    if len(res) > 1:
-        revisions = res[1:]
-        revisions.reverse()
+    rev = request.values.get('rev', None)
+    s, revisions = get_snippet(id, rev)
 
-
-    # set snippet (maybe better to use a new view ?)
-    rev = request.values.get('rev', None)
-    data = {}
-    if rev and revisions and rev!=res[0].value['revid']:
-        try:
-            for r in revisions:
-                if r.value['revid']==rev:
-                    data = r.value
-                    data['_id']=r.value['parent']
-                    raise FoundRev
-        except FoundRev:
-            pass
-        if not data:
-            data = res[0].value
-            data['_id'] = res[0].id 
-    else:
-        data = res[0].value
-        data['_id'] = res[0].id
-    
-    s= Snippet.wrap(data)
-   
     if 'application/json' in mimetypes:
         if request.method == 'GET':
             snippet_hash = {
 
 
 def view_rss(request, id):
-    revisions = None
-    res = _get_snippet_revisions(id)
-    if not res:
-        raise NotFound 
-    # set revisions
-    if len(res) > 1:
-        revisions = res[1:]
-        # reorder revisions
-        revisions.sort(lambda a,b: cmp(a.key[1], b.key[1]))
-        revisions.reverse()
-
-
-    # set snippet (maybe better to use a new view ?)
     rev = request.values.get('rev', None)
-    data = {}
-    if rev and revisions and rev!=res[0].value['revid']:
-        try:
-            for r in revisions:
-                if r.value['revid']==rev:
-                    data = r.value
-                    data['_id']=r.value['parent']
-                    raise FoundRev
-        except FoundRev:
-            pass
-        if not data:
-            data = res[0].value
-            data['_id'] = res[0].id
-    else:
-        data = res[0].value
-        data['_id'] = res[0].id
-
-    s = Snippet.wrap(data)
-    entries = []
+    s, revisions = get_snippet(id, rev)
 
     feed = RssFeed(
         title=s.title and s.title or "snippet #%s" % s.id,