From 5287dd6ec35475083c95e72ae3acdf41b5335a52 Mon Sep 17 00:00:00 2001 From: Gael Pasgrimaud Date: Sat, 15 Jan 2011 19:11:27 +0100 Subject: [PATCH] use exceptions.NotFound. move views declaration to view.zcml --- buildout.cfg | 5 +- pyramid_formalchemy/__init__.py | 20 ++++- pyramid_formalchemy/configure.zcml | 113 --------------------------- pyramid_formalchemy/resources.py | 4 +- pyramid_formalchemy/view.zcml | 118 +++++++++++++++++++++++++++++ pyramid_formalchemy/views.py | 20 ++--- pyramidapp/pyramidapp.db | Bin 0 -> 8192 bytes pyramidapp/pyramidapp/__init__.py | 4 +- 8 files changed, 152 insertions(+), 132 deletions(-) create mode 100644 pyramid_formalchemy/view.zcml create mode 100644 pyramidapp/pyramidapp.db diff --git a/buildout.cfg b/buildout.cfg index 86d7456..025a314 100644 --- a/buildout.cfg +++ b/buildout.cfg @@ -2,13 +2,14 @@ newest = false extensions = gp.vcsdevelop parts = eggs test -develop = . pyramidapp +develop = . pyramidapp ../fa.jquery [eggs] recipe = zc.recipe.egg eggs = pyramid_formalchemy pyramidapp + fa.jquery WebTest PasteScript Sphinx @@ -20,7 +21,7 @@ eggs = nose initialization = import os - os.chdir('pyramidapp') + os.chdir(os.path.join('${buildout:directory}', 'pyramidapp')) scripts= nosetests=nosetests paster=paster_serve diff --git a/pyramid_formalchemy/__init__.py b/pyramid_formalchemy/__init__.py index cf01fa5..443a59c 100644 --- a/pyramid_formalchemy/__init__.py +++ b/pyramid_formalchemy/__init__.py @@ -1,9 +1,18 @@ # -*- coding: utf-8 -*- def include(config): + """include formalchemy's zcml""" + config.load_zcml('pyramid.includes:configure.zcml') config.load_zcml('pyramid_formalchemy:configure.zcml') + config.load_zcml('pyramid_formalchemy:views.zcml') -def configure(config, models=None, forms=None, session_factory=None, package=None): +def include_jquery(config): + """include formalchemy's zcml""" + config.load_zcml('pyramid.includes:configure.zcml') + config.load_zcml('pyramid_formalchemy:configure.zcml') + config.load_zcml('fa.jquery:configure.zcml') + +def configure(config, models=None, forms=None, session_factory=None, package=None, use_jquery=True): """configure formalchemy's admin interface""" if models: models = config.maybe_dotted(models) @@ -26,6 +35,9 @@ def configure(config, models=None, forms=None, session_factory=None, package=Non 'fa.session_factory': session_factory, }) - config.add_route('fa_admin', '/admin/*traverse', - factory='pyramid_formalchemy.resources.AdminView') - + if use_jquery: + config.add_route('fa_admin', '/admin/*traverse', + factory='pyramid_formalchemy.resources.AdminView') + else: + config.add_route('fa_admin', '/admin/*traverse', + factory='pyramid_formalchemy.resources.AdminView') diff --git a/pyramid_formalchemy/configure.zcml b/pyramid_formalchemy/configure.zcml index 3547854..940a647 100644 --- a/pyramid_formalchemy/configure.zcml +++ b/pyramid_formalchemy/configure.zcml @@ -1,122 +1,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pyramid_formalchemy/resources.py b/pyramid_formalchemy/resources.py index 892d1e7..c1e9e5a 100644 --- a/pyramid_formalchemy/resources.py +++ b/pyramid_formalchemy/resources.py @@ -8,7 +8,7 @@ class AdminView(object): request.format = 'html' self.__parent__ = self.__name__ = None def __getitem__(self, item): - if item in ('json',): + if item in ('json', 'xhr'): self.request.format = item return self model = ModelListing(self.request, item) @@ -22,7 +22,7 @@ class ModelListing(object): self.__name__ = name self.__parent__ = None def __getitem__(self, item): - if item in ('json',): + if item in ('json', 'xhr'): self.request.format = item return self if item in ('new',): diff --git a/pyramid_formalchemy/view.zcml b/pyramid_formalchemy/view.zcml new file mode 100644 index 0000000..20e1093 --- /dev/null +++ b/pyramid_formalchemy/view.zcml @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pyramid_formalchemy/views.py b/pyramid_formalchemy/views.py index d06a7ff..5ce1da9 100644 --- a/pyramid_formalchemy/views.py +++ b/pyramid_formalchemy/views.py @@ -11,7 +11,9 @@ from formalchemy import fatypes from pyramid.view import view_config from pyramid.renderers import render from pyramid.renderers import get_renderer +from pyramid.response import Response from pyramid import httpexceptions as exc +from pyramid.exceptions import NotFound try: from formalchemy.ext.couchdb import Document @@ -99,7 +101,7 @@ class ModelView(object): return model elif hasattr(self.model, self.model_name): return getattr(self.model, self.model_name) - raise exc.HTTPNotFound(description='model %s not found' % self.model_name) + raise NotFound() def get_fieldset(self, id): if self.forms and hasattr(self.forms, self.model_name): @@ -166,7 +168,7 @@ class ModelView(object): if meth is not None: return meth(**kwargs) else: - return exc.HTTPNotfound() + raise NotFound() kwargs.update( main = get_renderer('pyramid_formalchemy:templates/admin/master.pt').implementation(), model_name=self.model_name, @@ -196,18 +198,18 @@ class ModelView(object): return data def render_xhr_format(self, fs=None, **kwargs): - response.content_type = 'text/html' + self.request.response_content_type = 'text/html' if fs is not None: - if 'field' in request.GET: - field_name = request.GET.get('field') + if 'field' in self.request.GET: + field_name = self.request.GET.get('field') fields = fs.render_fields if field_name in fields: field = fields[field_name] return field.render() else: - return exc.HTTPNotfound() - return fs.render() - return '' + raise NotFound() + return Response(fs.render()) + return Response('') def get_page(self, **kwargs): """return a ``webhelpers.paginate.Page`` used to display ``Grid``. @@ -252,7 +254,7 @@ class ModelView(object): model = S.query(model).get(id) if model: return model - raise exc.HTTPNotFound() + raise NotFound() def get_fieldset(self, id=None): """return a ``FieldSet`` object bound to the correct record for ``id``. diff --git a/pyramidapp/pyramidapp.db b/pyramidapp/pyramidapp.db new file mode 100644 index 0000000000000000000000000000000000000000..cbf4612e4f61ce1f7d57e4e4fd7d596b8871a078 GIT binary patch literal 8192 zcmeH~O=x3P6vy97-a99Gr|DOkrs?PFchYp)_nG&CqShg5u~r+~fdQdpY(^^WL_3Kn zB3KuqxNuQ)rQobYm|3_{X51-*D^Wp28D=2^f`Wnz8N^xm+Ri;xM%+0Fb1zBW?{@?5 z-2AzZbMyGh(q^-6o^I@H*P5oQBotLu?lVnAQMBlNbi^W2k4zlpBh$BH3U4TJ_*TKY z@yqBUhfF{wAQO-Y$OQhQ1UBNBO{dk}`DX3ZR{eCN5xunI)yl$Z#avyuf2k5RyoQ-K zlA9am;__9K#GI;7|A!et>V_bGQT-;62!bSK$TN zf_11u1wtsoFm!>YU)O)uuj*gvAL$?H@91ypujs9)j2tonnSe~-&Q5?_P06V{jYiWe z+vKo>7TGMpJXtKIIhtqjkI@{9HydS{x_LLZNVEKnGebodyFfE6R-Ot%-10Q#S;8rr zW(g)~ilsC`lPvx?O|W=lG|u9V(in?l(kP2PLMDqfOd~>^@(>NPgo8B15)9BFOR1j* zSo|FIvv_@!V{x<8$Kv!-mc`CcFN@Vf86kE#O+74OH>FvEF6w3}by63L-$9)$UORQL zxG8F9aoQ-wViUEoScXBGl0Dc5<)mQ<4iiRFaDYZaa0v_)?CS;y_B2Bm?8XgEuoE-l zf^F4^3AU7^swtU+x`6>hQUAZ9;C1{3uinP@CjVO2k