Commit 97397d5f authored by Francois Blackburn's avatar Francois Blackburn

sql: use IntAsString helper in Extension

Move this function into helper library
parent e4f6ab54
......@@ -17,32 +17,13 @@
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.schema import Column, PrimaryKeyConstraint, Index
from sqlalchemy.types import Integer, String, TypeDecorator
from sqlalchemy.types import Integer, String
from sqlalchemy.sql import func
from xivo_dao.helpers.db_manager import Base
from xivo_dao.helpers.db_manager import Base, IntAsString
from xivo_dao.alchemy import enum
# Used for Incall table
# http://docs.sqlalchemy.org/en/rel_0_9/_modules/examples/join_conditions/cast.html
class IntAsString(TypeDecorator):
"""Coerce integer->string type.
This is needed only if the relationship() from
string to int is writable, as SQLAlchemy will copy
the int parent values into the string attribute
on the child during a flush.
"""
impl = String
def process_bind_param(self, value, dialect):
if value is not None:
value = str(value)
return value
class Dialaction(Base):
USER_EVENTS = ('noanswer',
......
......@@ -21,7 +21,7 @@ from sqlalchemy.types import Integer, String, Boolean
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.sql import cast
from xivo_dao.helpers.db_manager import Base
from xivo_dao.helpers.db_manager import Base, IntAsString
from xivo_dao.alchemy import enum
......@@ -42,7 +42,7 @@ class Extension(Base):
context = Column(String(39), nullable=False, server_default='')
exten = Column(String(40), nullable=False, server_default='')
type = Column(enum.extenumbers_type, nullable=False)
typeval = Column(String(255), nullable=False, server_default='')
typeval = Column(IntAsString(255), nullable=False, server_default='')
@property
def name(self):
......
......@@ -22,6 +22,7 @@ from sqlalchemy import create_engine
from sqlalchemy.pool import Pool
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import String, TypeDecorator
from xivo.config_helper import ConfigParser, ErrorHandler
......@@ -33,6 +34,24 @@ Session = scoped_session(sessionmaker())
Base = declarative_base()
# http://docs.sqlalchemy.org/en/rel_0_9/_modules/examples/join_conditions/cast.html
class IntAsString(TypeDecorator):
"""Coerce integer->string type.
This is needed only if the relationship() from
string to int is writable, as SQLAlchemy will copy
the int parent values into the string attribute
on the child during a flush.
"""
impl = String
def process_bind_param(self, value, dialect):
if value is not None:
value = str(value)
return value
def todict(self, exclude=None):
exclude = exclude or []
d = {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment