-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
santagada@gmail.com
committed
Jul 18, 2013
1 parent
94d2ca5
commit 68fdd0b
Showing
6 changed files
with
64 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters