From 9d0f6461166ac73e6cc8e2247eece014353b1bfe Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 28 Dec 2007 15:38:51 +0000 Subject: [PATCH] created a requestmgr class to wrap up context initialization and pre-render cleanup git-svn-id: svn://svn.open-ils.org/ILS/branches/acq-experiment@8283 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/web/oilsweb/oilsweb/controllers/acq.py | 57 ++++++++++++----------- Open-ILS/web/oilsweb/oilsweb/controllers/admin.py | 30 ++++++------ Open-ILS/web/oilsweb/oilsweb/controllers/base.py | 5 +- Open-ILS/web/oilsweb/oilsweb/lib/request.py | 44 +++++++++++++++++ 4 files changed, 94 insertions(+), 42 deletions(-) create mode 100644 Open-ILS/web/oilsweb/oilsweb/lib/request.py diff --git a/Open-ILS/web/oilsweb/oilsweb/controllers/acq.py b/Open-ILS/web/oilsweb/oilsweb/controllers/acq.py index 64b0a5294f..39bb92267d 100644 --- a/Open-ILS/web/oilsweb/oilsweb/controllers/acq.py +++ b/Open-ILS/web/oilsweb/oilsweb/controllers/acq.py @@ -1,4 +1,5 @@ from oilsweb.lib.base import * +from oilsweb.lib.request import RequestMgr import logging, pylons import oilsweb.lib.context @@ -23,6 +24,9 @@ class AcqContext(SubContext): self.picklist_item = ContextItem(cgi_name='acq.pi', multi=True) self.extract_bib_field = ContextItem(default_value=oilsweb.lib.acq.search.extract_bib_field) self.prefix = ContextItem() + self.z39_sources = ContextItem() + self.search_classes = ContextItem() + self.search_classes_sorted = ContextItem() def postinit(self): self.prefix = "%s/acq" % Context.getContext().core.prefix @@ -33,33 +37,33 @@ Context.applySubContext('acq', AcqContext) class AcqController(BaseController): def index(self): - c.oils = oilsweb.lib.context.Context.init(request, response) - return render('oils/%s/acq/index.html' % c.oils.core.skin) + return RequestMgr().render('acq/index.html') def search(self): - c.oils = Context.init(request, response) - c.oils_z39_sources = oilsweb.lib.acq.search.fetch_z39_sources(c.oils) + r = RequestMgr() + r.ctx.acq.z39_sources = oilsweb.lib.acq.search.fetch_z39_sources(r.ctx) sc = {} - for data in c.oils_z39_sources.values(): + for data in r.ctx.acq.z39_sources.values(): for key, val in data['attrs'].iteritems(): sc[key] = val.get('label') or key - c.oils_search_classes = sc + r.ctx.acq.search_classes = sc keys = sc.keys() keys.sort() - c.oils_search_classes_sorted = keys + r.ctx.acq.search_classes_sorted = keys + log.debug("keys = %s" % unicode(r.ctx.acq.z39_sources)) - return render('oils/%s/acq/search.html' % c.oils.core.skin) + return r.render('acq/search.html') def pl_builder(self): - ctx = oilsweb.lib.context.Context.init(request, response) + r = RequestMgr() # add logic to see where we are fetching bib data from + # XXX fix + if r.ctx.acq.search_source: + c.oils_acq_records, r.ctx.acq.search_cache_key = self._build_z39_search(r.ctx) - if ctx.acq.search_source: - c.oils_acq_records, ctx.acq.search_cache_key = self._build_z39_search(ctx) - c.oils = ctx - return render('oils/%s/acq/pl_builder.html' % ctx.core.skin) + return r.render('acq/pl_builder.html') def _build_z39_search(self, ctx): @@ -85,34 +89,33 @@ class AcqController(BaseController): return oilsweb.lib.acq.search.multi_search(ctx, search) def rdetails(self): - c.oils = oilsweb.lib.context.Context.init(request, response) - rec_id = c.oils.acq.record_id - cache_key = c.oils.acq.search_cache_key + r = RequestMgr() + rec_id = r.ctx.acq.record_id + cache_key = r.ctx.acq.search_cache_key results = osrf.cache.CacheClient().get(cache_key) rec = self._find_cached_record(results, rec_id) if rec: - c.oils.acq.record = rec - #c.oils.acq.record_html = oilsweb.lib.bib.marc_to_html(rec['marcxml']) - return render('oils/%s/acq/rdetails.html' % c.oils.core.skin) + r.ctx.acq.record = rec + #r.ctx.acq.record_html = oilsweb.lib.bib.marc_to_html(rec['marcxml']) + return r.render('acq/rdetails.html') return 'exception -> no record' def create_picklist(self): - ctx = oilsweb.lib.context.Context.init(request, response) - if not isinstance(ctx.acq.picklist_item, list): - ctx.acq.picklist_item = [ctx.acq.picklist_item] + r = RequestMgr() + if not isinstance(r.ctx.acq.picklist_item, list): + r.ctx.acq.picklist_item = [r.ctx.acq.picklist_item] - results = osrf.cache.CacheClient().get(ctx.acq.search_cache_key) + results = osrf.cache.CacheClient().get(r.ctx.acq.search_cache_key) records = [] - for cache_id in ctx.acq.picklist_item: + for cache_id in r.ctx.acq.picklist_item: rec = self._find_cached_record(results, cache_id) records.append(rec) - c.oils_acq_records = records - c.oils = ctx - return render('oils/%s/acq/picklist.html' % c.oils.core.skin) + c.oils_acq_records = records # XXX + return r.render('acq/picklist.html') def _find_cached_record(self, results, cache_id): for res in results: diff --git a/Open-ILS/web/oilsweb/oilsweb/controllers/admin.py b/Open-ILS/web/oilsweb/oilsweb/controllers/admin.py index 10eb30a715..df8e6934b7 100644 --- a/Open-ILS/web/oilsweb/oilsweb/controllers/admin.py +++ b/Open-ILS/web/oilsweb/oilsweb/controllers/admin.py @@ -1,3 +1,4 @@ +from oilsweb.lib.request import RequestMgr from oilsweb.lib.base import * import oilsweb.lib.util from oilsweb.lib.context import Context, SubContext, ContextItem @@ -21,35 +22,38 @@ Context.applySubContext('adm', AdminContext) class AdminController(BaseController): def init(self, type, id=None): - c.oils = oilsweb.lib.context.Context.init(request, response) - c.oils.adm.object_class = type - meta = c.oils.adm.object_meta = oils.utils.idl.oilsGetIDLParser().IDLObject[type] + r = RequestMgr() + r.ctx.adm.object_class = type + meta = r.ctx.adm.object_meta = oils.utils.idl.oilsGetIDLParser().IDLObject[type] if id is not None: - c.oils.adm.object = osrf.ses.AtomicRequest( + r.ctx.adm.object = osrf.ses.AtomicRequest( 'open-ils.cstore', 'open-ils.cstore.direct.%s.retrieve' % meta['fieldmapper'].replace('::', '.'), id) + return r - c.oils.apply_cookies() + def test(self, type, id): + r = self.init() + return r.render('dashboard.html') def view(self, type, id): - self.init(type, id) - return render('oils/%s/admin/object.html' % c.oils.core.skin) + r = self.init(type, id) + return r.render('admin/object.html') def update(self, type, id): - self.init(type, id) + r = self.init(type, id) c.oils.adm.mode = 'update' - return render('oils/%s/admin/object.html' % c.oils.core.skin) + return r.render('admin/object.html') def create(self, type): - self.init(type) + r = self.init(type, id) c.oils.adm.mode = 'create' - return render('oils/%s/admin/object.html' % c.oils.core.skin) + return r.render('admin/object.html') def delete(self, type, id): - self.init(type, id) + r = self.init(type, id) c.oils.adm.mode = 'delete' - return render('oils/%s/admin/object.html' % c.oils.core.skin) # show a confirmation page + return r.render('admin/object.html') diff --git a/Open-ILS/web/oilsweb/oilsweb/controllers/base.py b/Open-ILS/web/oilsweb/oilsweb/controllers/base.py index 1bc2285278..a0f296b775 100644 --- a/Open-ILS/web/oilsweb/oilsweb/controllers/base.py +++ b/Open-ILS/web/oilsweb/oilsweb/controllers/base.py @@ -1,5 +1,6 @@ import logging +from oilsweb.lib.request import RequestMgr from oilsweb.lib.base import * from oilsweb.lib.context import Context, SubContext, ContextItem @@ -16,6 +17,6 @@ class BaseController(BaseController): ''' Controller for globally shared interfaces ''' def dashboard(self): - c.oils = Context.init(request, response) - return render('oils/%s/dashboard.html' % c.oils.core.skin) + r = RequestMgr() + return r.render('dashboard.html') diff --git a/Open-ILS/web/oilsweb/oilsweb/lib/request.py b/Open-ILS/web/oilsweb/oilsweb/lib/request.py new file mode 100644 index 0000000000..fe9f57c6b1 --- /dev/null +++ b/Open-ILS/web/oilsweb/oilsweb/lib/request.py @@ -0,0 +1,44 @@ +import pylons +from oilsweb.lib.base import * +import oilsweb.lib.context + +class RequestMgr(object): + ''' This is container class for aggregating the various Pylons global + variables, initializing the local and pylons context objects, and + rendering templates based on the skin + ''' + + def __init__(self): + # pylons request object + self.request = request + # pylons response object + self.response = response + # pylons session + self.session = session + # our local context object. + self.ctx = oilsweb.lib.context.Context.init(request, response) + # the global pylons context object + self.pylons_context = c + # true if we've saved the session/cookie data, etc. + self.finalized = False + + def finalize(self): + ''' Perform any last minute cleanup just prior to sending the result ''' + if not self.finalized: + self.session.save() + self.ctx.apply_cookies() + self.pylons_context.oils = self.ctx + self.finalized = True + + def render(self, tpath): + ''' Renders the given template using the configured skin. + @param tpath The path to the template. The tpath should + only include the path to the template after the skin component. + E.g. if the full path is /oils/myskin/base/dashboard.html, tpath + would be 'base/dashboard.html' + ''' + self.finalize() + return pylons.templating.render('oils/%s/%s' % (self.ctx.core.skin, tpath)) + + + -- 2.11.0