Skip to content

Commit

Permalink
using horus for registration
Browse files Browse the repository at this point in the history
  • Loading branch information
santagada@gmail.com committed Jul 18, 2013
1 parent 94d2ca5 commit 68fdd0b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 24 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ WebOb==1.2.3
zope.deprecation==4.0.2
zope.interface==4.0.5
zope.sqlalchemy==0.7.2
horus==0.9.13
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def run_tests(self):
'pyramid_debugtoolbar',
'zope.sqlalchemy',
'waitress',
'horus',
]

setup(name='timtec',
Expand Down
6 changes: 3 additions & 3 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def setup():
engine = create_engine('sqlite://')
from timtec.models import (
Base,
MyModel,
User
)
DBSession.configure(bind=engine)
Base.metadata.create_all(engine)
with transaction.manager:
model = MyModel(name='one', value=55)
model = User(username='one', password='kdkdk', email='skdsk', id='kkk', pk='kk')
DBSession.add(model)


Expand All @@ -32,5 +32,5 @@ def test_it():
from timtec.views import my_view
request = testing.DummyRequest()
info = my_view(request)
assert info['one'].name == 'one'
assert info['one'].username == 'one'
assert info['project'] == 'timtec'
7 changes: 5 additions & 2 deletions timtec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from .models import (
DBSession,
Base,
)

)
from . import models

def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
Expand All @@ -17,4 +17,7 @@ def main(global_config, **settings):
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.scan()
config.include('horus')
config.include('pyramid_mailer')
config.scan_horus(models)
return config.make_wsgi_app()
69 changes: 52 additions & 17 deletions timtec/models.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
from sqlalchemy import (
Column,
Integer,
Text,
import re
import sqlalchemy as sa
from sqlalchemy.ext.declarative import (
declarative_base,
declared_attr,
)

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import (
scoped_session,
sessionmaker,
)

from zope.sqlalchemy import ZopeTransactionExtension
from horus.models import (
GroupMixin,
UserMixin,
UserGroupMixin,
ActivationMixin,
)

class BaseModel(object):
"""Base class which auto-generates tablename, and surrogate
primary key column.
"""
__table_args__ = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}

@declared_attr
def pk(self):
# We use pk instead of id because id is a python builtin
return sa.Column(sa.Integer, primary_key=True)

_traversal_lookup_key = 'pk'

@declared_attr
def __tablename__(cls):
"""Convert CamelCase class name to underscores_between_words
table name."""
name = cls.__name__.replace('Mixin', '')

return (
name[0].lower() +
re.sub(r'([A-Z])', lambda m: "_" + m.group(0).lower(), name[1:])
)


DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
Base = declarative_base(cls=BaseModel)

class User(UserMixin, Base):
pass


class Group(GroupMixin, Base):
pass


class UserGroup(UserGroupMixin, Base):
pass

class MyModel(Base):
__tablename__ = 'models'
id = Column(Integer, primary_key=True)
name = Column(Text, unique=True)
value = Column(Integer)

def __init__(self, name, value):
self.name = name
self.value = value
class Activation(ActivationMixin, Base):
pass
4 changes: 2 additions & 2 deletions timtec/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

from .models import (
DBSession,
MyModel,
User,
)


@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
try:
one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()
one = DBSession.query(User).filter(User.username == 'one').first()
except DBAPIError:
return Response(conn_err_msg, content_type='text/plain', status_int=500)
return {'one': one, 'project': 'timtec'}
Expand Down

0 comments on commit 68fdd0b

Please sign in to comment.