Ronny Pfannschmidt committed 274cc80

clean up pastes views a bit more

Comments (0)

Files changed (1)


 from werkzeug import redirect, Response
 from werkzeug.exceptions import NotFound
-from flask import request, session, render_template
+from flask import request, session, render_template, url_for
 from lodgeit.lib import antispam
 #from lodgeit.i18n import list_languages as i18n_list_languages, _
 from lodgeit.models import Paste, db
 from lodgeit.lib.pagination import generate_pagination
 from lodgeit.lib.captcha import check_hashed_solution, Captcha
+from flask.ext.babel import gettext as _
 from flask import Blueprint
 pastes = Blueprint('pastes', __name__)
+@pastes.route('/', methods=['GET', 'POST'])
+@pastes.route('/+<language>', methods=['GET', 'POST'])
 def new_paste(language=None):
     """The 'create a new paste' view."""
     language = request.args.get('language', language)
                               'CAPTCHA solution was incorrect')
             show_captcha = True
         if code and language and not error:
-            paste = Paste(code, language, parent, req.user_hash,
-                          'private' in req.form)
+            paste = Paste(code, language, parent, session.user_hash,
+                          'private' in request.form)
             session['language'] = language
 def show_paste(identifier, raw=False):
     """Show an existing paste."""
-    linenos = local.request.args.get('linenos') != 'no'
+    linenos = request.args.get('linenos') != 'no'
     paste = Paste.get(identifier)
     if paste is None:
         raise NotFound()
     if raw:
         return Response(paste.code, mimetype='text/plain; charset=utf-8')
-    return render_to_response('show_paste.html',
+    return render_template('show_paste.html',
     paste = Paste.resolve_root(identifier)
     if paste is None:
         raise NotFound()
-    return render_to_response('paste_tree.html',
+    return render_template('paste_tree.html',
     """Paginated list of pages."""
     def link(page):
         if page == 1:
-            return local.request.get_url('pastes/show_all')
-        return local.request.get_url('pastes/show_all', page=page)
+            return request.get_url('pastes/show_all')
+        return request.get_url('pastes/show_all', page=page)
-    form_args = local.request.args
+    form_args = request.args
     query = Paste.find_all()
     pastes = query.limit(10).offset(10 * (page - 1)).all()
     if not pastes and page != 1:
         raise NotFound()
-    return render_to_response('show_all.html',
+    return render_template('show_all.html',
         pagination=generate_pagination(page, 10, query.count(), link),
         show_personal='show_personal' in form_args
 def compare_paste(new_id=None, old_id=None):
     """Render a diff view for two pastes."""
-    getform = local.request.form.get
+    getform = request.form.get
     # redirect for the compare form box
     if old_id is None:
         old_id = getform('old', '-1').lstrip('#')
         new_id = getform('new', '-1').lstrip('#')
-        return redirect(local.request.get_url('pastes/compare_paste', new_id=new_id, old_id=old_id))
+        return redirect(url_for('pastes.compare_paste', new_id=new_id, old_id=old_id))
-    old = Paste.get(old_id)
-    new = Paste.get(new_id)
-    if old is None or new is None:
-        raise NotFound()
+    old = Paste.query.get_or_404(old_id)
+    new = Paste.query.get_or_404(new_id)
-    return render_to_response('compare_paste.html',
+    return render_template('compare_paste.html',
         diff=old.compare_to(new, template=True)
 def unidiff_paste(new_id=None, old_id=None):
     """Render an udiff for the two pastes."""
-    old = Paste.get(old_id)
-    new = Paste.get(new_id)
-    if old is None or new is None:
-        raise NotFound()
-    return Response(old.compare_to(new), mimetype='text/plain')
+    old = Paste.query.get_or_404(old_id)
+    new = Paste.query.get_or_404(new_id)
+    return Response(old.compare_to(new),
+                    mimetype='text/plain')
 def set_colorscheme():
     """Minimal view that updates the style session cookie. Redirects
     back to the page the user is coming from.
-    style_name = local.request.form.get('style')
-    resp = redirect(local.request.environ.get('HTTP_REFERER') or local.request.get_url('pastes/new_paste'))
+    style_name = request.form.get('style')
+    resp = redirect(request.environ.get('HTTP_REFERER') or url_for('pastes.new_paste'))
     if style_name in STYLES:
         resp.set_cookie('style', style_name)
     return resp
     back to the page the user is coming from."""
     for key, value in i18n_list_languages():
         if key == lang:
-            local.request.set_language(lang)
+            request.set_language(lang)
-    return redirect(local.request.headers.get('HTTP_REFERER') or local.request.get_url('pastes/new_paste'))
+    return redirect(request.headers.get('HTTP_REFERER') or url_for('pastes.new_paste'))