Commits

Ronny Pfannschmidt  committed 641373f

more context processors, kill dead code

  • Participants
  • Parent commits fb283ac
  • Branches flask-port

Comments (0)

Files changed (3)

File lodgeit/application.py

 from flask.ext.babel import Babel
 
 app = flask.Flask('lodgeit')
+babel = Babel(app)
+
 
 from .i18n import list_languages
-app.jinja_env.globals.update({'i18n_languages': list_languages()})
 
 
-babel = Babel(app)
+@app.context_processor
+def inject_i18n_languages():
+    return {'i18n_languages': list_languages()}
+
+
+@app.context_processor
+def inject_style():
+    style, css = get_style(request)
+    return {'style': style, 'css': css}
+
+
+@app.context_processor
+def inject_new_replies():
+    if flask.request.method=='GET':
+        from lodgeit.models import Paste
+        return {'new_replies': Paste.fetch_replies()}
+
 
 @app.errorhandler(404)
 def not_found(e):

File lodgeit/controllers/pastes.py

 
 @pastes.before_request
 def add_userhash():
+    """\
+    automatically creates a new
+    `user_hash` and sets `request.first_visit` to `True` if it's a new user.
+    """
     if 'user_hash' not in session:
+        request.first_visit = True
         session['user_hash'] = generate_user_hash()
+    else:
+        request.first_visit = False
 
 
 

File lodgeit/utils.py

 import time
 from os import path
 
-from werkzeug import Request as RequestBase, Response
-from werkzeug.contrib.securecookie import SecureCookie
-
-from jinja2 import Environment, FileSystemLoader
-
-from babel import Locale
-
-from lodgeit.lib.highlighting import get_style
-
-try:
-except:
-    from sha import new as sha1
-
-#: constants
-COOKIE_NAME = u'lodgeit_session'
-
-
-
-
+from werkzeug import Request as RequestBase, 
 
 class Request(RequestBase):
     """Subclass of the `Request` object. automatically creates a new
     charset = 'utf-8'
 
     def __init__(self, environ, urls):
-        super(Request, self).__init__(environ)
-        self.first_visit = False
-        session = SecureCookie.load_cookie(self, COOKIE_NAME,
-                                           local.application.secret_key)
-        user_hash = session.get('user_hash')
-
-        if not user_hash:
-            session['user_hash'] = generate_user_hash()
-            self.first_visit = True
-        self.user_hash = session['user_hash']
-        self.session = session
-
         # language is limited to english until translations are ready
         lang = session.get('locale')
         if lang is None:
             lang = 'en'
-            #lang = (self.accept_languages.best or 'en').split('-')[0]
-        self.locale = Locale.parse(lang)
-        self.urls = urls
 
     def set_language(self, lang):
         self.session['locale'] = lang
 
 
-
-def render_template(template_name, **context):
-    request = local.request
-    style, css = get_style(request)
-    context.update(
-        request=request,
-        gettext=request.translations.ugettext,
-        ngettext=request.translations.ungettext,
-        style = style,
-        css = css,
-    )
-    return jinja_environment.get_template(template_name).render(context)
-
-
-def render_to_response(template_name, **context):
-    """Render a template to a response. This automatically fetches
-    the list of new replies for the layout template. It also
-    adds the current request to the context. This is used for the
-    welcome message.
-    """
-    from lodgeit.models import Paste
-    request = local.request
-    if request.method == 'GET':
-        context['new_replies'] = Paste.fetch_replies()
-    return Response(render_template(template_name, **context),
-                    mimetype='text/html')