Commits

Gael Pasgrimaud committed 850b428

add pyramid app based od pylons_sqla

Comments (0)

Files changed (22)

 import os, shutil, sys, tempfile, textwrap, urllib, urllib2, subprocess
 from optparse import OptionParser
 
+sys.path[0:0] = [
+    '/Users/gawel/py/FormAlchemy/parts/buildout',
+    '/Users/gawel/eggs/distribute-0.6.13-py2.6.egg',
+    '/Users/gawel/eggs/zc.buildout-1.5.2-py2.6.egg/',
+    ]
+
 if sys.platform == 'win32':
     def quote(c):
         if ' ' in c:
     os.environ,
     PYTHONPATH=setup_requirement_path)
 
-requirement = 'zc.buildout'
-version = options.version
-if version is None and not options.accept_buildout_test_releases:
-    # Figure out the most recent final version of zc.buildout.
-    import setuptools.package_index
-    _final_parts = '*final-', '*final'
-    def _final_version(parsed_version):
-        for part in parsed_version:
-            if (part[:1] == '*') and (part not in _final_parts):
-                return False
-        return True
-    index = setuptools.package_index.PackageIndex(
-        search_path=[setup_requirement_path])
-    if find_links:
-        index.add_find_links((find_links,))
-    req = pkg_resources.Requirement.parse(requirement)
-    if index.obtain(req) is not None:
-        best = []
-        bestv = None
-        for dist in index[req.project_name]:
-            distv = dist.parsed_version
-            if _final_version(distv):
-                if bestv is None or distv > bestv:
-                    best = [dist]
-                    bestv = distv
-                elif distv == bestv:
-                    best.append(dist)
-        if best:
-            best.sort()
-            version = best[-1].version
-if version:
-    requirement = '=='.join((requirement, version))
-cmd.append(requirement)
-
-if is_jython:
-    import subprocess
-    exitcode = subprocess.Popen(cmd, env=env).wait()
-else: # Windows prefers this, apparently; otherwise we would prefer subprocess
-    exitcode = os.spawnle(*([os.P_WAIT, sys.executable] + cmd + [env]))
-if exitcode != 0:
-    sys.stdout.flush()
-    sys.stderr.flush()
-    print ("An error occurred when trying to install zc.buildout. "
-           "Look above this message for any errors that "
-           "were output by easy_install.")
-    sys.exit(exitcode)
-
-ws.add_entry(eggs_dir)
-ws.require(requirement)
 import zc.buildout.buildout
 zc.buildout.buildout.main(args)
 if not options.eggs: # clean up temporary egg directory
 [buildout]
 newest=false
-parts = test pylonsapp pylons_test sphinx
+parts = test pylonsapp pylons_test pyramidapp pyramid_test sphinx
 develop = . pylonsapp
 versions = versions
 find-links=
 scripts =
     nosetests=test_pylons
 
+[pyramidapp]
+recipe = zc.recipe.egg
+eggs =
+    ${test:eggs}
+    PasteScript
+    Pyramid
+scripts =
+    paster=paster
+
+[pyramid_test]
+recipe = zc.recipe.egg
+initialization = import os; os.chdir('${buildout:directory}/pyramidapp/')
+eggs =
+    ${pyramidapp:eggs}
+scripts =
+    nosetests=test_pyramid
+
 [sphinx]
 recipe = zc.recipe.egg
 eggs=

formalchemy/ext/pyramid/__init__.py

 
 class ModelListing(object):
     def __init__(self, request, name):
+        self.request = request
         request.model_name = name
         self.__name__ = name
         self.__parent__ = None
         self.request = request
         self.settings = request.registry.settings
         self.model = self.settings['fa.models']
+        try:
+            self.id = request.model_id
+        except:
+            self.id = None
 
     @property
     def model_name(self):
             redirect(model_url(self.collection_name))
         return self.render(format=format, id=id)
 
-    @view_config(name='', request_method='GET', **VIEW_ARGS)
-    def show(self, id=None, format='html', **kwargs):
+    def show(self):
         """REST api"""
-        fs = self.get_fieldset(id=id)
+        fs = self.get_fieldset(id=self.id)
         fs.readonly = True
-        return self.render(format=format, fs=fs, action='show', id=id)
+        return self.render(fs=fs, action='show', id=id)
 
     @view_config(name='new', request_method='GET', **VIEW_ARGS)
     def new(self, format='html', **kwargs):

formalchemy/ext/pyramid/configure.zcml

     renderer="formalchemy:ext/pyramid/forms/listing.pt"
     />
 
+<view
+    name=""
+    route_name="fa_admin"
+    context=".ModelItem"
+    view=".ModelView"
+    attr="show"
+    request_method="GET"
+    renderer="formalchemy:ext/pyramid/forms/show.pt"
+    />
+
 </configure>

formalchemy/ext/pyramid/forms/edit.pt

+<html metal:use-macro="main.macros['master']">
+  <body>
+    <div metal:fill-slot="main">
+      <form action="" method="POST" enctype="multipart/form-data">
+      <div tal:content="structure fs.render()" />
+      <div metal:use-macro="main.macros['buttons']">
+      </div>
+    </div>
+  </body>
+</html>
+

formalchemy/ext/pyramid/forms/master.pt

         </div>
     </body>
 </html>
+<div metal:define-macro="buttons">
+  <p class="fa_field">
+    <a class="ui-widget-header ui-widget-link ui-widget-button ui-corner-all" href="#">
+      <input type="submit" value="${F_('Save')}" />
+    </a>
+    <a class="ui-widget-header ui-widget-link ui-corner-all" href="./..">
+      <span class="ui-icon ui-icon-circle-arrow-w"></span>
+      ${F_('Cancel')}
+    </a>
+  </p>
+</div>

formalchemy/ext/pyramid/forms/show.pt

+<html metal:use-macro="main.macros['master']">
+  <body>
+    <div metal:fill-slot="main">
+      <div tal:content="structure fs.render()" />
+      <div metal:use-macro="main.macros['buttons']">
+      </div>
+    </div>
+  </body>
+</html>
+

pyramidapp/CHANGES.txt

+0.0
+---
+
+-  Initial version

pyramidapp/README.txt

+pyramidapp README
+
+
+

pyramidapp/buildout.cfg

+[buildout]
+newest = false
+extensions = gp.vcsdevelop
+#vcs-extend-develop =
+find-links = http://www.gawel.org/docs/dist
+parts = eggs
+develop =
+  ../../FormAlchemy
+  ../pyramid
+  .
+
+[eggs]
+recipe = zc.recipe.egg
+eggs =
+    PasteScript
+    Sphinx
+    Pyramid
+    FormAlchemy
+    pyramidapp
+    nose

pyramidapp/development.ini

+[app:pyramidapp]
+use = egg:pyramidapp
+reload_templates = true
+mako.directories = pyramidapp:templates
+debug_authorization = false
+debug_notfound = false
+debug_templates = true
+default_locale_name = en
+db_string = sqlite:///%(here)s/tutorial.db
+db_echo = true
+session.type = file
+session.data_dir = %(here)s/data/sessions/data
+session.lock_dir = %(here)s/data/sessions/lock
+session.key = pyramidapp
+session.secret = c10f781f39fd805959a3fe420be0bddd63c56948
+
+[pipeline:main]
+pipeline =
+    egg:WebError#evalerror
+    egg:repoze.tm2#tm
+    pyramidapp
+
+[server:main]
+use = egg:Paste#http
+host = 0.0.0.0
+port = 6543

pyramidapp/pyramidapp/__init__.py

+from pyramid.configuration import Configurator
+from pyramid.settings import asbool
+
+#from pyramid_beaker import session_factory_from_settings
+
+def main(global_config, **settings):
+    """ This function returns a Pyramid WSGI application.
+    """
+    from pyramidapp.models import initialize_sql
+    db_string = settings.get('db_string')
+    if db_string is None:
+        raise ValueError("No 'db_string' value in application "
+                         "configuration.")
+    initialize_sql(db_string, asbool(settings.get('db_echo')))
+    config = Configurator(settings=settings)
+    config.begin()
+    #session_factory = session_factory_from_settings(settings)
+    #config.set_session_factory(session_factory)
+    config.add_static_view('static', 'pyramidapp:static/')
+    config.add_handler('main', '/:action', 'pyramidapp.handlers:MyHandler')
+    config.add_handler('home', '/', 'pyramidapp.handlers:MyHandler',
+                       action='index')
+    config.add_subscriber('pyramidapp.subscribers.add_renderer_globals',
+                          'pyramid.events.BeforeRender')
+    config.end()
+    return config.make_wsgi_app()

pyramidapp/pyramidapp/handlers.py

+from pyramid.view import action
+
+from pyramidapp.models import MyModel
+
+class MyHandler(object):
+    def __init__(self, request):
+        self.request = request
+
+    @action(renderer='mytemplate.mako')
+    def index(self):
+        root = MyModel.by_name('root')
+        return {'root':root, 'project':'pyramidapp'}

pyramidapp/pyramidapp/models.py

+import transaction
+
+from sqlalchemy import create_engine
+from sqlalchemy import Column
+from sqlalchemy import Integer
+from sqlalchemy import Unicode
+
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.ext.declarative import declarative_base
+
+from sqlalchemy.orm import scoped_session
+from sqlalchemy.orm import sessionmaker
+
+from zope.sqlalchemy import ZopeTransactionExtension
+
+DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
+Base = declarative_base()
+
+class MyModel(Base):
+    __tablename__ = 'models'
+    id = Column(Integer, primary_key=True)
+    name = Column(Unicode(255), unique=True)
+    value = Column(Integer)
+
+    def __init__(self, name, value):
+        self.name = name
+        self.value = value
+    
+    @classmethod
+    def by_name(cls, name=None):
+        return DBSession.query(cls).filter(cls.name == name).first()
+
+def populate():
+    model = MyModel(name=u'root', value=55)
+    DBSession.add(model)
+    DBSession.flush()
+    transaction.commit()
+    
+def initialize_sql(db_string, db_echo=False):
+    engine = create_engine(db_string, echo=db_echo)
+    DBSession.configure(bind=engine)
+    Base.metadata.bind = engine
+    Base.metadata.create_all(engine)
+    try:
+        populate()
+    except IntegrityError:
+        pass

pyramidapp/pyramidapp/static/favicon.ico

Added
New image

pyramidapp/pyramidapp/static/logo.png

Added
New image

pyramidapp/pyramidapp/static/pylons.css

+html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;/* 16px */
+vertical-align:baseline;background:transparent;}
+body{line-height:1;}
+ol,ul{list-style:none;}
+blockquote,q{quotes:none;}
+blockquote:before,blockquote:after,q:before,q:after{content:'';content:none;}
+/* remember to define focus styles! */
+:focus{outline:0;}
+/* remember to highlight inserts somehow! */
+ins{text-decoration:none;}
+del{text-decoration:line-through;}
+/* tables still need 'cellspacing="0"' in the markup */
+table{border-collapse:collapse;border-spacing:0;}
+/* restyling */
+sub{vertical-align:sub;font-size:smaller;line-height:normal;}
+sup{vertical-align:super;font-size:smaller;line-height:normal;}
+/* lists */
+ul,menu,dir{display:block;list-style-type:disc;margin:1em 0;padding-left:40px;}
+ol{display:block;list-style-type:decimal-leading-zero;margin:1em 0;padding-left:40px;}
+li{display:list-item;}
+/* nested lists have no top/bottom margins */
+ul ul,ul ol,ul dir,ul menu,ul dl,ol ul,ol ol,ol dir,ol menu,ol dl,dir ul,dir ol,dir dir,dir menu,dir dl,menu ul,menu ol,menu dir,menu menu,menu dl,dl ul,dl ol,dl dir,dl menu,dl dl{margin-top:0;margin-bottom:0;}
+/* 2 deep unordered lists use a circle */
+ol ul,ul ul,menu ul,dir ul,ol menu,ul menu,menu menu,dir menu,ol dir,ul dir,menu dir,dir dir{list-style-type:circle;}
+/* 3 deep (or more) unordered lists use a square */
+ol ol ul,ol ul ul,ol menu ul,ol dir ul,ol ol menu,ol ul menu,ol menu menu,ol dir menu,ol ol dir,ol ul dir,ol menu dir,ol dir dir,ul ol ul,ul ul ul,ul menu ul,ul dir ul,ul ol menu,ul ul menu,ul menu menu,ul dir menu,ul ol dir,ul ul dir,ul menu dir,ul dir dir,menu ol ul,menu ul ul,menu menu ul,menu dir ul,menu ol menu,menu ul menu,menu menu menu,menu dir menu,menu ol dir,menu ul dir,menu menu dir,menu dir dir,dir ol ul,dir ul ul,dir menu ul,dir dir ul,dir ol menu,dir ul menu,dir menu menu,dir dir menu,dir ol dir,dir ul dir,dir menu dir,dir dir dir{list-style-type:square;}
+.hidden{display:none;}
+p{line-height:1.5em;}
+h1{font-size:1.75em;/* 28px */
+line-height:1.7em;font-family:helvetica,verdana;}
+h2{font-size:1.5em;/* 24px */
+line-height:1.7em;font-family:helvetica,verdana;}
+h3{font-size:1.25em;/* 20px */
+line-height:1.7em;font-family:helvetica,verdana;}
+h4{font-size:1em;line-height:1.7em;font-family:helvetica,verdana;}
+html,body{width:100%;height:100%;}
+body{margin:0;padding:0;background-color:#ffffff;position:relative;font:16px/24px "Nobile","Lucida Grande",Lucida,Verdana,sans-serif;}
+a{color:#1b61d6;text-decoration:none;}
+a:hover{color:#e88f00;text-decoration:underline;}
+body h1,
+body h2,
+body h3,
+body h4,
+body h5,
+body h6{font-family:"Nobile","Lucida Grande",Lucida,Verdana,sans-serif;font-weight:normal;color:#144fb2;font-style:normal;}
+#wrap {min-height: 100%;}
+#header,#footer{width:100%;color:#ffffff;height:40px;position:absolute;text-align:center;line-height:40px;overflow:hidden;font-size:12px;}
+#header{background-color:#e88f00;top:0;font-size:14px;}
+#footer{background-color:#000000;bottom:0;position: relative;margin-top:-40px;clear:both;}
+.header,.footer{width:700px;margin-right:auto;margin-left:auto;}
+.wrapper{width:100%}
+#top,#bottom{width:100%;}
+#top{color:#888;background-color:#eee;height:300px;border-bottom:2px solid #ddd;}
+#bottom{color:#222;background-color:#ffffff;overflow:hidden;padding-bottom:80px;}
+.top,.bottom{width:700px;margin-right:auto;margin-left:auto;}
+.top{padding-top:100px;}
+.app-welcome{margin-top:25px;}
+.app-name{color:#000000;font-weight:bold;}
+.bottom{padding-top:50px;}
+#left{width:325px;float:left;padding-right:25px;}
+#right{width:325px;float:right;padding-left:25px;}
+.align-left{text-align:left;}
+.align-right{text-align:right;}
+.align-center{text-align:center;}
+ul.links{margin:0;padding:0;}
+ul.links li{list-style-type:none;font-size:14px;}
+form{border-style:none;}
+fieldset{border-style:none;}
+input{color:#222;border:1px solid #ccc;font-family:sans-serif;font-size:12px;line-height:16px;}
+input[type=text]{}
+input[type=submit]{background-color:#ddd;font-weight:bold;}
+/*Opera Fix*/
+body:before {content:"";height:100%;float:left;width:0;margin-top:-32767px;}

pyramidapp/pyramidapp/subscribers.py

+from pyramid.threadlocal import get_current_request
+from pyramid.exceptions import ConfigurationError
+from pyramid.url import route_url
+
+def add_renderer_globals(event):
+    """ A subscriber to the ``pyramid.events.BeforeRender`` events.  Updates
+    the :term:`renderer globals` with values that are familiar to Pylons
+    users."""
+    request = event.get('request')
+    if request is None:
+        request = get_current_request()
+    globs = {
+        'url': route_url,
+        'h':None,
+        }
+    if request is not None:
+        tmpl_context = request.tmpl_context
+        globs['c'] = tmpl_context
+        globs['tmpl_context'] = tmpl_context
+        try:
+            globs['session'] = request.session
+        except ConfigurationError:
+            pass
+    event.update(globs)
+
+

pyramidapp/pyramidapp/templates/mytemplate.mako

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal">
+<head>
+	<title>The Pyramid Web Application Development Framework</title>
+	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
+	<meta name="keywords" content="python web application" />
+	<meta name="description" content="pyramid web application" />
+	<link rel="shortcut icon" href="${request.application_url}/static/favicon.ico" />
+	<link rel="stylesheet" href="${request.application_url}/static/pylons.css" type="text/css" media="screen" charset="utf-8" />
+	<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Nobile:regular,italic,bold,bolditalic&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
+	<!--[if !IE 7]>
+	<style type="text/css">
+		#wrap {display:table;height:100%}
+	</style>
+	<![endif]-->
+</head>
+<body>
+	<div id="wrap">
+		<div id="header">
+			<div class="header">The Pyramid Web Application Development Framework</div>
+		</div>
+		<div id="top">
+			<div class="top align-center">
+				<img src="${request.application_url}/static/logo.png" width="300" height="80"/>
+				<p class="app-welcome">
+					Welcome to <span class="app-name">${project}</span>, an application generated by<br/>
+					the Pyramid web application development framework.
+				</p>
+			</div>
+		</div>
+		<div id="bottom">
+			<div class="bottom">
+				<div id="left" class="align-right">
+					<h3>Search Pyramid documentation</h3>
+					<form method="get" action="http://docs.pylonshq.com/pyramid/dev/search.html">
+		      			<input type="text" id="q" name="q" value="" />
+		      			<input type="submit" id="x" value="Search" />
+		  			</form>
+					<br/>
+					<p>The root object's name is "${root.name}"</p>
+				</div>
+				<div id="right" class="align-left">
+					<h3>Pyramid links</h3>
+					<ul class="links">
+						<li>
+							<a href="http://pylonshq.com">Pylons Website</a>
+						</li>
+						<li>
+							<a href="http://docs.pylonshq.com/">The Pylons Project Documentation</a>
+						</li>
+						<li>
+							<a href="http://docs.pylonshq.com/pyramid/dev/#narrative-documentation">Narrative Documentation</a>
+						</li>
+						<li>
+							<a href="http://docs.pylonshq.com/pyramid/dev/#api-documentation">API Documentation</a>
+						</li>
+						<li>
+							<a href="http://docs.pylonshq.com/pyramid/dev/#tutorials">Tutorials</a>
+						</li>
+						<li>
+							<a href="http://docs.pylonshq.com/pyramid/dev/#change-history">Change History</a>
+						</li>
+						<li>
+							<a href="http://docs.pylonshq.com/pyramid/dev/#sample-applications">Sample Applications</a>
+						</li>
+						<li>
+							<a href="http://docs.pylonshq.com/pyramid/dev/#support-and-development">Support and Development</a>
+						</li>
+						<li>
+							<a href="irc://irc.freenode.net#pyramid">IRC Channel</a>
+						</li>
+		  			</ul>
+				</div>
+			</div>
+		</div>
+	</div>
+	<div id="footer">
+		<div class="footer">© Copyright 2008-2010, Agendaless Consulting.</div>
+	</div>
+</body>
+</html>

pyramidapp/pyramidapp/tests.py

+import unittest
+
+class MyHandlerTests(unittest.TestCase):
+    def setUp(self):
+        from pyramid.configuration import Configurator
+        from pyramidapp.models import initialize_sql
+        self.session = initialize_sql('sqlite://')
+        self.config = Configurator()
+        self.config.begin()
+
+    def tearDown(self):
+        self.config.end()
+
+    def _makeOne(self, request):
+        from pyramidapp.handlers import MyHandler
+        return MyHandler(request)
+
+    def test_index(self):
+        request = DummyRequest()
+        handler = self._makeOne(request)
+        info = handler.index()
+        self.assertEqual(info['project'], 'pyramidapp')
+        self.assertEqual(info['root'].name, 'root')
+
+class DummyRequest(object):
+    pass

pyramidapp/setup.cfg

+[nosetests]
+match=^test
+nocapture=1
+cover-package=pyramidapp
+with-coverage=1
+cover-erase=1
+
+[compile_catalog]
+directory = pyramidapp/locale
+domain = pyramidapp
+statistics = true
+
+[extract_messages]
+add_comments = TRANSLATORS:
+output_file = pyramidapp/locale/pyramidapp.pot
+width = 80
+
+[init_catalog]
+domain = pyramidapp
+input_file = pyramidapp/locale/pyramidapp.pot
+output_dir = pyramidapp/locale
+
+[update_catalog]
+domain = pyramidapp
+input_file = pyramidapp/locale/pyramidapp.pot
+output_dir = pyramidapp/locale
+previous = true

pyramidapp/setup.py

+import os
+import sys
+
+from setuptools import setup, find_packages
+
+here = os.path.abspath(os.path.dirname(__file__))
+README = open(os.path.join(here, 'README.txt')).read()
+CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()
+
+requires = [
+    'pyramid',
+    #    'pyramid_beaker',
+    'SQLAlchemy',
+    'transaction',
+    'repoze.tm2',
+    'zope.sqlalchemy',
+    'WebError',
+]
+
+if sys.version_info[:3] < (2,5,0):
+    requires.append('pysqlite')
+    
+setup(name='pyramidapp',
+      version='0.0',
+      description='pyramidapp',
+      long_description=README + '\n\n' +  CHANGES,
+      classifiers=[
+        "Programming Language :: Python",
+        "Framework :: Pylons",
+        "Topic :: Internet :: WWW/HTTP",
+        "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
+        ],
+      author='',
+      author_email='',
+      url='',
+      keywords='web pyramid pylons',
+      packages=find_packages(),
+      include_package_data=True,
+      zip_safe=False,
+      install_requires=requires,
+      tests_require=requires,
+      test_suite="pyramidapp",
+      entry_points = """\
+      [paste.app_factory]
+      main = pyramidapp:main
+      """,
+      paster_plugins=['pyramid'],
+      )
+