~sirn/fanboi2

794cc9fa2b4e00343dbc9d9844f845970f3b2dae — Kridsada Thanabulpong 4 years ago 8988d5b
Remove column definition in BaseModel.

In order to make everything more explicit and to allow primary key for
the model to be customized, e.g. in case of one-to-one relationship.
M fanboi2/models/_base.py => fanboi2/models/_base.py +2 -13
@@ 2,9 2,7 @@ import json
import re
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.sql import func
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.sqltypes import DateTime, Integer, Text
from sqlalchemy.sql.sqltypes import Text
from sqlalchemy.sql.type_api import TypeDecorator
from zope.sqlalchemy import ZopeTransactionExtension
from ._versioned import make_versioned_class


@@ 31,16 29,7 @@ class JsonType(TypeDecorator):


class BaseModel(object):
    """Primary mixin that provides common fields for SQLAlchemy models."""

    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime(timezone=True), default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

    @declared_attr
    def __tablename__(self):
        name = RE_FIRST_CAP.sub(r'\1_\2', self.__name__)
        return RE_ALL_CAP.sub(r'\1_\2', name).lower()
    """Primary mixin that provides common behavior for SQLAlchemy models."""

    def __init__(self, **kwargs):
        for key, value in list(kwargs.items()):

M fanboi2/models/board.py => fanboi2/models/board.py +7 -1
@@ 1,7 1,8 @@
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import synonym
from sqlalchemy.sql import func
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.sqltypes import String, Text, Unicode
from sqlalchemy.sql.sqltypes import Integer, DateTime, String, Text, Unicode
from ._base import Base, JsonType, Versioned




@@ 19,6 20,11 @@ class Board(Versioned, Base):
    should always be accessed using :attr:`slug`.
    """

    __tablename__ = 'board'

    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime(timezone=True), default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    slug = Column(String(64), unique=True, nullable=False)
    title = Column(Unicode(255), nullable=False)
    _settings = Column('settings', JsonType, nullable=False, default={})

M fanboi2/models/post.py => fanboi2/models/post.py +5 -1
@@ 2,7 2,7 @@ from sqlalchemy import event
from sqlalchemy.orm import backref, relationship
from sqlalchemy.sql import func, select
from sqlalchemy.sql.schema import Column, ForeignKey, UniqueConstraint
from sqlalchemy.sql.sqltypes import Integer, String, Text, Boolean
from sqlalchemy.sql.sqltypes import Integer, DateTime, String, Text, Boolean
from ._base import Base, Versioned




@@ 12,8 12,12 @@ class Post(Versioned, Base):
    sequential number specifying its position within :class:`Topic`.
    """

    __tablename__ = 'post'
    __table_args__ = (UniqueConstraint('topic_id', 'number'),)

    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime(timezone=True), default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    topic_id = Column(Integer, ForeignKey('topic.id'), nullable=False)
    ip_address = Column(String, nullable=False)
    ident = Column(String(32), nullable=True)

M fanboi2/models/topic.py => fanboi2/models/topic.py +6 -1
@@ 3,7 3,7 @@ from sqlalchemy import event
from sqlalchemy.orm import backref, column_property, relationship
from sqlalchemy.sql import desc, func, select
from sqlalchemy.sql.schema import Column, ForeignKey
from sqlalchemy.sql.sqltypes import Integer, Enum, Unicode
from sqlalchemy.sql.sqltypes import Integer, DateTime, Enum, Unicode
from ._base import Base, DBSession, Versioned
from .post import Post



@@ 14,6 14,11 @@ class Topic(Versioned, Base):
    to :class:`Post`.
    """

    __tablename__ = 'topic'

    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime(timezone=True), default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    board_id = Column(Integer, ForeignKey('board.id'), nullable=False)
    title = Column(Unicode(255), nullable=False)
    status = Column(Enum('open', 'locked', 'archived', name='topic_status'),

M fanboi2/tests/test_models.py => fanboi2/tests/test_models.py +2 -4
@@ 131,6 131,8 @@ class TestBaseModel(ModelMixin, unittest.TestCase):
        MockBase = declarative_base()

        class MockModel(BaseModel, MockBase):
            __tablename__ = 'mock'
            id = Column(Integer, primary_key=True)
            y = Column(Integer)
            x = Column(Integer)
        return MockModel


@@ 141,10 143,6 @@ class TestBaseModel(ModelMixin, unittest.TestCase):
        self.assertEqual(mock.x, 1)
        self.assertEqual(mock.y, 2)

    def test_tablename(self):
        model_class = self._getTargetClass()
        self.assertEqual(model_class.__tablename__, 'mock_model')


class TestVersioned(unittest.TestCase):