initial upload
This commit is contained in:
78
cao_blogr/models/__init__.py
Normal file
78
cao_blogr/models/__init__.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.orm import configure_mappers
|
||||
import zope.sqlalchemy
|
||||
|
||||
# import or define all models here to ensure they are attached to the
|
||||
# Base.metadata prior to any initialization routines
|
||||
from .user import User
|
||||
from .blog_record import BlogRecord
|
||||
|
||||
# run configure_mappers after defining all of the models to ensure
|
||||
# all relationships can be setup
|
||||
configure_mappers()
|
||||
|
||||
|
||||
def get_engine(settings, prefix='sqlalchemy.'):
|
||||
return engine_from_config(settings, prefix)
|
||||
|
||||
|
||||
def get_session_factory(engine):
|
||||
factory = sessionmaker()
|
||||
factory.configure(bind=engine)
|
||||
return factory
|
||||
|
||||
|
||||
def get_tm_session(session_factory, transaction_manager):
|
||||
"""
|
||||
Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.
|
||||
|
||||
This function will hook the session to the transaction manager which
|
||||
will take care of committing any changes.
|
||||
|
||||
- When using pyramid_tm it will automatically be committed or aborted
|
||||
depending on whether an exception is raised.
|
||||
|
||||
- When using scripts you should wrap the session in a manager yourself.
|
||||
For example::
|
||||
|
||||
import transaction
|
||||
|
||||
engine = get_engine(settings)
|
||||
session_factory = get_session_factory(engine)
|
||||
with transaction.manager:
|
||||
dbsession = get_tm_session(session_factory, transaction.manager)
|
||||
|
||||
"""
|
||||
dbsession = session_factory()
|
||||
zope.sqlalchemy.register(
|
||||
dbsession, transaction_manager=transaction_manager)
|
||||
return dbsession
|
||||
|
||||
|
||||
def includeme(config):
|
||||
"""
|
||||
Initialize the model for a Pyramid app.
|
||||
|
||||
Activate this setup using ``config.include('cao_blogr.models')``.
|
||||
|
||||
"""
|
||||
settings = config.get_settings()
|
||||
settings['tm.manager_hook'] = 'pyramid_tm.explicit_manager'
|
||||
|
||||
# use pyramid_tm to hook the transaction lifecycle to the request
|
||||
config.include('pyramid_tm')
|
||||
|
||||
# use pyramid_retry to retry a request when transient exceptions occur
|
||||
config.include('pyramid_retry')
|
||||
|
||||
session_factory = get_session_factory(get_engine(settings))
|
||||
config.registry['dbsession_factory'] = session_factory
|
||||
|
||||
# make request.dbsession available for use in Pyramid
|
||||
config.add_request_method(
|
||||
# r.tm is the transaction manager used by pyramid_tm
|
||||
lambda r: get_tm_session(session_factory, r.tm),
|
||||
'dbsession',
|
||||
reify=True
|
||||
)
|
||||
33
cao_blogr/models/blog_record.py
Normal file
33
cao_blogr/models/blog_record.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import datetime #<- will be used to set default dates on models
|
||||
from cao_blogr.models.meta import Base #<- we need to import our sqlalchemy metadata from which model classes will inherit
|
||||
from sqlalchemy import (
|
||||
Column,
|
||||
Integer,
|
||||
Unicode, #<- will provide Unicode field
|
||||
UnicodeText, #<- will provide Unicode text field
|
||||
DateTime, #<- time abstraction field
|
||||
)
|
||||
from webhelpers2.text import urlify #<- will generate slugs
|
||||
from webhelpers2.date import distance_of_time_in_words #<- human friendly dates
|
||||
|
||||
|
||||
class BlogRecord(Base):
|
||||
__tablename__ = 'entries'
|
||||
id = Column(Integer, primary_key=True)
|
||||
title = Column(Unicode(255), unique=True, nullable=False)
|
||||
body = Column(UnicodeText, default=u'')
|
||||
body_html = Column(UnicodeText, default=u'')
|
||||
tag = Column(Unicode, default=u'pyramid')
|
||||
topic = Column(Unicode, default=u'blog')
|
||||
created = Column(DateTime, default=datetime.datetime.utcnow)
|
||||
edited = Column(DateTime, default=datetime.datetime.utcnow)
|
||||
|
||||
@property
|
||||
def slug(self):
|
||||
return urlify(self.title)
|
||||
|
||||
@property
|
||||
def created_in_words(self):
|
||||
return distance_of_time_in_words(self.created,
|
||||
datetime.datetime.utcnow())
|
||||
|
||||
16
cao_blogr/models/meta.py
Normal file
16
cao_blogr/models/meta.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.schema import MetaData
|
||||
|
||||
# Recommended naming convention used by Alembic, as various different database
|
||||
# providers will autogenerate vastly different names making migrations more
|
||||
# difficult. See: http://alembic.zzzcomputing.com/en/latest/naming.html
|
||||
NAMING_CONVENTION = {
|
||||
"ix": "ix_%(column_0_label)s",
|
||||
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
||||
"ck": "ck_%(table_name)s_%(constraint_name)s",
|
||||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
||||
"pk": "pk_%(table_name)s"
|
||||
}
|
||||
|
||||
metadata = MetaData(naming_convention=NAMING_CONVENTION)
|
||||
Base = declarative_base(metadata=metadata)
|
||||
34
cao_blogr/models/user.py
Normal file
34
cao_blogr/models/user.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import datetime #<- will be used to set default dates on models
|
||||
from cao_blogr.models.meta import Base #<- we need to import our sqlalchemy metadata from which model classes will inherit
|
||||
from sqlalchemy import (
|
||||
Column,
|
||||
Integer,
|
||||
Unicode, #<- will provide Unicode field
|
||||
UnicodeText, #<- will provide Unicode text field
|
||||
DateTime, #<- time abstraction field
|
||||
)
|
||||
|
||||
from passlib.apps import custom_app_context as blogger_pwd_context
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'users'
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(Unicode(255), unique=True, nullable=False)
|
||||
password = Column(Unicode(255), nullable=False)
|
||||
last_logged = Column(DateTime, default=datetime.datetime.utcnow)
|
||||
|
||||
def verify_password(self, password):
|
||||
# is it cleartext?
|
||||
if password == self.password:
|
||||
self.set_password(password)
|
||||
# verify password
|
||||
result = blogger_pwd_context.verify(password, self.password)
|
||||
if result:
|
||||
# pwd OK, set last login date
|
||||
self.last_logged = datetime.datetime.now()
|
||||
return result
|
||||
|
||||
def set_password(self, password):
|
||||
password_hash = blogger_pwd_context.encrypt(password)
|
||||
self.password = password_hash
|
||||
Reference in New Issue
Block a user