Browse Source

Begin Pyramid Alchemy Project with cookiecutter

Guénaël Muller 6 years ago
commit
ed76d15014

+ 3 - 0
.coveragerc View File

@@ -0,0 +1,3 @@
1
+[run]
2
+source = tracim
3
+omit = tracim/test*

+ 65 - 0
.gitignore View File

@@ -0,0 +1,65 @@
1
+# Byte-compiled / optimized / DLL files
2
+__pycache__/
3
+*.py[cod]
4
+
5
+# C extensions
6
+*.so
7
+
8
+# Distribution / packaging
9
+.Python
10
+env/
11
+build/
12
+develop-eggs/
13
+eggs/
14
+#lib/
15
+lib64/
16
+parts/
17
+sdist/
18
+var/
19
+*.egg-info/
20
+.installed.cfg
21
+*.egg
22
+
23
+# Installer logs
24
+pip-log.txt
25
+pip-delete-this-directory.txt
26
+
27
+# Unit test / coverage reports
28
+htmlcov/
29
+.tox/
30
+.coverage
31
+.cache
32
+nosetests.xml
33
+coverage.xml
34
+.pytest_cache
35
+
36
+# Mr Developer
37
+.mr.developer.cfg
38
+.project
39
+.pydevproject
40
+
41
+# Rope
42
+.ropeproject
43
+
44
+# Django stuff:
45
+*.log
46
+*.pot
47
+
48
+# Sphinx documentation
49
+docs/_build/
50
+
51
+# Vim and dev tools
52
+*.swp
53
+.idea
54
+
55
+# Site-local config file
56
+development.ini
57
+track.js
58
+wsgidav.conf
59
+# Temporary files
60
+*~
61
+*.sqlite
62
+*.lock
63
+
64
+# binary translation files
65
+*.mo

+ 4 - 0
CHANGES.txt View File

@@ -0,0 +1,4 @@
1
+0.0
2
+---
3
+
4
+- Begin Pyramid Project for Tracim v2

+ 2 - 0
MANIFEST.in View File

@@ -0,0 +1,2 @@
1
+include *.txt *.ini *.cfg *.rst
2
+recursive-include tracim *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2

+ 33 - 0
README.txt View File

@@ -0,0 +1,33 @@
1
+tracim
2
+======
3
+
4
+Getting Started
5
+---------------
6
+
7
+- Change directory into your newly created project.
8
+
9
+    cd tracim
10
+
11
+- Create a Python virtual environment.
12
+
13
+    python3 -m venv env
14
+
15
+- Upgrade packaging tools.
16
+
17
+    env/bin/pip install --upgrade pip setuptools
18
+
19
+- Install the project in editable mode with its testing requirements.
20
+
21
+    env/bin/pip install -e ".[testing]"
22
+
23
+- Configure the database.
24
+
25
+    env/bin/initialize_tracim_db development.ini
26
+
27
+- Run your project's tests.
28
+
29
+    env/bin/pytest
30
+
31
+- Run your project.
32
+
33
+    env/bin/pserve development.ini

+ 65 - 0
production.ini View File

@@ -0,0 +1,65 @@
1
+###
2
+# app configuration
3
+# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
4
+###
5
+
6
+[app:main]
7
+use = egg:tracim
8
+
9
+pyramid.reload_templates = false
10
+pyramid.debug_authorization = false
11
+pyramid.debug_notfound = false
12
+pyramid.debug_routematch = false
13
+pyramid.default_locale_name = en
14
+
15
+sqlalchemy.url = sqlite:///%(here)s/tracim.sqlite
16
+
17
+retry.attempts = 3
18
+
19
+###
20
+# wsgi server configuration
21
+###
22
+
23
+[server:main]
24
+use = egg:waitress#main
25
+listen = *:6543
26
+
27
+###
28
+# logging configuration
29
+# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
30
+###
31
+
32
+[loggers]
33
+keys = root, tracim, sqlalchemy
34
+
35
+[handlers]
36
+keys = console
37
+
38
+[formatters]
39
+keys = generic
40
+
41
+[logger_root]
42
+level = WARN
43
+handlers = console
44
+
45
+[logger_tracim]
46
+level = WARN
47
+handlers =
48
+qualname = tracim
49
+
50
+[logger_sqlalchemy]
51
+level = WARN
52
+handlers =
53
+qualname = sqlalchemy.engine
54
+# "level = INFO" logs SQL queries.
55
+# "level = DEBUG" logs SQL queries and results.
56
+# "level = WARN" logs neither.  (Recommended for production systems.)
57
+
58
+[handler_console]
59
+class = StreamHandler
60
+args = (sys.stderr,)
61
+level = NOTSET
62
+formatter = generic
63
+
64
+[formatter_generic]
65
+format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s

+ 3 - 0
pytest.ini View File

@@ -0,0 +1,3 @@
1
+[pytest]
2
+testpaths = tracim
3
+python_files = *.py

+ 60 - 0
setup.py View File

@@ -0,0 +1,60 @@
1
+import os
2
+
3
+from setuptools import setup, find_packages
4
+
5
+here = os.path.abspath(os.path.dirname(__file__))
6
+with open(os.path.join(here, 'README.txt')) as f:
7
+    README = f.read()
8
+with open(os.path.join(here, 'CHANGES.txt')) as f:
9
+    CHANGES = f.read()
10
+
11
+requires = [
12
+    'plaster_pastedeploy',
13
+    'pyramid >= 1.9a',
14
+    'pyramid_debugtoolbar',
15
+    'pyramid_jinja2',
16
+    'pyramid_retry',
17
+    'pyramid_tm',
18
+    'SQLAlchemy',
19
+    'transaction',
20
+    'zope.sqlalchemy',
21
+    'waitress',
22
+]
23
+
24
+tests_require = [
25
+    'WebTest >= 1.3.1',  # py3 compat
26
+    'pytest',
27
+    'pytest-cov',
28
+]
29
+
30
+setup(
31
+    name='tracim',
32
+    version='0.0',
33
+    description='tracim',
34
+    long_description=README + '\n\n' + CHANGES,
35
+    classifiers=[
36
+        'Programming Language :: Python',
37
+        'Framework :: Pyramid',
38
+        'Topic :: Internet :: WWW/HTTP',
39
+        'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
40
+    ],
41
+    author='',
42
+    author_email='',
43
+    url='',
44
+    keywords='web pyramid pylons',
45
+    packages=find_packages(),
46
+    include_package_data=True,
47
+    zip_safe=False,
48
+    extras_require={
49
+        'testing': tests_require,
50
+    },
51
+    install_requires=requires,
52
+    entry_points={
53
+        'paste.app_factory': [
54
+            'main = tracim:main',
55
+        ],
56
+        'console_scripts': [
57
+            'initialize_tracim_db = tracim.scripts.initializedb:main',
58
+        ],
59
+    },
60
+)

+ 12 - 0
tracim/__init__.py View File

@@ -0,0 +1,12 @@
1
+from pyramid.config import Configurator
2
+
3
+
4
+def main(global_config, **settings):
5
+    """ This function returns a Pyramid WSGI application.
6
+    """
7
+    config = Configurator(settings=settings)
8
+    config.include('pyramid_jinja2')
9
+    config.include('.models')
10
+    config.include('.routes')
11
+    config.scan()
12
+    return config.make_wsgi_app()

+ 77 - 0
tracim/models/__init__.py View File

@@ -0,0 +1,77 @@
1
+from sqlalchemy import engine_from_config
2
+from sqlalchemy.orm import sessionmaker
3
+from sqlalchemy.orm import configure_mappers
4
+import zope.sqlalchemy
5
+
6
+# import or define all models here to ensure they are attached to the
7
+# Base.metadata prior to any initialization routines
8
+from .mymodel import MyModel  # flake8: noqa
9
+
10
+# run configure_mappers after defining all of the models to ensure
11
+# all relationships can be setup
12
+configure_mappers()
13
+
14
+
15
+def get_engine(settings, prefix='sqlalchemy.'):
16
+    return engine_from_config(settings, prefix)
17
+
18
+
19
+def get_session_factory(engine):
20
+    factory = sessionmaker()
21
+    factory.configure(bind=engine)
22
+    return factory
23
+
24
+
25
+def get_tm_session(session_factory, transaction_manager):
26
+    """
27
+    Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.
28
+
29
+    This function will hook the session to the transaction manager which
30
+    will take care of committing any changes.
31
+
32
+    - When using pyramid_tm it will automatically be committed or aborted
33
+      depending on whether an exception is raised.
34
+
35
+    - When using scripts you should wrap the session in a manager yourself.
36
+      For example::
37
+
38
+          import transaction
39
+
40
+          engine = get_engine(settings)
41
+          session_factory = get_session_factory(engine)
42
+          with transaction.manager:
43
+              dbsession = get_tm_session(session_factory, transaction.manager)
44
+
45
+    """
46
+    dbsession = session_factory()
47
+    zope.sqlalchemy.register(
48
+        dbsession, transaction_manager=transaction_manager)
49
+    return dbsession
50
+
51
+
52
+def includeme(config):
53
+    """
54
+    Initialize the model for a Pyramid app.
55
+
56
+    Activate this setup using ``config.include('tracim.models')``.
57
+
58
+    """
59
+    settings = config.get_settings()
60
+    settings['tm.manager_hook'] = 'pyramid_tm.explicit_manager'
61
+
62
+    # use pyramid_tm to hook the transaction lifecycle to the request
63
+    config.include('pyramid_tm')
64
+
65
+    # use pyramid_retry to retry a request when transient exceptions occur
66
+    config.include('pyramid_retry')
67
+
68
+    session_factory = get_session_factory(get_engine(settings))
69
+    config.registry['dbsession_factory'] = session_factory
70
+
71
+    # make request.dbsession available for use in Pyramid
72
+    config.add_request_method(
73
+        # r.tm is the transaction manager used by pyramid_tm
74
+        lambda r: get_tm_session(session_factory, r.tm),
75
+        'dbsession',
76
+        reify=True
77
+    )

+ 16 - 0
tracim/models/meta.py View File

@@ -0,0 +1,16 @@
1
+from sqlalchemy.ext.declarative import declarative_base
2
+from sqlalchemy.schema import MetaData
3
+
4
+# Recommended naming convention used by Alembic, as various different database
5
+# providers will autogenerate vastly different names making migrations more
6
+# difficult. See: http://alembic.zzzcomputing.com/en/latest/naming.html
7
+NAMING_CONVENTION = {
8
+    "ix": "ix_%(column_0_label)s",
9
+    "uq": "uq_%(table_name)s_%(column_0_name)s",
10
+    "ck": "ck_%(table_name)s_%(constraint_name)s",
11
+    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
12
+    "pk": "pk_%(table_name)s"
13
+}
14
+
15
+metadata = MetaData(naming_convention=NAMING_CONVENTION)
16
+Base = declarative_base(metadata=metadata)

+ 18 - 0
tracim/models/mymodel.py View File

@@ -0,0 +1,18 @@
1
+from sqlalchemy import (
2
+    Column,
3
+    Index,
4
+    Integer,
5
+    Text,
6
+)
7
+
8
+from .meta import Base
9
+
10
+
11
+class MyModel(Base):
12
+    __tablename__ = 'models'
13
+    id = Column(Integer, primary_key=True)
14
+    name = Column(Text)
15
+    value = Column(Integer)
16
+
17
+
18
+Index('my_index', MyModel.name, unique=True, mysql_length=255)

+ 3 - 0
tracim/routes.py View File

@@ -0,0 +1,3 @@
1
+def includeme(config):
2
+    config.add_static_view('static', 'static', cache_max_age=3600)
3
+    config.add_route('home', '/')

+ 1 - 0
tracim/scripts/__init__.py View File

@@ -0,0 +1 @@
1
+# package

+ 45 - 0
tracim/scripts/initializedb.py View File

@@ -0,0 +1,45 @@
1
+import os
2
+import sys
3
+import transaction
4
+
5
+from pyramid.paster import (
6
+    get_appsettings,
7
+    setup_logging,
8
+    )
9
+
10
+from pyramid.scripts.common import parse_vars
11
+
12
+from ..models.meta import Base
13
+from ..models import (
14
+    get_engine,
15
+    get_session_factory,
16
+    get_tm_session,
17
+    )
18
+from ..models import MyModel
19
+
20
+
21
+def usage(argv):
22
+    cmd = os.path.basename(argv[0])
23
+    print('usage: %s <config_uri> [var=value]\n'
24
+          '(example: "%s development.ini")' % (cmd, cmd))
25
+    sys.exit(1)
26
+
27
+
28
+def main(argv=sys.argv):
29
+    if len(argv) < 2:
30
+        usage(argv)
31
+    config_uri = argv[1]
32
+    options = parse_vars(argv[2:])
33
+    setup_logging(config_uri)
34
+    settings = get_appsettings(config_uri, options=options)
35
+
36
+    engine = get_engine(settings)
37
+    Base.metadata.create_all(engine)
38
+
39
+    session_factory = get_session_factory(engine)
40
+
41
+    with transaction.manager:
42
+        dbsession = get_tm_session(session_factory, transaction.manager)
43
+
44
+        model = MyModel(name='one', value=1)
45
+        dbsession.add(model)

BIN
tracim/static/pyramid-16x16.png View File


BIN
tracim/static/pyramid.png View File


+ 154 - 0
tracim/static/theme.css View File

@@ -0,0 +1,154 @@
1
+@import url(//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700);
2
+body {
3
+  font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
4
+  font-weight: 300;
5
+  color: #ffffff;
6
+  background: #bc2131;
7
+}
8
+h1,
9
+h2,
10
+h3,
11
+h4,
12
+h5,
13
+h6 {
14
+  font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
15
+  font-weight: 300;
16
+}
17
+p {
18
+  font-weight: 300;
19
+}
20
+.font-normal {
21
+  font-weight: 400;
22
+}
23
+.font-semi-bold {
24
+  font-weight: 600;
25
+}
26
+.font-bold {
27
+  font-weight: 700;
28
+}
29
+.starter-template {
30
+  margin-top: 250px;
31
+}
32
+.starter-template .content {
33
+  margin-left: 10px;
34
+}
35
+.starter-template .content h1 {
36
+  margin-top: 10px;
37
+  font-size: 60px;
38
+}
39
+.starter-template .content h1 .smaller {
40
+  font-size: 40px;
41
+  color: #f2b7bd;
42
+}
43
+.starter-template .content .lead {
44
+  font-size: 25px;
45
+  color: #f2b7bd;
46
+}
47
+.starter-template .content .lead .font-normal {
48
+  color: #ffffff;
49
+}
50
+.starter-template .links {
51
+  float: right;
52
+  right: 0;
53
+  margin-top: 125px;
54
+}
55
+.starter-template .links ul {
56
+  display: block;
57
+  padding: 0;
58
+  margin: 0;
59
+}
60
+.starter-template .links ul li {
61
+  list-style: none;
62
+  display: inline;
63
+  margin: 0 10px;
64
+}
65
+.starter-template .links ul li:first-child {
66
+  margin-left: 0;
67
+}
68
+.starter-template .links ul li:last-child {
69
+  margin-right: 0;
70
+}
71
+.starter-template .links ul li.current-version {
72
+  color: #f2b7bd;
73
+  font-weight: 400;
74
+}
75
+.starter-template .links ul li a, a {
76
+  color: #f2b7bd;
77
+  text-decoration: underline;
78
+}
79
+.starter-template .links ul li a:hover, a:hover {
80
+  color: #ffffff;
81
+  text-decoration: underline;
82
+}
83
+.starter-template .links ul li .icon-muted {
84
+  color: #eb8b95;
85
+  margin-right: 5px;
86
+}
87
+.starter-template .links ul li:hover .icon-muted {
88
+  color: #ffffff;
89
+}
90
+.starter-template .copyright {
91
+  margin-top: 10px;
92
+  font-size: 0.9em;
93
+  color: #f2b7bd;
94
+  text-transform: lowercase;
95
+  float: right;
96
+  right: 0;
97
+}
98
+@media (max-width: 1199px) {
99
+  .starter-template .content h1 {
100
+    font-size: 45px;
101
+  }
102
+  .starter-template .content h1 .smaller {
103
+    font-size: 30px;
104
+  }
105
+  .starter-template .content .lead {
106
+    font-size: 20px;
107
+  }
108
+}
109
+@media (max-width: 991px) {
110
+  .starter-template {
111
+    margin-top: 0;
112
+  }
113
+  .starter-template .logo {
114
+    margin: 40px auto;
115
+  }
116
+  .starter-template .content {
117
+    margin-left: 0;
118
+    text-align: center;
119
+  }
120
+  .starter-template .content h1 {
121
+    margin-bottom: 20px;
122
+  }
123
+  .starter-template .links {
124
+    float: none;
125
+    text-align: center;
126
+    margin-top: 60px;
127
+  }
128
+  .starter-template .copyright {
129
+    float: none;
130
+    text-align: center;
131
+  }
132
+}
133
+@media (max-width: 767px) {
134
+  .starter-template .content h1 .smaller {
135
+    font-size: 25px;
136
+    display: block;
137
+  }
138
+  .starter-template .content .lead {
139
+    font-size: 16px;
140
+  }
141
+  .starter-template .links {
142
+    margin-top: 40px;
143
+  }
144
+  .starter-template .links ul li {
145
+    display: block;
146
+    margin: 0;
147
+  }
148
+  .starter-template .links ul li .icon-muted {
149
+    display: none;
150
+  }
151
+  .starter-template .copyright {
152
+    margin-top: 20px;
153
+  }
154
+}

+ 8 - 0
tracim/templates/404.jinja2 View File

@@ -0,0 +1,8 @@
1
+{% extends "layout.jinja2" %}
2
+
3
+{% block content %}
4
+<div class="content">
5
+  <h1><span class="font-semi-bold">Pyramid</span> <span class="smaller">Alchemy scaffold</span></h1>
6
+  <p class="lead"><span class="font-semi-bold">404</span> Page Not Found</p>
7
+</div>
8
+{% endblock content %}

+ 64 - 0
tracim/templates/layout.jinja2 View File

@@ -0,0 +1,64 @@
1
+<!DOCTYPE html>
2
+<html lang="{{request.locale_name}}">
3
+  <head>
4
+    <meta charset="utf-8">
5
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+    <meta name="description" content="pyramid web application">
8
+    <meta name="author" content="Pylons Project">
9
+    <link rel="shortcut icon" href="{{request.static_url('tracim:static/pyramid-16x16.png')}}">
10
+
11
+    <title>Cookiecutter Alchemy project for the Pyramid Web Framework</title>
12
+
13
+    <!-- Bootstrap core CSS -->
14
+    <link href="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
15
+
16
+    <!-- Custom styles for this scaffold -->
17
+    <link href="{{request.static_url('tracim:static/theme.css')}}" rel="stylesheet">
18
+
19
+    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
20
+    <!--[if lt IE 9]>
21
+      <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js" integrity="sha384-0s5Pv64cNZJieYFkXYOTId2HMA2Lfb6q2nAcx2n0RTLUnCAoTTsS0nKEO27XyKcY" crossorigin="anonymous"></script>
22
+      <script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js" integrity="sha384-f1r2UzjsxZ9T4V1f2zBO/evUqSEOpeaUUZcMTz1Up63bl4ruYnFYeM+BxI4NhyI0" crossorigin="anonymous"></script>
23
+    <![endif]-->
24
+  </head>
25
+
26
+  <body>
27
+
28
+    <div class="starter-template">
29
+      <div class="container">
30
+        <div class="row">
31
+          <div class="col-md-2">
32
+            <img class="logo img-responsive" src="{{request.static_url('tracim:static/pyramid.png') }}" alt="pyramid web framework">
33
+          </div>
34
+          <div class="col-md-10">
35
+            {% block content %}
36
+                <p>No content</p>
37
+            {% endblock content %}
38
+          </div>
39
+        </div>
40
+        <div class="row">
41
+          <div class="links">
42
+            <ul>
43
+              <li><i class="glyphicon glyphicon-cog icon-muted"></i><a href="https://github.com/Pylons/pyramid">Github Project</a></li>
44
+              <li><i class="glyphicon glyphicon-globe icon-muted"></i><a href="https://webchat.freenode.net/?channels=pyramid">IRC Channel</a></li>
45
+              <li><i class="glyphicon glyphicon-home icon-muted"></i><a href="https://pylonsproject.org">Pylons Project</a></li>
46
+            </ul>
47
+          </div>
48
+        </div>
49
+        <div class="row">
50
+          <div class="copyright">
51
+            Copyright &copy; Pylons Project
52
+          </div>
53
+        </div>
54
+      </div>
55
+    </div>
56
+
57
+
58
+    <!-- Bootstrap core JavaScript
59
+    ================================================== -->
60
+    <!-- Placed at the end of the document so the pages load faster -->
61
+    <script src="//oss.maxcdn.com/libs/jquery/1.10.2/jquery.min.js" integrity="sha384-aBL3Lzi6c9LNDGvpHkZrrm3ZVsIwohDD7CDozL0pk8FwCrfmV7H9w8j3L7ikEv6h" crossorigin="anonymous"></script>
62
+    <script src="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js" integrity="sha384-s1ITto93iSMDxlp/79qhWHi+LsIi9Gx6yL+cOKDuymvihkfol83TYbLbOw+W/wv4" crossorigin="anonymous"></script>
63
+  </body>
64
+</html>

+ 8 - 0
tracim/templates/mytemplate.jinja2 View File

@@ -0,0 +1,8 @@
1
+{% extends "layout.jinja2" %}
2
+
3
+{% block content %}
4
+<div class="content">
5
+  <h1><span class="font-semi-bold">Pyramid</span> <span class="smaller">Alchemy project</span></h1>
6
+  <p class="lead">Welcome to <span class="font-normal">tracim</span>, a&nbsp;Pyramid application generated&nbsp;by<br><span class="font-normal">Cookiecutter</span>.</p>
7
+</div>
8
+{% endblock content %}

+ 65 - 0
tracim/tests.py View File

@@ -0,0 +1,65 @@
1
+import unittest
2
+import transaction
3
+
4
+from pyramid import testing
5
+
6
+
7
+def dummy_request(dbsession):
8
+    return testing.DummyRequest(dbsession=dbsession)
9
+
10
+
11
+class BaseTest(unittest.TestCase):
12
+    def setUp(self):
13
+        self.config = testing.setUp(settings={
14
+            'sqlalchemy.url': 'sqlite:///:memory:'
15
+        })
16
+        self.config.include('.models')
17
+        settings = self.config.get_settings()
18
+
19
+        from .models import (
20
+            get_engine,
21
+            get_session_factory,
22
+            get_tm_session,
23
+            )
24
+
25
+        self.engine = get_engine(settings)
26
+        session_factory = get_session_factory(self.engine)
27
+
28
+        self.session = get_tm_session(session_factory, transaction.manager)
29
+
30
+    def init_database(self):
31
+        from .models.meta import Base
32
+        Base.metadata.create_all(self.engine)
33
+
34
+    def tearDown(self):
35
+        from .models.meta import Base
36
+
37
+        testing.tearDown()
38
+        transaction.abort()
39
+        Base.metadata.drop_all(self.engine)
40
+
41
+
42
+class TestMyViewSuccessCondition(BaseTest):
43
+
44
+    def setUp(self):
45
+        super(TestMyViewSuccessCondition, self).setUp()
46
+        self.init_database()
47
+
48
+        from .models import MyModel
49
+
50
+        model = MyModel(name='one', value=55)
51
+        self.session.add(model)
52
+
53
+    def test_passing_view(self):
54
+        from .views.default import my_view
55
+        info = my_view(dummy_request(self.session))
56
+        self.assertEqual(info['one'].name, 'one')
57
+        self.assertEqual(info['project'], 'tracim')
58
+
59
+
60
+class TestMyViewFailureCondition(BaseTest):
61
+
62
+    def test_failing_view(self):
63
+        from .views.default import my_view
64
+        info = my_view(dummy_request(self.session))
65
+        self.assertEqual(info.status_int, 500)

+ 0 - 0
tracim/views/__init__.py View File


+ 33 - 0
tracim/views/default.py View File

@@ -0,0 +1,33 @@
1
+from pyramid.response import Response
2
+from pyramid.view import view_config
3
+
4
+from sqlalchemy.exc import DBAPIError
5
+
6
+from ..models import MyModel
7
+
8
+
9
+@view_config(route_name='home', renderer='../templates/mytemplate.jinja2')
10
+def my_view(request):
11
+    try:
12
+        query = request.dbsession.query(MyModel)
13
+        one = query.filter(MyModel.name == 'one').first()
14
+    except DBAPIError:
15
+        return Response(db_err_msg, content_type='text/plain', status=500)
16
+    return {'one': one, 'project': 'tracim'}
17
+
18
+
19
+db_err_msg = """\
20
+Pyramid is having a problem using your SQL database.  The problem
21
+might be caused by one of the following things:
22
+
23
+1.  You may need to run the "initialize_tracim_db" script
24
+    to initialize your database tables.  Check your virtual
25
+    environment's "bin" directory for this script and try to run it.
26
+
27
+2.  Your database server may not be running.  Check that the
28
+    database server referred to by the "sqlalchemy.url" setting in
29
+    your "development.ini" file is running.
30
+
31
+After you fix the problem, please restart the Pyramid application to
32
+try it again.
33
+"""

+ 7 - 0
tracim/views/notfound.py View File

@@ -0,0 +1,7 @@
1
+from pyramid.view import notfound_view_config
2
+
3
+
4
+@notfound_view_config(renderer='../templates/404.jinja2')
5
+def notfound_view(request):
6
+    request.response.status = 404
7
+    return {}