Skip to content

Commit 7d744de

Browse files
committed
starter code for "using sqlalchemy" chapter.
1 parent 6f40de0 commit 7d744de

File tree

109 files changed

+3495
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+3495
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[run]
2+
source = pypi
3+
omit = pypi/test*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
*.egg
2+
*.egg-info
3+
*.pyc
4+
*$py.class
5+
*~
6+
.coverage
7+
coverage.xml
8+
build/
9+
dist/
10+
.tox/
11+
nosetests.xml
12+
env*/
13+
tmp/
14+
Data.fs*
15+
*.sublime-project
16+
*.sublime-workspace
17+
.*.sw?
18+
.sw?
19+
.DS_Store
20+
coverage
21+
test

src/ch10-using-sqlachemy/final/pypi_using_sqlachemy/.idea/inspectionProfiles/Project_Default.xml

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ch10-using-sqlachemy/final/pypi_using_sqlachemy/.idea/misc.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ch10-using-sqlachemy/final/pypi_using_sqlachemy/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ch10-using-sqlachemy/final/pypi_using_sqlachemy/.idea/pypi_using_sqlachemy.iml

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ch10-using-sqlachemy/final/pypi_using_sqlachemy/.idea/webResources.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0.0
2+
---
3+
4+
- Initial version.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include *.txt *.ini *.cfg *.rst
2+
recursive-include pypi *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Python Package Index
2+
====================
3+
4+
Getting Started
5+
---------------
6+
7+
- Change directory into your newly created project.
8+
9+
cd pypi
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+
- Run your project's tests.
24+
25+
env/bin/pytest
26+
27+
- Run your project.
28+
29+
env/bin/pserve development.ini
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
###
2+
# app configuration
3+
# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
4+
###
5+
6+
[app:main]
7+
use = egg:pypi
8+
9+
pyramid.reload_templates = true
10+
pyramid.debug_authorization = false
11+
pyramid.debug_notfound = false
12+
pyramid.debug_routematch = false
13+
pyramid.default_locale_name = en
14+
pyramid.includes =
15+
pyramid_debugtoolbar
16+
17+
# By default, the toolbar only appears for clients from IP addresses
18+
# '127.0.0.1' and '::1'.
19+
# debugtoolbar.hosts = 127.0.0.1 ::1
20+
21+
###
22+
# wsgi server configuration
23+
###
24+
25+
[server:main]
26+
use = egg:waitress#main
27+
listen = localhost:6543
28+
29+
###
30+
# logging configuration
31+
# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
32+
###
33+
34+
[loggers]
35+
keys = root, pypi
36+
37+
[handlers]
38+
keys = console
39+
40+
[formatters]
41+
keys = generic
42+
43+
[logger_root]
44+
level = INFO
45+
handlers = console
46+
47+
[logger_pypi]
48+
level = DEBUG
49+
handlers =
50+
qualname = pypi
51+
52+
[handler_console]
53+
class = StreamHandler
54+
args = (sys.stderr,)
55+
level = NOTSET
56+
formatter = generic
57+
58+
[formatter_generic]
59+
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
###
2+
# app configuration
3+
# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
4+
###
5+
6+
[app:main]
7+
use = egg:pypi
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+
###
16+
# wsgi server configuration
17+
###
18+
19+
[server:main]
20+
use = egg:waitress#main
21+
listen = *:6543
22+
23+
###
24+
# logging configuration
25+
# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
26+
###
27+
28+
[loggers]
29+
keys = root, pypi
30+
31+
[handlers]
32+
keys = console
33+
34+
[formatters]
35+
keys = generic
36+
37+
[logger_root]
38+
level = WARN
39+
handlers = console
40+
41+
[logger_pypi]
42+
level = WARN
43+
handlers =
44+
qualname = pypi
45+
46+
[handler_console]
47+
class = StreamHandler
48+
args = (sys.stderr,)
49+
level = NOTSET
50+
formatter = generic
51+
52+
[formatter_generic]
53+
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import os
2+
3+
from pyramid.config import Configurator
4+
5+
from pypi.data.db_session import DbSession
6+
7+
8+
def main(global_config, **settings):
9+
""" This function returns a Pyramid WSGI application.
10+
"""
11+
config = Configurator(settings=settings)
12+
init_includes(config)
13+
init_db(config)
14+
init_routing(config)
15+
16+
return config.make_wsgi_app()
17+
18+
19+
def init_includes(config):
20+
config.include('pyramid_chameleon')
21+
22+
23+
def init_routing(config):
24+
config.add_static_view('static', 'static', cache_max_age=3600)
25+
26+
# home controller
27+
config.add_route('home', '/')
28+
config.add_route('about', '/about')
29+
30+
# package controller
31+
config.add_route('popular', '/{num}', custom_predicates=[
32+
lambda info, _: info['match'].get('num', '').isdigit()
33+
])
34+
35+
config.add_route('package_details', '/project/{package_name}')
36+
config.add_route('package_details/', '/project/{package_name}/')
37+
38+
config.add_route('releases', '/project/{package_name}/releases')
39+
config.add_route('releases/', '/project/{package_name}/releases/')
40+
41+
config.add_route('release_version', '/project/{package_name}/releases/{release_version}')
42+
43+
# account controller
44+
config.add_route('account_home', '/account')
45+
config.add_route('login', '/account/login')
46+
config.add_route('register', '/account/register')
47+
config.add_route('logout', '/account/logout')
48+
49+
# cms controller -- VERY END
50+
config.add_route('cms_page', '*subpath')
51+
52+
config.scan()
53+
54+
55+
def init_db(_):
56+
db_file = os.path.abspath(
57+
os.path.join(
58+
os.path.dirname(__file__),
59+
'db',
60+
'pypi.sqlite'
61+
))
62+
DbSession.global_init(db_file)

src/ch10-using-sqlachemy/final/pypi_using_sqlachemy/pypi/controllers/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from pyramid.view import view_config
2+
3+
4+
# ################### INDEX #################################
5+
6+
7+
@view_config(route_name='account_home',
8+
renderer='pypi:templates/account/index.pt')
9+
def index(request):
10+
return {}
11+
12+
13+
# ################### LOGIN #################################
14+
15+
@view_config(route_name='login',
16+
renderer='pypi:templates/account/login.pt',
17+
request_method='GET')
18+
def login_get(request):
19+
return {}
20+
21+
22+
@view_config(route_name='login',
23+
renderer='pypi:templates/account/login.pt',
24+
request_method='POST')
25+
def login_post(request):
26+
return {}
27+
28+
29+
# ################### REGISTRATION ############################
30+
31+
@view_config(route_name='register',
32+
renderer='pypi:templates/account/register.pt',
33+
request_method='GET')
34+
def register_get(request):
35+
return {}
36+
37+
38+
@view_config(route_name='register',
39+
renderer='pypi:templates/account/register.pt',
40+
request_method='POST')
41+
def register_post(request):
42+
return {}
43+
44+
45+
# ################### LOGOUT ############################
46+
47+
@view_config(route_name='logout')
48+
def logout(request):
49+
return {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pyramid.httpexceptions import HTTPNotFound
2+
from pyramid.request import Request
3+
from pyramid.view import view_config
4+
5+
fake_db = {
6+
'company/history': {
7+
'page_title': 'Company history',
8+
'page_details': 'Details about company history...',
9+
},
10+
'company/employees': {
11+
'page_title': 'Our team',
12+
'page_details': 'Details about company employees ...',
13+
},
14+
}
15+
16+
17+
@view_config(route_name='cms_page', renderer='pypi:templates/cms/page.pt')
18+
def cms_page(request: Request):
19+
subpath = request.matchdict.get('subpath')
20+
suburl = '/'.join(subpath)
21+
22+
page = fake_db.get(suburl)
23+
if not page:
24+
raise HTTPNotFound()
25+
26+
return page

0 commit comments

Comments
 (0)