lodgeit-main / lodgeit / utils.py

# -*- coding: utf-8 -*-
"""
    lodgeit.utils
    ~~~~~~~~~~~~~

    Serveral utilities used by LodgeIt.

    :copyright: 2007-2008 by Christopher Grebs.
    :license: BSD
"""
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'





class Request(RequestBase):
    """Subclass of the `Request` object. automatically creates a new
    `user_hash` and sets `first_visit` to `True` if it's a new user.
    It also stores the engine and dbsession on it.
    """
    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')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.