Skip to content

Commit bd88094

Browse files
committed
first commit
0 parents  commit bd88094

16 files changed

+450
-0
lines changed

alembic.ini

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = alembic
6+
7+
# template used to generate migration files
8+
# file_template = %%(rev)s_%%(slug)s
9+
10+
sqlalchemy.url = sqlite:///%(here)s/shirly.db
11+
12+
13+
# Logging configuration
14+
[loggers]
15+
keys = root,sqlalchemy,alembic
16+
17+
[handlers]
18+
keys = console
19+
20+
[formatters]
21+
keys = generic
22+
23+
[logger_root]
24+
level = WARN
25+
handlers = console
26+
qualname =
27+
28+
[logger_sqlalchemy]
29+
level = WARN
30+
handlers =
31+
qualname = sqlalchemy.engine
32+
33+
[logger_alembic]
34+
level = INFO
35+
handlers =
36+
qualname = alembic
37+
38+
[handler_console]
39+
class = StreamHandler
40+
args = (sys.stderr,)
41+
level = NOTSET
42+
formatter = generic
43+
44+
[formatter_generic]
45+
format = %(levelname)-5.5s [%(name)s] %(message)s
46+
datefmt = %H:%M:%S

alembic/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

alembic/env.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from __future__ import with_statement
2+
from alembic import context
3+
from sqlalchemy import engine_from_config, pool
4+
from logging.config import fileConfig
5+
6+
# this is the Alembic Config object, which provides
7+
# access to the values within the .ini file in use.
8+
config = context.config
9+
10+
# Interpret the config file for Python logging.
11+
# This line sets up loggers basically.
12+
fileConfig(config.config_file_name)
13+
14+
# add your model's MetaData object here
15+
# for 'autogenerate' support
16+
# from myapp import mymodel
17+
# target_metadata = mymodel.Base.metadata
18+
import shirly.models
19+
target_metadata = shirly.models.Base.metadata
20+
21+
# other values from the config, defined by the needs of env.py,
22+
# can be acquired:
23+
# my_important_option = config.get_main_option("my_important_option")
24+
# ... etc.
25+
26+
def run_migrations_offline():
27+
"""Run migrations in 'offline' mode.
28+
29+
This configures the context with just a URL
30+
and not an Engine, though an Engine is acceptable
31+
here as well. By skipping the Engine creation
32+
we don't even need a DBAPI to be available.
33+
34+
Calls to context.execute() here emit the given string to the
35+
script output.
36+
37+
"""
38+
url = config.get_main_option("sqlalchemy.url")
39+
context.configure(url=url)
40+
41+
with context.begin_transaction():
42+
context.run_migrations()
43+
44+
def run_migrations_online():
45+
"""Run migrations in 'online' mode.
46+
47+
In this scenario we need to create an Engine
48+
and associate a connection with the context.
49+
50+
"""
51+
engine = engine_from_config(
52+
config.get_section(config.config_ini_section),
53+
prefix='sqlalchemy.',
54+
poolclass=pool.NullPool)
55+
56+
connection = engine.connect()
57+
context.configure(
58+
connection=connection,
59+
target_metadata=target_metadata
60+
)
61+
62+
try:
63+
with context.begin_transaction():
64+
context.run_migrations()
65+
finally:
66+
connection.close()
67+
68+
if context.is_offline_mode():
69+
run_migrations_offline()
70+
else:
71+
run_migrations_online()
72+

alembic/script.py.mako

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision}
5+
Create Date: ${create_date}
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = ${repr(up_revision)}
11+
down_revision = ${repr(down_revision)}
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
${imports if imports else ""}
16+
17+
def upgrade():
18+
${upgrades if upgrades else "pass"}
19+
20+
21+
def downgrade():
22+
${downgrades if downgrades else "pass"}

alembic/versions/4d46b1bd74d7_.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""empty message
2+
3+
Revision ID: 4d46b1bd74d7
4+
Revises: None
5+
Create Date: 2012-04-13 08:14:39.706046
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = '4d46b1bd74d7'
11+
down_revision = None
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
16+
17+
def upgrade():
18+
### commands auto generated by Alembic - please adjust! ###
19+
op.create_table('users',
20+
sa.Column('id', sa.Integer(), nullable=False),
21+
sa.Column('user_name', sa.Unicode(length=255), nullable=True, unique=True),
22+
sa.Column('password', sa.String(length=255), nullable=True),
23+
sa.PrimaryKeyConstraint('id')
24+
)
25+
### end Alembic commands ###
26+
27+
28+
def downgrade():
29+
### commands auto generated by Alembic - please adjust! ###
30+
op.drop_table('users')
31+
### end Alembic commands ###

development.ini

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[app:main]
2+
paste.app_factory = shirly:main
3+
pyramid.debug_authentication = true
4+
pyramid.debug_authorization = true
5+
pyramid.reload_templates = true
6+
pyramid.includes =
7+
pyramid_tm
8+
shirly.security
9+
who.ini = %(here)s/who.ini
10+
sqlalchemy.url = sqlite:///%(here)s/shirly.db
11+
sqlalchemy.echo = true
12+
13+
[server:main]
14+
use = egg:waitress
15+
host = 0.0.0.0
16+
port = 6543
17+
18+
[loggers]
19+
keys=root,simpleExample
20+
21+
[handlers]
22+
keys=consoleHandler
23+
24+
[formatters]
25+
keys=simpleFormatter
26+
27+
[logger_root]
28+
level=DEBUG
29+
handlers=consoleHandler
30+
31+
[logger_simpleExample]
32+
level=DEBUG
33+
handlers=consoleHandler
34+
qualname=simpleExample
35+
propagate=0
36+
37+
[handler_consoleHandler]
38+
class=StreamHandler
39+
level=DEBUG
40+
formatter=simpleFormatter
41+
args=(sys.stdout,)
42+
43+
[formatter_simpleFormatter]
44+
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
45+
datefmt=

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[aliases]
2+
dev = develop easy_install shirly[testing] shirly[docs] shirly[dev]

setup.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from setuptools import setup, find_packages
2+
3+
requires = [
4+
"pyramid",
5+
"pyramid-tm",
6+
"pyramid-who",
7+
"pyramid-fanstatic",
8+
"pyramid-simpleform",
9+
"sqlahelper",
10+
"repoze.who",
11+
"repoze.who.plugins.sa",
12+
"waitress",
13+
"js.jquery",
14+
"js.jqueryui",
15+
"js.tinymce",
16+
]
17+
18+
tests_require = [
19+
"nose",
20+
"webtest",
21+
"coverage",
22+
"tox",
23+
"mock",
24+
]
25+
26+
points = {
27+
'console_scripts': [
28+
'add_user=shirly.scripts:add_user',
29+
],
30+
}
31+
32+
setup(
33+
name="shirly",
34+
install_requires=requires,
35+
tests_require=tests_require,
36+
extras_require={
37+
"testing": tests_require,
38+
"docs": ["sphinx"],
39+
"dev": ["alembic"],
40+
},
41+
test_suite="shirly",
42+
package_dir={"": "src"},
43+
entry_points=points,
44+
)

src/shirly/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
from pyramid.config import Configurator
3+
import sqlahelper
4+
from sqlalchemy import engine_from_config
5+
6+
def main(global_config, **settings):
7+
engine = engine_from_config(settings)
8+
sqlahelper.add_engine(engine)
9+
config = Configurator(settings=settings,
10+
root_factory='.models.ShirlyResource')
11+
config.add_route('top', '/')
12+
config.add_route('login', '/login')
13+
config.add_route('logout', '/logout')
14+
config.scan()
15+
return config.make_wsgi_app()

src/shirly/models.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import hashlib
2+
import sqlahelper
3+
import sqlalchemy as sa
4+
from pyramid.security import Everyone, Allow, Authenticated
5+
6+
class ShirlyResource(object):
7+
__acl__ = [
8+
(Allow, Authenticated, 'viewer'),
9+
]
10+
def __init__(self, request):
11+
self.request = request
12+
13+
14+
Base = sqlahelper.get_base()
15+
DBSession = sqlahelper.get_session()
16+
17+
class User(Base):
18+
__tablename__ = 'users'
19+
20+
id = sa.Column(sa.Integer, primary_key=True)
21+
user_name = sa.Column(sa.Unicode(255), unique=True)
22+
_password = sa.Column("password", sa.String(255))
23+
24+
def set_password(self, password):
25+
self._password = hashlib.sha1(password).hexdigest()
26+
27+
password = property(fset=set_password)
28+
29+
def validate_password(self, password):
30+
return self._password == hashlib.sha1(password).hexdigest()

src/shirly/scripts.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
from pyramid.paster import bootstrap
3+
import sqlahelper
4+
from . import models as m
5+
6+
def add_user():
7+
env = bootstrap(sys.argv[1])
8+
settings = env['registry'].settings
9+
10+
user_name = raw_input('user name: ')
11+
password = raw_input('password: ')
12+
user = m.User(user_name=user_name, password=password)
13+
m.DBSession.add(user)
14+
import transaction
15+
transaction.commit()

src/shirly/security.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import logging
2+
from pyramid.authorization import ACLAuthorizationPolicy
3+
from pyramid_who.whov2 import WhoV2AuthenticationPolicy
4+
from repoze.who.api import get_api as get_who_api
5+
from repoze.who.interfaces import IAPIFactory
6+
7+
def includeme(config):
8+
who_ini = config.registry.settings['who.ini']
9+
authorization_policy = ACLAuthorizationPolicy()
10+
authentication_policy = WhoV2AuthenticationPolicy(who_ini,
11+
identifier_id='auth_tkt')
12+
api_factory = authentication_policy._api_factory
13+
config.registry.registerUtility(api_factory, IAPIFactory)
14+
config.set_authorization_policy(authorization_policy)
15+
config.set_authentication_policy(authentication_policy)
16+
17+
config.add_tween('shirly.security.tween_factory')
18+
19+
config.set_request_property(authenticated_user, 'authenticated_user')
20+
21+
def tween_factory(handler, registry):
22+
api_factory = registry.getUtility(IAPIFactory)
23+
def who_api_tween(request):
24+
api_factory(request.environ)
25+
return handler(request)
26+
return who_api_tween
27+
28+
def authenticate(request):
29+
who_api = get_who_api(request.environ)
30+
identity, headers = who_api.login({'login': request.params['login'], 'password': request.params['password']})
31+
logging.debug('logged in %s' % identity)
32+
logging.debug('headers in %s' % headers)
33+
return identity.get('repoze.who.userid')
34+
35+
def authenticated_user(request):
36+
return request.environ.get('repoze.who.identity', {}).get('user')

src/shirly/templates/index.mak

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>Shirly</h1>
2+
${request.authenticated_user.user_name}
3+
<a href="${request.route_url('logout')}">Logout</a>

src/shirly/templates/login.mak

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Login</h1>
2+
<form method="POST" action="${request.route_url('login')}" >
3+
<input type="text" name="login" />
4+
<input type="password" name="password" />
5+
<input type="submit" value="Login" />
6+
</form>

0 commit comments

Comments
 (0)