#!/usr/bin/env python
# -*- coding: utf-8 -*-

from notmm.dbapi.orm import decorators
from notmm.utils.configparse import loadconf
from notmm.utils.http.decorators import post_required
from blogengine.template import direct_to_template
from moviereviews import configuration as c
settings = c.settings
with_session = c..with_session
with_schevo_database = decorators.with_schevo_database

##
# Legacy SQLAlchemy decorator
# Provides experimental scoped_session support for 
# Django views
@with_session(engine=c.engine)
def test_session(request, **kwds):

    template_name = kwds['template_name']
    # app._scoped_session
    assert request.environ['_scoped_session'] != None

    return direct_to_template(request, template_name)

@post_required
@with_schevo_database(settings)
def test_schevo_database(request, **kwds):
    assert request.method is 'POST', request.method
    assert 'schevo.db.moviereviews' in request.environ.keys()
    db = request.environ['schevo.db.moviereviews']
    assert db.format == 2, 'invalid db format: %d' % db.format
    assert hasattr(db, 'Q'), 'db has no Q attribute!'
    #Q = db.Q
    #movies = (item for item in db.Movie.find())
    #db.close()
    return direct_to_template(request, template_name='sandbox/test.mako')
    
