def tag_session(req): app = get_app() cookie_name = app.conf['sessions/cookie_name'] session = SecureCookie.load_cookie(req, cookie_name, app.conf['sessions/secret']) req.session = session local.session = session
def test_check_login(): from glashammer.bundles.auth import setup_auth, login from glashammer.bundles.sessions import get_session called = [] def check(u, p): called.append((u, p)) return u == p def view(req): login('a') return Response() def setup_app(app): app.add_setup(setup_auth) app.connect_event('password-check', check) app.add_url('/', 'a/b', view=view) app = make_app(setup_app, 'test_output') c = Client(app) c.open() session = get_session() token_key = get_app().conf['auth/token_key'] assert token_key in session assert session[token_key] == 'a'
def _date_format(formatter, obj, format): app = get_app() if app is None: locale = Locale('en') else: locale = app.locale return formatter(obj, format, locale=locale)
def get_engine(): """Get the SQLAlchemy DB Engine This returns the global SQLALchemy engine, which is *usually* shared amongst all threads. """ return get_app().sqla_db_engine
def ngettext(singular, plural, n): """Translate the possible pluralized string to the language of the application. """ app = get_app() if app is None: if n == 1: return singular return plural return app.translations.ungettext(singular, plural, n)
def cleanup_sessions(response): # save the session app = get_app() session = get_session() if session.should_save: cookie_name = app.conf['sessions/cookie_name'] if session.get('pmt'): max_age = 60 * 60 * 24 * 31 expires = time() + max_age else: max_age = expires = None session.save_cookie(response, cookie_name, max_age=max_age, expires=expires, session_expires=expires)
def parse_datetime(string): """Parses a string into a datetime object.""" if string.lower() == _('now'): return datetime.utcnow() convert = lambda fmt: datetime(*strptime(string, fmt)[:7]) cfg = get_app().cfg # first of all try the following format because a while ago it was # Textpress' default format try: return convert(u'%Y-%m-%d %H:%M') except ValueError: pass # no go with time only, and current day base = datetime.utcnow() for fmt in TIME_FORMATS: try: val = convert(fmt) except ValueError: continue return base.replace(hour=val.hour, minute=val.minute, second=val.second) # no try date + time def combined(): for t_fmt in TIME_FORMATS: for d_fmt in DATE_FORMATS: yield t_fmt + ' ' + d_fmt yield d_fmt + ' ' + t_fmt for fmt in combined(): try: return convert(fmt) except ValueError: pass raise ValueError('invalid date format')
def list_languages(): """Return a list of all languages.""" app = get_app() if app: locale = app.locale else: locale = Locale('en') languages = [('en', Locale('en').get_display_name(locale))] folder = os.path.dirname(__file__) for filename in os.listdir(folder): if filename == 'en' or not \ os.path.isdir(os.path.join(folder, filename)): continue try: l = Locale.parse(filename) except UnknownLocaleError: continue languages.append((str(l), l.get_display_name(locale))) languages.sort(key=lambda x: x[1].lower()) return languages
def get_cache(): return get_app().cache
def test_engine(self): assert self.app.sqla_db_engine is get_app().sqla_db_engine
def test_engine(self): assert get_app().sqla_db_engine
def _bview(req): app = get_app() assert_raises((RuntimeError,), app.add_config_var, 'a', str, 'a') return Response('hello')
def test_get_app(self): assert get_app() is self.app
def logout(): session = get_session() app = get_app() del session[app.conf['auth/token_key']]
def login(token): session = get_session() app = get_app() session[app.conf['auth/token_key']] = token
def get_username(): session = get_session() app = get_app() return session.get(app.conf['auth/token_key'])
def get_engine(): """Get the SQLA DB Engine""" return get_app().sqla_db_engine
def init_data(app): engine = get_app().sqla_db_engine metadata.create_all(engine)
def _generate_token(): return sha1("%s#%s" % (time(), get_app().cfg['sessions/secret'])).hexdigest()
def _open_conn(): db = get_app().zodb_db local.zodb_db = db local.zodb_conn = db.open()
def gettext(string): """Translate the given string to the language of the application.""" app = get_app() if app is None: return string return app.translations.ugettext(string)
def initialize_manager(*args): """ Ensure there is a thread local isntance of the stores manager. """ if not hasattr(local, 'storm_stores_manager'): local.storm_stores_manager = StoreManager(get_app().conf['stormdb/dburi'])
def wrapped(*args, **kw): if get_app().conf['auth/token_key'] in get_session(): return f(*args, **kw) else: return redirect(url_for('auth/login'))