Example #1
0
    def create_sequence(cls, values):
        """ create the sequence for one instance """
        if 'seq_name' in values:
            seq_name = values['seq_name']
        else:
            seq_id = cls.registry.execute(SQLASequence(cls._cls_seq_name))
            seq_name = '%s_%d' % (cls.__tablename__, seq_id)
            values['seq_name'] = seq_name
        if 'number' in values:
            seq = SQLASequence(seq_name, values['number'])
        else:
            values['number'] = 0
            seq = SQLASequence(seq_name)

        seq.create(cls.registry.bind)
        return values
Example #2
0
    def initialize_model(cls):
        """ Create the sequence to determine name """
        super(Sequence, cls).initialize_model()
        seq = SQLASequence(cls._cls_seq_name)
        seq.create(cls.registry.bind)

        if hasattr(cls.registry, '_need_sequence_to_create_if_not_exist'):
            if cls.registry._need_sequence_to_create_if_not_exist:
                for vals in cls.registry._need_sequence_to_create_if_not_exist:
                    if 'formater' in vals and vals['formater'] is None:
                        del vals['formater']

                    if cls.query().filter(cls.code == vals['code']).count():
                        continue

                    cls.insert(**vals)
Example #3
0
    def create_sequence(cls, values):
        """Create the database sequence for an instance of Sequence Model.

        :return: suitable field values for insertion of the Model instance
        :rtype: dict
        """
        seq_name = values.get('seq_name')
        if seq_name is None:
            seq_id = cls.registry.execute(SQLASequence(cls._cls_seq_name))
            seq_name = '%s_%d' % (cls.__tablename__, seq_id)
            values['seq_name'] = seq_name

        number = values.setdefault('number', 0)
        if number:
            seq = SQLASequence(seq_name, number)
        else:
            seq = SQLASequence(seq_name)
        seq.create(cls.registry.bind)
        return values
Example #4
0
    def initialize_model(cls):
        """ Create the sequence to determine name """
        super(Sequence, cls).initialize_model()
        seq = SQLASequence(cls._cls_seq_name)
        seq.create(cls.registry.bind)

        to_create = getattr(cls.registry,
                            '_need_sequence_to_create_if_not_exist', ())
        if to_create is None:
            return

        for vals in to_create:
            if cls.query().filter(cls.code == vals['code']).count():
                continue

            formatter = vals.get('formater')
            if formatter is None:
                del vals['formater']

            cls.insert(**vals)
from sqlalchemy import Column, Sequence, String, Integer
from sqlalchemy.dialects.postgresql import JSONB
from database import ModelBase


users_id_seq = Sequence('users_id_seq')


class Users(ModelBase):
    __tablename__ = 'users'

    id = Column(Integer,
                users_id_seq,
                server_default=users_id_seq.next_value(),
                primary_key=True,
                nullable=False)
    name = Column(String(255), nullable=False, unique=True)
    comments = Column(JSONB)

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'comments': self.comments,
        }
Example #6
0
 def test_sequence(self):
     self._run_test(Sequence("foo_seq"))
Example #7
0
class MonitoredSite(Base, ObjectWithDynProp):

    __tablename__ = 'MonitoredSite'
    moduleFormName = 'MonitoredSiteForm'
    moduleGridName = 'MonitoredSiteGrid'

    ID = Column(Integer, Sequence('MonitoredSite__id_seq'), primary_key=True)
    Name = Column(String(250), nullable=False)
    Category = Column(String(250), nullable=False)
    Creator = Column(Integer, nullable=False)
    Active = Column(BIT, nullable=False, default=1)
    creationDate = Column(DateTime, nullable=False, default=func.now())
    Place = Column(String(250))

    FK_MonitoredSiteType = Column(Integer, ForeignKey('MonitoredSiteType.ID'))

    MonitoredSitePositions = relationship('MonitoredSitePosition',
                                          backref='MonitoredSite',
                                          cascade="all,delete-orphan")
    MonitoredSiteDynPropValues = relationship('MonitoredSiteDynPropValue',
                                              backref='MonitoredSite',
                                              cascade="all,delete-orphan")
    Stations = relationship('Station')
    Equipments = relationship('Equipment')

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        ObjectWithDynProp.__init__(self)

    # @orm.reconstructor
    # def init_on_load(self):
    #     ''' init_on_load is called on the fetch of object '''
    #     self.__init__()

    def GetNewValue(self, nameProp):
        ReturnedValue = MonitoredSiteDynPropValue()
        ReturnedValue.MonitoredSiteDynProp = self.session.query(
            MonitoredSiteDynProp).filter(
                MonitoredSiteDynProp.Name == nameProp).first()
        return ReturnedValue

    def GetDynPropValues(self):
        return self.MonitoredSiteDynPropValues

    def GetDynProps(self, nameProp):
        return self.session.query(MonitoredSiteDynProp).filter(
            MonitoredSiteDynProp.Name == nameProp).one()

    def GetType(self):
        if self.MonitoredSiteType != None:
            return self.MonitoredSiteType
        else:
            return self.session.query(MonitoredSiteType).get(
                self.FK_MonitoredSiteType)

    def GetAllProp(self):
        props = super(MonitoredSite, self).GetAllProp()
        props.extend([{
            'name': statProp.key,
            'type': statProp.type
        } for statProp in MonitoredSitePosition.__table__.columns])
        return props

    def GetLastPositionWithDate(self, date_):
        query = select([MonitoredSitePosition]).where(
            and_(MonitoredSitePosition.FK_MonitoredSite == self.ID,
                 MonitoredSitePosition.StartDate <= date_)).order_by(
                     desc(MonitoredSitePosition.StartDate)).limit(1)
        curPos = dict(self.session.execute(query).fetchone())
        return curPos

    def LoadNowValues(self):
        super(MonitoredSite, self).LoadNowValues()
        lastPos = self.GetLastPositionWithDate(func.now())
        if lastPos is not None:
            for key in lastPos:
                self.__properties__[key] = lastPos[key]

    def GetSchemaFromStaticProps(self, FrontModules, DisplayMode):
        Editable = (DisplayMode.lower() == 'edit')
        resultat = {}
        type_ = self.GetType().ID
        Fields = self.session.query(ModuleForms).filter(
            ModuleForms.Module_ID == FrontModules.ID).order_by(
                ModuleForms.FormOrder).all()
        SiteProps = dict(self.__table__.columns)
        PoseProps = dict(MonitoredSitePosition.__table__.columns)
        del PoseProps['ID']
        SiteProps.update(PoseProps)

        for curStatProp in SiteProps:
            CurModuleForms = list(
                filter(
                    lambda x: x.Name == curStatProp and
                    (x.TypeObj == str(type_) or x.TypeObj == None), Fields))

            if (len(CurModuleForms) > 0):
                CurModuleForms = CurModuleForms[0]
                resultat[CurModuleForms.Name] = CurModuleForms.GetDTOFromConf(
                    DisplayMode.lower())
        return resultat

    def getFlatObject(self, schema=None):
        ''' return flat object with static properties and
        last existing value of dyn props '''
        resultat = {}
        if self.ID is not None:
            max_iter = max(len(self.__table__.columns),
                           len(self.__properties__))
            for i in range(max_iter):
                try:
                    curStatProp = list(self.__table__.columns)[i]
                    resultat[curStatProp.key] = self.getProperty(
                        curStatProp.key)
                except:
                    pass
                try:
                    curDynPropName = list(self.__properties__)[i]
                    resultat[curDynPropName] = self.getProperty(curDynPropName)
                except Exception as e:
                    # print_exc()
                    pass
        else:
            max_iter = len(self.__table__.columns)
            for i in range(max_iter):
                try:
                    curStatProp = list(self.__table__.columns)[i]
                    curVal = self.getProperty(curStatProp.key)
                    if curVal is not None:
                        resultat[curStatProp.key] = self.getProperty(
                            curStatProp.key)
                except:
                    pass
        if not schema and hasattr(self, 'getForm'):
            schema = self.getForm()['schema']
        if schema:
            resultat = formatValue(resultat, schema)
        return resultat

    def setProperty(self, nameProp, valeur, useDate=None):
        super().setProperty(nameProp, valeur)
        if hasattr(self.newPosition, nameProp):
            curTypeAttr = str(
                self.newPosition.__table__.c[nameProp].type).split('(')[0]

            if 'date'.lower() in curTypeAttr.lower():
                valeur = parse(valeur.replace(' ', ''))
                setattr(self.newPosition, nameProp, valeur)
            else:
                setattr(self.newPosition, nameProp, valeur)
            if ((nameProp not in self.__properties__) or
                (isEqual(self.__properties__[nameProp], valeur) is False)):
                self.positionChanged = True

    def updateFromJSON(self, DTOObject):
        self.newPosition = MonitoredSitePosition()
        self.positionChanged = False
        super(MonitoredSite, self).updateFromJSON(DTOObject)
        if self.positionChanged:
            sameDatePosition = list(
                filter(
                    lambda x: x.StartDate == datetime.strptime(
                        DTOObject['StartDate'], '%d/%m/%Y %H:%M:%S'),
                    self.MonitoredSitePositions))
            if len(sameDatePosition) > 0:
                sameDatePosition[0].LAT = DTOObject['LAT']
                sameDatePosition[0].LON = DTOObject['LON']
                sameDatePosition[0].ELE = DTOObject['ELE']
                sameDatePosition[0].Precision = DTOObject['Precision']
                sameDatePosition[0].Comments = DTOObject['Comments']
            else:
                self.MonitoredSitePositions.append(self.newPosition)
Example #8
0
class DbWallet(Base):
    """
    Database definitions for wallets in Sqlalchemy format

    Contains one or more keys.

    """
    __tablename__ = 'wallets'
    id = Column(Integer,
                Sequence('wallet_id_seq'),
                primary_key=True,
                doc="Unique wallet ID")
    name = Column(String(80), unique=True, doc="Unique wallet name")
    owner = Column(String(50), doc="Wallet owner")
    network_name = Column(String(20),
                          ForeignKey('networks.name'),
                          doc="Name of network, i.e.: bitcoin, litecoin")
    network = relationship("DbNetwork", doc="Link to DbNetwork object")
    purpose = Column(
        Integer,
        doc=
        "Wallet purpose ID. BIP-44 purpose field, indicating which key-scheme is used default is 44"
    )
    scheme = Column(String(25),
                    doc="Key structure type, can be BIP-32 or single")
    witness_type = Column(
        String(20),
        default='legacy',
        doc=
        "Wallet witness type. Can be 'legacy', 'segwit' or 'p2sh-segwit'. Default is legacy."
    )
    encoding = Column(
        String(15),
        default='base58',
        doc=
        "Default encoding to use for address generation, i.e. base58 or bech32. Default is base58."
    )
    main_key_id = Column(
        Integer,
        doc=
        "Masterkey ID for this wallet. All other keys are derived from the masterkey in a "
        "HD wallet bip32 wallet")
    keys = relationship("DbKey",
                        back_populates="wallet",
                        doc="Link to keys (DbKeys objects) in this wallet")
    transactions = relationship(
        "DbTransaction",
        back_populates="wallet",
        doc="Link to transaction (DbTransactions) in this wallet")
    multisig_n_required = Column(
        Integer,
        default=1,
        doc="Number of required signature for multisig, "
        "only used for multisignature master key")
    sort_keys = Column(Boolean,
                       default=False,
                       doc="Sort keys in multisig wallet")
    parent_id = Column(
        Integer,
        ForeignKey('wallets.id'),
        doc="Wallet ID of parent wallet, used in multisig wallets")
    children = relationship(
        "DbWallet",
        lazy="joined",
        join_depth=2,
        doc="Wallet IDs of children wallets, used in multisig wallets")
    multisig = Column(
        Boolean,
        default=True,
        doc="Indicates if wallet is a multisig wallet. Default is True")
    cosigner_id = Column(
        Integer,
        doc=
        "ID of cosigner of this wallet. Used in multisig wallets to differentiate between "
        "different wallets")
    key_path = Column(
        String(100),
        doc=
        "Key path structure used in this wallet. Key path for multisig wallet, use to create "
        "your own non-standard key path. Key path must follow the following rules: "
        "* Path start with masterkey (m) and end with change / address_index "
        "* If accounts are used, the account level must be 3. I.e.: m/purpose/coin_type/account/ "
        "* All keys must be hardened, except for change, address_index or cosigner_id "
        " Max length of path is 8 levels")
    default_account_id = Column(
        Integer,
        doc=
        "ID of default account for this wallet if multiple accounts are used")

    __table_args__ = (
        CheckConstraint(scheme.in_(['single', 'bip32']),
                        name='constraint_allowed_schemes'),
        CheckConstraint(encoding.in_(['base58', 'bech32']),
                        name='constraint_default_address_encodings_allowed'),
        CheckConstraint(witness_type.in_(['legacy', 'segwit', 'p2sh-segwit']),
                        name='wallet_constraint_allowed_types'),
    )

    def __repr__(self):
        return "<DbWallet(name='%s', network='%s'>" % (self.name,
                                                       self.network_name)
Example #9
0
class DbTransaction(Base):
    """
    Database definitions for transactions in Sqlalchemy format

    Refers to 1 or more keys which can be part of a wallet

    """
    __tablename__ = 'transactions'
    id = Column(Integer,
                Sequence('transaction_id_seq'),
                primary_key=True,
                doc="Unique transaction index for internal usage")
    txid = Column(LargeBinary(32),
                  index=True,
                  doc="Bytes representation of transaction ID")
    wallet_id = Column(Integer,
                       ForeignKey('wallets.id'),
                       index=True,
                       doc="ID of wallet which contains this transaction")
    account_id = Column(Integer, index=True, doc="ID of account")
    wallet = relationship(
        "DbWallet",
        back_populates="transactions",
        doc="Link to Wallet object which contains this transaction")
    witness_type = Column(String(20),
                          default='legacy',
                          doc="Is this a legacy or segwit transaction?")
    version = Column(
        BigInteger,
        default=1,
        doc=
        "Tranaction version. Default is 1 but some wallets use another version number"
    )
    locktime = Column(
        BigInteger,
        default=0,
        doc=
        "Transaction level locktime. Locks the transaction until a specified block "
        "(value from 1 to 5 million) or until a certain time (Timestamp in seconds after 1-jan-1970)."
        " Default value is 0 for transactions without locktime")
    date = Column(
        DateTime,
        default=datetime.utcnow,
        doc="Date when transaction was confirmed and included in a block. "
        "Or when it was created when transaction is not send or confirmed")
    coinbase = Column(
        Boolean,
        default=False,
        doc="Is True when this is a coinbase transaction, default is False")
    confirmations = Column(
        Integer,
        default=0,
        doc=
        "Number of confirmation when this transaction is included in a block. "
        "Default is 0: unconfirmed")
    block_height = Column(
        Integer,
        index=True,
        doc="Number of block this transaction is included in")
    size = Column(Integer, doc="Size of the raw transaction in bytes")
    fee = Column(BigInteger, doc="Transaction fee")
    inputs = relationship(
        "DbTransactionInput",
        cascade="all,delete",
        doc="List of all inputs as DbTransactionInput objects")
    outputs = relationship(
        "DbTransactionOutput",
        cascade="all,delete",
        doc="List of all outputs as DbTransactionOutput objects")
    status = Column(
        String(20),
        default='new',
        doc="Current status of transaction, can be one of the following: new', "
        "'unconfirmed', 'confirmed'. Default is 'new'")
    is_complete = Column(
        Boolean,
        default=True,
        doc="Allow to store incomplete transactions, for instance if not all "
        "inputs are known when retrieving UTXO's")
    input_total = Column(
        BigInteger,
        default=0,
        doc=
        "Total value of the inputs of this transaction. Input total = Output total + fee. "
        "Default is 0")
    output_total = Column(
        BigInteger,
        default=0,
        doc=
        "Total value of the outputs of this transaction. Output total = Input total - fee"
    )
    network_name = Column(String(20),
                          ForeignKey('networks.name'),
                          doc="Blockchain network name of this transaction")
    network = relationship("DbNetwork", doc="Link to DbNetwork object")
    raw = Column(
        LargeBinary,
        doc=
        "Raw transaction hexadecimal string. Transaction is included in raw format on the blockchain"
    )
    verified = Column(Boolean,
                      default=False,
                      doc="Is transaction verified. Default is False")

    __table_args__ = (
        UniqueConstraint('wallet_id',
                         'txid',
                         name='constraint_wallet_transaction_hash_unique'),
        CheckConstraint(status.in_(['new', 'unconfirmed', 'confirmed']),
                        name='constraint_status_allowed'),
        CheckConstraint(witness_type.in_(['legacy', 'segwit']),
                        name='transaction_constraint_allowed_types'),
    )

    def __repr__(self):
        return "<DbTransaction(txid='%s', confirmations='%s')>" % (
            self.txid, self.confirmations)
Example #10
0
from app.connect import connection, mymetadata_generate_process
from sqlalchemy import Table, Integer, Column, Integer, String, Sequence, DateTime, Boolean, JSON, ForeignKey
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import mapper, relationship
from app.input.entities.input import Einput
from app.generate.entities.last_month import ElastMonth

table_inputs = Table(
    'inputs', mymetadata_generate_process,
    Column('id_input',
           Integer,
           Sequence('inputs_endowments_seq'),
           primary_key=True,
           nullable=False), Column('taks_id', String, nullable=False),
    Column('input_json', JSON, nullable=False),
    Column('is_active', Boolean, nullable=False),
    Column('last_month_id',
           Integer,
           ForeignKey('last_month.id_last_month'),
           nullable=False), Column('attempts', Integer, nullable=False),
    Column('created', DateTime, nullable=False), Column('updated', DateTime))

mapper(Einput,
       table_inputs,
       properties={
           'last_month': relationship(ElastMonth, backref='table_inputs')
       })


def add_input(data: Einput):
    try:
Example #11
0
from config import db
from sqlalchemy import Column, Integer, String, Sequence, Date

ORDER_ID_SEQ = Sequence('order_id_seq')


class Order(db.Model):
    __tablename__ = "orders"
    id = db.Column(db.Integer(),
                   ORDER_ID_SEQ,
                   primary_key=True,
                   server_default=ORDER_ID_SEQ.next_value())
    user_id = db.Column(db.Integer(), unique=False, nullable=False)
    item_id = db.Column(db.Integer(), unique=False, nullable=False)
    number_of_items = db.Column(db.Integer(), unique=False, nullable=False)
    date = db.Column(db.Date(), unique=False, nullable=False)
    status = db.Column(db.String(),
                       unique=False,
                       nullable=False,
                       default='new')

    def __init__(self, user_id, item_id, number_of_items, date, status):
        self.user_id = user_id
        self.item_id = item_id
        self.number_of_items = number_of_items
        self.date = date
        self.status = status

    def __repr__(self):
        return "Order(%s, %s, %d, %d, %s)" % (self.user_id, self.item_id,
                                              self.number_of_items, self.date,
Example #12
0
    def test_create_drop_ddl(self):
        self.assert_compile(
            CreateSequence(Sequence("foo_seq")),
            "CREATE SEQUENCE foo_seq START WITH 1",
        )

        self.assert_compile(
            CreateSequence(Sequence("foo_seq", start=5)),
            "CREATE SEQUENCE foo_seq START WITH 5",
        )

        self.assert_compile(
            CreateSequence(Sequence("foo_seq", increment=2)),
            "CREATE SEQUENCE foo_seq INCREMENT BY 2 START WITH 1",
        )

        self.assert_compile(
            CreateSequence(Sequence("foo_seq", increment=2, start=5)),
            "CREATE SEQUENCE foo_seq INCREMENT BY 2 START WITH 5",
        )

        self.assert_compile(
            CreateSequence(
                Sequence("foo_seq", increment=2, start=0, minvalue=0)),
            "CREATE SEQUENCE foo_seq INCREMENT BY 2 START WITH 0 MINVALUE 0",
        )

        self.assert_compile(
            CreateSequence(
                Sequence("foo_seq", increment=2, start=1, maxvalue=5)),
            "CREATE SEQUENCE foo_seq INCREMENT BY 2 START WITH 1 MAXVALUE 5",
        )

        self.assert_compile(
            CreateSequence(
                Sequence("foo_seq", increment=2, start=1, nomaxvalue=True)),
            "CREATE SEQUENCE foo_seq INCREMENT BY 2 START WITH 1 NO MAXVALUE",
        )

        self.assert_compile(
            CreateSequence(
                Sequence("foo_seq", increment=2, start=0, nominvalue=True)),
            "CREATE SEQUENCE foo_seq INCREMENT BY 2 START WITH 0 NO MINVALUE",
        )

        self.assert_compile(
            CreateSequence(
                Sequence("foo_seq", start=1, maxvalue=10, cycle=True)),
            "CREATE SEQUENCE foo_seq START WITH 1 MAXVALUE 10 CYCLE",
        )

        self.assert_compile(
            CreateSequence(Sequence("foo_seq", cache=1000, order=True)),
            "CREATE SEQUENCE foo_seq START WITH 1 CACHE 1000 ORDER",
        )

        self.assert_compile(
            CreateSequence(Sequence("foo_seq", order=True)),
            "CREATE SEQUENCE foo_seq START WITH 1 ORDER",
        )

        self.assert_compile(DropSequence(Sequence("foo_seq")),
                            "DROP SEQUENCE foo_seq")
Example #13
0
 def setup_class(cls):
     cls.seq = Sequence("my_sequence")
     cls.seq.create(testing.db)
Example #14
0
class SequenceTest(fixtures.TestBase, testing.AssertsCompiledSQL):
    __requires__ = ("sequences", )
    __backend__ = True

    @testing.combinations(
        (Sequence("foo_seq"), ),
        (Sequence("foo_seq", start=8), ),
        (Sequence("foo_seq", increment=5), ),
    )
    def test_start_increment(self, seq):
        seq.create(testing.db)
        try:
            with testing.db.connect() as conn:
                values = [conn.execute(seq) for i in range(3)]
                start = seq.start or testing.db.dialect.default_sequence_base
                inc = seq.increment or 1
                eq_(values, list(range(start, start + inc * 3, inc)))

        finally:
            seq.drop(testing.db)

    def _has_sequence(self, connection, name):
        return testing.db.dialect.has_sequence(connection, name)

    def test_nextval_unsupported(self):
        """test next_value() used on non-sequence platform
        raises NotImplementedError."""

        s = Sequence("my_seq")
        d = sqlite.dialect()
        assert_raises_message(
            NotImplementedError,
            "Dialect 'sqlite' does not support sequence increments.",
            s.next_value().compile,
            dialect=d,
        )

    def test_checkfirst_sequence(self, connection):
        s = Sequence("my_sequence")
        s.create(connection, checkfirst=False)
        assert self._has_sequence(connection, "my_sequence")
        s.create(connection, checkfirst=True)
        s.drop(connection, checkfirst=False)
        assert not self._has_sequence(connection, "my_sequence")
        s.drop(connection, checkfirst=True)

    def test_checkfirst_metadata(self, connection):
        m = MetaData()
        Sequence("my_sequence", metadata=m)
        m.create_all(connection, checkfirst=False)
        assert self._has_sequence(connection, "my_sequence")
        m.create_all(connection, checkfirst=True)
        m.drop_all(connection, checkfirst=False)
        assert not self._has_sequence(connection, "my_sequence")
        m.drop_all(connection, checkfirst=True)

    def test_checkfirst_table(self, connection):
        m = MetaData()
        s = Sequence("my_sequence")
        t = Table("t", m, Column("c", Integer, s, primary_key=True))
        t.create(connection, checkfirst=False)
        assert self._has_sequence(connection, "my_sequence")
        t.create(connection, checkfirst=True)
        t.drop(connection, checkfirst=False)
        assert not self._has_sequence(connection, "my_sequence")
        t.drop(connection, checkfirst=True)

    @testing.provide_metadata
    def test_table_overrides_metadata_create(self, connection):
        metadata = self.metadata
        Sequence("s1", metadata=metadata)
        s2 = Sequence("s2", metadata=metadata)
        s3 = Sequence("s3")
        t = Table("t", metadata, Column("c", Integer, s3, primary_key=True))
        assert s3.metadata is metadata

        t.create(connection, checkfirst=True)
        s3.drop(connection)

        # 't' is created, and 's3' won't be
        # re-created since it's linked to 't'.
        # 's1' and 's2' are, however.
        metadata.create_all(connection)
        assert self._has_sequence(connection, "s1")
        assert self._has_sequence(connection, "s2")
        assert not self._has_sequence(connection, "s3")

        s2.drop(connection)
        assert self._has_sequence(connection, "s1")
        assert not self._has_sequence(connection, "s2")

        metadata.drop_all(connection)
        assert not self._has_sequence(connection, "s1")
        assert not self._has_sequence(connection, "s2")

    @testing.requires.returning
    @testing.requires.supports_sequence_for_autoincrement_column
    @testing.provide_metadata
    def test_freestanding_sequence_via_autoinc(self, connection):
        t = Table(
            "some_table",
            self.metadata,
            Column(
                "id",
                Integer,
                autoincrement=True,
                primary_key=True,
                default=Sequence("my_sequence",
                                 metadata=self.metadata).next_value(),
            ),
        )
        self.metadata.create_all(connection)

        result = connection.execute(t.insert())
        eq_(result.inserted_primary_key, (1, ))
Example #15
0
    def test_func_embedded_select(self, connection):
        """test can use next_value() in select column expr"""

        s = Sequence("my_sequence")
        self._assert_seq_result(connection.scalar(select(s.next_value())))
Example #16
0
    def test_execute_optional_next_value(self, connection):
        """test func.next_value().execute()/.scalar() works
        with connectionless execution."""

        s = Sequence("my_sequence", optional=True)
        self._assert_seq_result(connection.scalar(s.next_value()))
Example #17
0
    def test_execute_optional(self, connection):
        """test dialect executes a Sequence, returns nextval, whether
        or not "optional" is set"""

        s = Sequence("my_sequence", optional=True)
        self._assert_seq_result(connection.execute(s))
Example #18
0
class Collection(Base):

    __tablename__ = 'collection'

    def __repr__(self):
        return "<Collection (%s)>" % self.id

    id = Column(Integer,
                Sequence('collection_seq_id', optional=True),
                primary_key=True)
    UUID = Column(Unicode(36))
    title = Column(Unicode(255))
    type = Column(Unicode(255))
    url = Column(Unicode(255))
    description = Column(UnicodeText)
    markup_language = Column(Unicode(100))
    contents = Column(UnicodeText)
    html = Column(UnicodeText)
    speaker_id = Column(Integer, ForeignKey('speaker.id', ondelete='SET NULL'))
    speaker = relation('Speaker')
    source_id = Column(Integer, ForeignKey('source.id', ondelete='SET NULL'))
    source = relation('Source')
    elicitor_id = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'))
    elicitor = relation('User', primaryjoin='Collection.elicitor_id==User.id')
    enterer_id = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'))
    enterer = relation('User', primaryjoin='Collection.enterer_id==User.id')
    modifier_id = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'))
    modifier = relation('User', primaryjoin='Collection.modifier_id==User.id')
    date_elicited = Column(Date)
    datetime_entered = Column(DateTime)
    datetime_modified = Column(DateTime, default=now)
    tags = relation('Tag', secondary=CollectionTag.__table__)
    files = relation('File',
                     secondary=CollectionFile.__table__,
                     backref='collections')
    # forms attribute is defined in a relation/backref in the form model

    # The contents_unpacked column holds the contents of the collection where all
    # collection references in the contents field are replaced with the contents
    # of the referred-to collections.  These referred-to collections can refer
    # to others in turn.  The forms related to a collection are calculated by
    # gathering the form references from contents_unpacked.  The result of all
    # this is that the contents (and form references) of a collection can be
    # altered by updates to another collection; however, these updates will not
    # propagate until the collection in question is itself updated.
    contents_unpacked = Column(UnicodeText)

    def get_dict(self):
        """Return a Python dictionary representation of the Collection.  This
        facilitates JSON-stringification, cf. utils.JSONOLDEncoder.  Relational
        data are truncated, e.g., collection_dict['elicitor'] is a dict with keys
        for 'id', 'first_name' and 'last_name' (cf. get_mini_user_dict above) and
        lacks keys for other attributes such as 'username',
        'personal_page_content', etc.
        """

        return {
            'id': self.id,
            'UUID': self.UUID,
            'title': self.title,
            'type': self.type,
            'url': self.url,
            'description': self.description,
            'markup_language': self.markup_language,
            'contents': self.contents,
            'contents_unpacked': self.contents_unpacked,
            'html': self.html,
            'date_elicited': self.date_elicited,
            'datetime_entered': self.datetime_entered,
            'datetime_modified': self.datetime_modified,
            'speaker': self.get_mini_speaker_dict(self.speaker),
            'source': self.get_mini_source_dict(self.source),
            'elicitor': self.get_mini_user_dict(self.elicitor),
            'enterer': self.get_mini_user_dict(self.enterer),
            'modifier': self.get_mini_user_dict(self.modifier),
            'tags': self.get_tags_list(self.tags),
            'files': self.get_files_list(self.files)
        }

    def get_full_dict(self):
        result = self.get_dict()
        result['forms'] = self.get_forms_list(self.forms)
        return result
Example #19
0
 def test_checkfirst(self):
     s = Sequence("my_sequence")
     s.create(testing.db, checkfirst=False)
     s.create(testing.db, checkfirst=True)
     s.drop(testing.db, checkfirst=False)
     s.drop(testing.db, checkfirst=True)
Example #20
0
"""Db connectivity in this application is done using sqlalchemy core + txpostgres

Parts of the application use these tables to create sqlalchemy core queries,
which are then handed off to the connectionpool (txchatexamples.util.ConnectionPool)
to be rendered.

"""

from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, Sequence
from sqlalchemy.dialects.postgresql import TIMESTAMP

md = MetaData()

chat_users_id_seq = Sequence('chat_users_id_seq')

users = Table(
    'chat_users', md,
    Column('user_id', Integer(), chat_users_id_seq, primary_key=True),
    Column('token', String(32), unique=True),
    Column('name', String(200), unique=True))

chat_logs_seq = Sequence('chat_logs_id_seq')

logs = Table(
    'chat_logs', md,
    Column('log_id',
           Integer(),
           chat_logs_seq,
           server_default=chat_logs_seq.next_value(),
           primary_key=True),
    Column('user_id', Integer(), ForeignKey('chat_users')),
Example #21
0
class User(DeclarativeBase):
    """
    User definition.

    This is the user definition used by :mod:`repoze.who`, which requires at
    least the ``email`` column.
    """

    __tablename__ = 'users'

    user_id = Column(Integer,
                     Sequence('seq__users__user_id'),
                     autoincrement=True,
                     primary_key=True)
    email = Column(Unicode(255), unique=True, nullable=False)
    display_name = Column(Unicode(255))
    _password = Column('password', Unicode(128))
    created = Column(DateTime, default=datetime.utcnow)
    is_active = Column(Boolean, default=True, nullable=False)
    imported_from = Column(Unicode(32), nullable=True)
    timezone = Column(Unicode(255), nullable=False, server_default='')
    _webdav_left_digest_response_hash = Column(
        'webdav_left_digest_response_hash', Unicode(128))
    auth_token = Column(Unicode(255))
    auth_token_created = Column(DateTime)

    @hybrid_property
    def email_address(self):
        return self.email

    def __repr__(self):
        return '<User: email=%s, display=%s>' % (repr(
            self.email), repr(self.display_name))

    def __unicode__(self):
        return self.display_name or self.email

    @property
    def permissions(self):
        """Return a set with all permissions granted to the user."""
        perms = set()
        for g in self.groups:
            perms = perms | set(g.permissions)
        return perms

    @property
    def profile(self) -> Profile:
        profile_id = 0
        if len(self.groups) > 0:
            profile_id = max(group.group_id for group in self.groups)
        return Profile(profile_id)

    @property
    def calendar_url(self) -> str:
        # TODO - 20160531 - Bastien: Cyclic import if import in top of file
        from tracim.lib.calendar import CalendarManager
        calendar_manager = CalendarManager(None)

        return calendar_manager.get_user_calendar_url(self.user_id)

    @classmethod
    def by_email_address(cls, email):
        """Return the user object whose email address is ``email``."""
        return DBSession.query(cls).filter_by(email=email).first()

    @classmethod
    def by_user_name(cls, username):
        """Return the user object whose user name is ``username``."""
        return DBSession.query(cls).filter_by(email=username).first()

    @classmethod
    def _hash_password(cls, cleartext_password: str) -> str:
        salt = sha256()
        salt.update(os.urandom(60))
        salt = salt.hexdigest()

        hash = sha256()
        # Make sure password is a str because we cannot hash unicode objects
        hash.update((cleartext_password + salt).encode('utf-8'))
        hash = hash.hexdigest()

        ciphertext_password = salt + hash

        # Make sure the hashed password is a unicode object at the end of the
        # process because SQLAlchemy _wants_ unicode objects for Unicode cols
        # FIXME - D.A. - 2013-11-20 - The following line has been removed since using python3. Is this normal ?!
        # password = password.decode('utf-8')

        return ciphertext_password

    def _set_password(self, cleartext_password: str) -> None:
        """
        Set ciphertext password from cleartext password.

        Hash cleartext password on the fly,
        Store its ciphertext version,
        Update the WebDAV hash as well.
        """
        self._password = self._hash_password(cleartext_password)
        self.update_webdav_digest_auth(cleartext_password)

    def _get_password(self) -> str:
        """Return the hashed version of the password."""
        return self._password

    password = synonym('_password',
                       descriptor=property(_get_password, _set_password))

    @classmethod
    def _hash_digest(cls, digest):
        return md5(bytes(digest, 'utf-8')).hexdigest()

    def _set_hash_digest(self, digest):
        self._webdav_left_digest_response_hash = self._hash_digest(digest)

    def _get_hash_digest(self):
        return self._webdav_left_digest_response_hash

    webdav_left_digest_response_hash = synonym(
        '_webdav_left_digest_response_hash',
        descriptor=property(_get_hash_digest, _set_hash_digest))

    def update_webdav_digest_auth(self, cleartext_password: str) -> None:
        self.webdav_left_digest_response_hash \
            = '{username}:/:{cleartext_password}'.format(
                username=self.email,
                cleartext_password=cleartext_password,
            )

    def validate_password(self, cleartext_password: str) -> bool:
        """
        Check the password against existing credentials.

        :param cleartext_password: the password that was provided by the user
            to try and authenticate. This is the clear text version that we
            will need to match against the hashed one in the database.
        :type cleartext_password: unicode object.
        :return: Whether the password is valid.
        :rtype: bool

        """
        result = False
        if self.password:
            hash = sha256()
            hash.update(
                (cleartext_password + self.password[:64]).encode('utf-8'))
            result = self.password[64:] == hash.hexdigest()
            if result and not self.webdav_left_digest_response_hash:
                self.update_webdav_digest_auth(cleartext_password)
        return result

    def get_display_name(self, remove_email_part: bool = False) -> str:
        """
        Get a name to display from corresponding member or email.

        :param remove_email_part: If True and display name based on email,
            remove @xxx.xxx part of email in returned value
        :return: display name based on user name or email.
        """
        if self.display_name:
            return self.display_name
        else:
            if remove_email_part:
                at_pos = self.email.index('@')
                return self.email[0:at_pos]
            return self.email

    def get_role(self, workspace: 'Workspace') -> int:
        for role in self.roles:
            if role.workspace == workspace:
                return role.role

        from tracim.model.data import UserRoleInWorkspace
        return UserRoleInWorkspace.NOT_APPLICABLE

    def get_active_roles(self) -> ['UserRoleInWorkspace']:
        """
        :return: list of roles of the user for all not-deleted workspaces
        """
        roles = []
        for role in self.roles:
            if not role.workspace.is_deleted:
                roles.append(role)
        return roles

    def ensure_auth_token(self) -> None:
        """
        Create auth_token if None, regenerate auth_token if too much old.

        auth_token validity is set in
        :return:
        """
        from tracim.config.app_cfg import CFG
        validity_seconds = CFG.get_instance().USER_AUTH_TOKEN_VALIDITY

        if not self.auth_token or not self.auth_token_created:
            self.auth_token = str(uuid.uuid4())
            self.auth_token_created = datetime.utcnow()
            DBSession.flush()
            return

        now_seconds = time.mktime(datetime.utcnow().timetuple())
        auth_token_seconds = time.mktime(self.auth_token_created.timetuple())
        difference = now_seconds - auth_token_seconds

        if difference > validity_seconds:
            self.auth_token = uuid.uuid4()
            self.auth_token_created = datetime.utcnow()
            DBSession.flush()
Example #22
0
session = DBSession()


class DailyStats(Base):
    __tablename__ = 'daily_stats'
    date = Column(DateTime, primary_key=True)
    posts = Column(Integer)
    comments = Column(Integer)
    comments_per_post = Column(DECIMAL(4, 2))
    positive_polarity = Column(DECIMAL(4, 2))
    negative_polarity = Column(DECIMAL(4, 2))
    net_polarity = Column(DECIMAL(4, 2))
    subjectivity = Column(DECIMAL(4, 2))


Column(Integer, Sequence('user_id_seq'), primary_key=True)


class Posts(Base):
    __tablename__ = 'posts'
    url = Column(String, primary_key=True)
    date = Column(DateTime)
    user = Column(String)
    title = Column(String)
    polarity = Column(DECIMAL(4, 2))
    subjectivity = Column(DECIMAL(4, 2))
    comment_count = Column(Integer)


def init_graphs():
    dailyStats = session.query(DailyStats).order_by(DailyStats.date).all()
Example #23
0
class DbKey(Base):
    """
    Database definitions for keys in Sqlalchemy format

    Part of a wallet, and used by transactions

    """
    __tablename__ = 'keys'
    id = Column(Integer,
                Sequence('key_id_seq'),
                primary_key=True,
                doc="Unique Key ID")
    parent_id = Column(Integer,
                       Sequence('parent_id_seq'),
                       doc="Parent Key ID. Used in HD wallets")
    name = Column(String(80), index=True, doc="Key name string")
    account_id = Column(Integer,
                        index=True,
                        doc="ID of account if key is part of a HD structure")
    depth = Column(
        Integer,
        doc=
        "Depth of key if it is part of a HD structure. Depth=0 means masterkey, "
        "depth=1 are the masterkeys children.")
    change = Column(Integer,
                    doc="Change or normal address: Normal=0, Change=1")
    address_index = Column(
        BigInteger, doc="Index of address in HD key structure address level")
    public = Column(LargeBinary(128),
                    index=True,
                    doc="Bytes representation of public key")
    private = Column(LargeBinary(128),
                     index=True,
                     doc="Bytes representation of private key")
    wif = Column(
        String(255),
        index=True,
        doc="Public or private WIF (Wallet Import Format) representation")
    compressed = Column(Boolean,
                        default=True,
                        doc="Is key compressed or not. Default is True")
    key_type = Column(
        String(10),
        default='bip32',
        doc="Type of key: single, bip32 or multisig. Default is bip32")
    address = Column(
        String(255),
        index=True,
        doc=
        "Address representation of key. An cryptocurrency address is a hash of the public key"
    )
    cosigner_id = Column(
        Integer, doc="ID of cosigner, used if key is part of HD Wallet")
    encoding = Column(
        String(15),
        default='base58',
        doc='Encoding used to represent address: base58 or bech32')
    purpose = Column(Integer, default=44, doc="Purpose ID, default is 44")
    is_private = Column(Boolean, doc="Is key private or not?")
    path = Column(String(100), doc="String of BIP-32 key path")
    wallet_id = Column(Integer,
                       ForeignKey('wallets.id'),
                       index=True,
                       doc="Wallet ID which contains this key")
    wallet = relationship("DbWallet",
                          back_populates="keys",
                          doc="Related Wallet object")
    transaction_inputs = relationship(
        "DbTransactionInput",
        cascade="all,delete",
        back_populates="key",
        doc="All DbTransactionInput objects this key is part of")
    transaction_outputs = relationship(
        "DbTransactionOutput",
        cascade="all,delete",
        back_populates="key",
        doc="All DbTransactionOutput objects this key is part of")
    balance = Column(BigInteger,
                     default=0,
                     doc="Total balance of UTXO's linked to this key")
    used = Column(
        Boolean,
        default=False,
        doc="Has key already been used on the blockchain in as input or output? "
        "Default is False")
    network_name = Column(
        String(20),
        ForeignKey('networks.name'),
        doc="Name of key network, i.e. bitcoin, litecoin, dash")
    latest_txid = Column(
        LargeBinary(32),
        doc="TxId of latest transaction downloaded from the blockchain")
    network = relationship("DbNetwork", doc="DbNetwork object for this key")
    multisig_parents = relationship(
        "DbKeyMultisigChildren",
        backref='child_key',
        primaryjoin=id == DbKeyMultisigChildren.child_id,
        doc="List of parent keys")
    multisig_children = relationship(
        "DbKeyMultisigChildren",
        backref='parent_key',
        order_by="DbKeyMultisigChildren.key_order",
        primaryjoin=id == DbKeyMultisigChildren.parent_id,
        doc="List of children keys")

    __table_args__ = (
        CheckConstraint(key_type.in_(['single', 'bip32', 'multisig']),
                        name='constraint_key_types_allowed'),
        CheckConstraint(encoding.in_(['base58', 'bech32']),
                        name='constraint_address_encodings_allowed'),
        UniqueConstraint('wallet_id',
                         'public',
                         name='constraint_wallet_pubkey_unique'),
        UniqueConstraint('wallet_id',
                         'private',
                         name='constraint_wallet_privkey_unique'),
        UniqueConstraint('wallet_id',
                         'wif',
                         name='constraint_wallet_wif_unique'),
        UniqueConstraint('wallet_id',
                         'address',
                         name='constraint_wallet_address_unique'),
    )

    def __repr__(self):
        return "<DbKey(id='%s', name='%s', wif='%s'>" % (self.id, self.name,
                                                         self.wif)
Example #24
0
 def id_sequence(cls):
     return Sequence(cls.id_sequence_name, schema=cls.metadata.schema)
Example #25
0
    def test_update(self, connection):
        """
        Tests sending functions and SQL expressions to the VALUES and SET
        clauses of INSERT/UPDATE instances, and that column-level defaults
        get overridden.
        """

        meta = self.metadata
        t = Table(
            "t1",
            meta,
            Column(
                "id",
                testing.db.dialect.sequence_default_column_type,
                Sequence("t1idseq", optional=True),
                primary_key=True,
            ),
            Column("value", Integer),
        )
        t2 = Table(
            "t2",
            meta,
            Column(
                "id",
                testing.db.dialect.sequence_default_column_type,
                Sequence("t2idseq", optional=True),
                primary_key=True,
            ),
            Column("value", Integer, default=7),
            Column("stuff", String(20), onupdate="thisisstuff"),
        )
        meta.create_all(connection)
        connection.execute(t.insert(values=dict(value=func.length("one"))))
        eq_(connection.execute(t.select()).first().value, 3)
        connection.execute(t.update(values=dict(value=func.length("asfda"))))
        eq_(connection.execute(t.select()).first().value, 5)

        r = connection.execute(
            t.insert(values=dict(value=func.length("sfsaafsda"))))
        id_ = r.inserted_primary_key[0]
        eq_(connection.execute(t.select(t.c.id == id_)).first().value, 9)
        connection.execute(t.update(values={t.c.value: func.length("asdf")}))
        eq_(connection.execute(t.select()).first().value, 4)
        connection.execute(t2.insert())
        connection.execute(t2.insert(values=dict(value=func.length("one"))))
        connection.execute(
            t2.insert(values=dict(value=func.length("asfda") + -19)),
            stuff="hi",
        )

        res = sorted(connection.execute(select([t2.c.value, t2.c.stuff])))
        eq_(res, [(-14, "hi"), (3, None), (7, None)])

        connection.execute(
            t2.update(values=dict(value=func.length("asdsafasd"))),
            stuff="some stuff",
        )
        eq_(
            connection.execute(select([t2.c.value, t2.c.stuff])).fetchall(),
            [(9, "some stuff"), (9, "some stuff"), (9, "some stuff")],
        )

        connection.execute(t2.delete())

        connection.execute(t2.insert(values=dict(value=func.length("one") +
                                                 8)))
        eq_(connection.execute(t2.select()).first().value, 11)

        connection.execute(t2.update(values=dict(value=func.length("asfda"))))
        eq_(
            connection.execute(select([t2.c.value, t2.c.stuff])).first(),
            (5, "thisisstuff"),
        )

        connection.execute(
            t2.update(values={
                t2.c.value: func.length("asfdaasdf"),
                t2.c.stuff: "foo",
            }))

        eq_(
            connection.execute(select([t2.c.value, t2.c.stuff])).first(),
            (9, "foo"),
        )
Example #26
0
class Morphology(MorphologyFST, Base):

    __tablename__ = 'morphology'

    def __repr__(self):
        return '<Morphology (%s)>' % self.id

    id = Column(Integer,
                Sequence('morphology_seq_id', optional=True),
                primary_key=True)
    UUID = Column(Unicode(36))
    name = Column(Unicode(255))
    description = Column(UnicodeText)
    script_type = Column(Unicode(5))
    lexicon_corpus_id = Column(Integer,
                               ForeignKey('corpus.id', ondelete='SET NULL'))
    lexicon_corpus = relation(
        'Corpus', primaryjoin='Morphology.lexicon_corpus_id==Corpus.id')
    rules_corpus_id = Column(Integer,
                             ForeignKey('corpus.id', ondelete='SET NULL'))
    rules_corpus = relation(
        'Corpus', primaryjoin='Morphology.rules_corpus_id==Corpus.id')
    enterer_id = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'))
    enterer = relation('User', primaryjoin='Morphology.enterer_id==User.id')
    modifier_id = Column(Integer, ForeignKey('user.id', ondelete='SET NULL'))
    modifier = relation('User', primaryjoin='Morphology.modifier_id==User.id')
    datetime_entered = Column(DateTime)
    datetime_modified = Column(DateTime, default=now)
    compile_succeeded = Column(Boolean, default=False)
    compile_message = Column(Unicode(255))
    compile_attempt = Column(Unicode(36))  # a UUID
    generate_attempt = Column(Unicode(36))  # a UUID
    extract_morphemes_from_rules_corpus = Column(Boolean, default=False)
    rules_generated = Column(
        UnicodeText
    )  # word formation rules (i.e., strings of categories and delimiters) separated by spaces -- system-generated value
    rules = Column(
        UnicodeText
    )  # word formation rules (i.e., strings of categories and delimiters) separated by spaces -- user-generated value
    rich_upper = Column(
        Boolean, default=False
    )  # if True, the morphemes on the upper side of the tape are in <f|g|c> forma, else f format
    rich_lower = Column(
        Boolean, default=False
    )  # if True, the morphemes on the lower side of the tape are in <f|g|c> forma, else f format
    include_unknowns = Column(
        Boolean, default=False
    )  # if True, morphemes of unknown category will be added to lexicon.
    # NOTE: the include_unknowns attribute currently has no effect: it turned out to be a major complication!

    # These incidental attributes are initialized by the constructor.
    parent_directory = Column(Unicode(255))
    word_boundary_symbol = Column(Unicode(10))
    rare_delimiter = Column(Unicode(10))
    morpheme_delimiters = Column(Unicode(255))

    def get_dict(self):
        return {
            'id': self.id,
            'UUID': self.UUID,
            'name': self.name,
            'lexicon_corpus': self.get_mini_dict_for(self.lexicon_corpus),
            'rules_corpus': self.get_mini_dict_for(self.rules_corpus),
            'script_type': self.script_type,
            'description': self.description,
            'enterer': self.get_mini_user_dict(self.enterer),
            'modifier': self.get_mini_user_dict(self.modifier),
            'datetime_entered': self.datetime_entered,
            'datetime_modified': self.datetime_modified,
            'compile_succeeded': self.compile_succeeded,
            'compile_message': self.compile_message,
            'compile_attempt': self.compile_attempt,
            'generate_attempt': self.generate_attempt,
            'extract_morphemes_from_rules_corpus':
            self.extract_morphemes_from_rules_corpus,
            'rules': self.rules,
            'rules_generated': self.rules_generated,
            'rich_upper': self.rich_upper,
            'rich_lower': self.rich_lower,
            'include_unknowns': self.include_unknowns
        }

    def generate_rules_and_lexicon(self):
        try:
            return self._generate_rules_and_lexicon()
        except Exception, e:
            log.warn('GOT EXCEPTION TRYING TO GENERATE RULES AND LEXICON')
            log.warn(e)
            return [], {}
Example #27
0
    def test_py_vs_server_default_detection_two(self):
        has_ = self._check_default_slots

        metadata = MetaData()
        ColumnDefault, DefaultClause = sa.ColumnDefault, sa.DefaultClause

        tbl = Table(
            "t2",
            metadata,
            Column("col1", Integer, Sequence("foo")),
            Column(
                "col2", Integer, default=Sequence("foo"), server_default="y"
            ),
            Column("col3", Integer, Sequence("foo"), server_default="x"),
            Column("col4", Integer, ColumnDefault("x"), DefaultClause("y")),
            Column(
                "col5",
                Integer,
                ColumnDefault("x"),
                DefaultClause("y"),
                onupdate="z",
            ),
            Column(
                "col6",
                Integer,
                ColumnDefault("x"),
                server_default="y",
                onupdate="z",
            ),
            Column(
                "col7", Integer, default="x", server_default="y", onupdate="z"
            ),
            Column(
                "col8",
                Integer,
                server_onupdate="u",
                default="x",
                server_default="y",
                onupdate="z",
            ),
        )
        tbl.append_column(
            Column(
                "col4",
                Integer,
                ColumnDefault("x"),
                DefaultClause("y"),
                DefaultClause("y", for_update=True),
            ),
            replace_existing=True,
        )
        has_(tbl, "col1", "default")
        has_(tbl, "col2", "default", "server_default")
        has_(tbl, "col3", "default", "server_default")
        has_(tbl, "col4", "default", "server_default", "server_onupdate")
        has_(tbl, "col5", "default", "server_default", "onupdate")
        has_(tbl, "col6", "default", "server_default", "onupdate")
        has_(tbl, "col7", "default", "server_default", "onupdate")
        has_(
            tbl,
            "col8",
            "default",
            "server_default",
            "onupdate",
            "server_onupdate",
        )
Example #28
0
class BsPerson(Base):

    __tablename__ = 'bs_person'

    name = Column(String)
    first_name = Column(String)
    middle_name = Column(String)
    date_of_birth = Column(Date)
    contact_surname = Column(String)
    contact_first_name = Column(String)
    contact_position = Column(String)
    person_id = Column(String,
                       Sequence('ps_person_person_id_seq'),
                       primary_key=True)
    # person_id = Column(Integer, primary_key=True)
    person_register = Column(String)
    state_registration_no = Column(String)
    bank_account_no = Column(String)
    phone = Column(String)
    mobile_phone = Column(String)
    email_address = Column(String)
    website = Column(String)
    address_town_or_local_name = Column(String)
    address_neighbourhood = Column(String)
    address_street_name = Column(String)
    address_khaskhaa = Column(String)
    address_building_no = Column(String)
    address_entrance_no = Column(String)
    address_apartment_no = Column(String)
    created_at = Column(DateTime)
    updated_at = Column(DateTime)

    # foreign keys:
    type = Column(Integer, ForeignKey('cl_person_type.code'))
    type_ref = relationship("ClPersonType")

    country = Column(Integer, ForeignKey('cl_country_list.code'))
    country_ref = relationship("ClCountryList")

    gender = Column(Integer, ForeignKey('cl_gender.code'))
    gender_ref = relationship("ClGender")

    bank = Column(Integer, ForeignKey('cl_bank.code'))
    bank_ref = relationship("ClBank")

    address_au_level1 = Column(String, ForeignKey('au_level1.code'))
    au_level1_ref = relationship("AuLevel1")

    address_au_level2 = Column(String, ForeignKey('au_level2.code'))
    au_level2_ref = relationship("AuLevel2")

    address_au_level3 = Column(String, ForeignKey('au_level3.code'))
    au_level3_ref = relationship("AuLevel3")

    address_au_khoroolol = Column(Integer, ForeignKey('au_khoroolol.fid'))
    address_au_khoroolol_ref = relationship("AuKhoroolol")

    fees = relationship("CtFee",
                        backref='person_ref',
                        lazy='dynamic',
                        cascade="all, delete, delete-orphan")
    archived_fees = relationship("CtArchivedFee",
                                 backref='person_ref',
                                 lazy='dynamic',
                                 cascade="all, delete, delete-orphan")

    taxes = relationship("CtTaxAndPrice",
                         backref='person_ref',
                         lazy='dynamic',
                         cascade="all, delete, delete-orphan")
    archived_taxes = relationship("CtArchivedTaxAndPrice",
                                  backref='person_ref',
                                  lazy='dynamic',
                                  cascade="all, delete, delete-orphan")
Example #29
0
class ValidationResult(Base):

    __tablename__ = 'qc_validation_results'

    id_seq = Sequence("qc_validation_results_id_seq", metadata=Base.metadata)
    id = Column(Integer, primary_key=True, server_default=id_seq.next_value())

    node_id = Column(String(64), nullable=False)
    submitter_id = Column(String(128), nullable=False)

    error_type = Column(String(128), nullable=True, default='', index=True)

    # from Node.label
    node_type = Column(String(128), nullable=False, index=True)
    message = Column(Text, nullable=False)

    severity = Column(SEVERITY, nullable=True, index=True)

    related_nodes = Column(JSONB, nullable=True)

    test_run_id = Column(Integer,
                         ForeignKey("qc_test_runs.id"),
                         nullable=False,
                         primary_key=True)
    test_run = relationship('TestRun', back_populates='test_results')

    date_created = Column(
        DateTime(timezone=True),
        nullable=False,
        server_default=text('now()'),
    )

    last_updated = Column(
        DateTime(timezone=True),
        nullable=False,
        server_default=text('now()'),
    )

    @validates("severity")
    def validate_severity(self, key, severity):

        if not severity:
            return severity

        # ensure all upper cases
        severity = severity.upper()

        # map fatal to failed
        if severity == "FATAL":
            severity = "CRITICAL"

        return severity

    def __repr__(self):
        return "<ValidationResult(id=%d, error='%s')>" % (self.id,
                                                          self.error_type)

    def to_json(self):
        return {
            'node_id': self.node_id,
            'submitter_id': self.submitter_id,
            'error': self.error_type,
            'severity': self.severity,
            'message': self.message,
            'related_nodes': self.related_nodes,
            'date_created': self.date_created.isoformat(),
            'last_updated': self.last_updated.isoformat()
        }
Example #30
0
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('msg_id_seq'), primary_key=True, nullable=False)
    username = Column(String(40), nullable=False, unique=True)
    password = Column(String(64), nullable=False)
    coins = Column(Integer, default=0)
Example #31
0
from sqlalchemy import Column, ForeignKey, Integer, String, Sequence
from sqlalchemy.orm import relationship

id_seq = Sequence('id_seq')
from .database import Base


class User(Base):
    __tablename__ = "users"

    uid = Column(String(255), primary_key=True, unique=True, index=True)
    email = Column(String(100), unique=True, index=True)
    projects = relationship("Project", back_populates="owner")


class Project(Base):
    __tablename__ = "projects"
    id = Column(Integer, id_seq, primary_key=True, unique=True, index=True)
    account = Column(String(100), index=True)
    repository = Column(String(100), index=True)
    owner_id = Column(String(255), ForeignKey("users.uid"))

    owner = relationship("User", back_populates="projects")
Example #32
0
    def test_py_vs_server_default_detection(self):
        def has_(name, *wanted):
            slots = [
                'default', 'onupdate', 'server_default', 'server_onupdate'
            ]
            col = tbl.c[name]
            for slot in wanted:
                slots.remove(slot)
                assert getattr(col, slot) is not None, getattr(col, slot)
            for slot in slots:
                assert getattr(col, slot) is None, getattr(col, slot)

        tbl = t
        has_('col1', 'default')
        has_('col2', 'default', 'onupdate')
        has_('col3', 'default', 'onupdate')
        has_('col4', 'server_default')
        has_('col5', 'server_default')
        has_('col6', 'default', 'onupdate')
        has_('boolcol1', 'default')
        has_('boolcol2', 'default')
        has_('col7', 'default', 'onupdate')
        has_('col8', 'default', 'onupdate')
        has_('col9', 'default', 'server_default')

        ColumnDefault, DefaultClause = sa.ColumnDefault, sa.DefaultClause

        t2 = Table(
            't2', MetaData(), Column('col1', Integer, Sequence('foo')),
            Column('col2',
                   Integer,
                   default=Sequence('foo'),
                   server_default='y'),
            Column('col3', Integer, Sequence('foo'), server_default='x'),
            Column('col4', Integer, ColumnDefault('x'), DefaultClause('y')),
            Column('col4', Integer, ColumnDefault('x'), DefaultClause('y'),
                   DefaultClause('y', for_update=True)),
            Column('col5',
                   Integer,
                   ColumnDefault('x'),
                   DefaultClause('y'),
                   onupdate='z'),
            Column('col6',
                   Integer,
                   ColumnDefault('x'),
                   server_default='y',
                   onupdate='z'),
            Column('col7',
                   Integer,
                   default='x',
                   server_default='y',
                   onupdate='z'),
            Column('col8',
                   Integer,
                   server_onupdate='u',
                   default='x',
                   server_default='y',
                   onupdate='z'))
        tbl = t2
        has_('col1', 'default')
        has_('col2', 'default', 'server_default')
        has_('col3', 'default', 'server_default')
        has_('col4', 'default', 'server_default', 'server_onupdate')
        has_('col5', 'default', 'server_default', 'onupdate')
        has_('col6', 'default', 'server_default', 'onupdate')
        has_('col7', 'default', 'server_default', 'onupdate')
        has_('col8', 'default', 'server_default', 'onupdate',
             'server_onupdate')
Example #33
0
class MorphologicalParserBackup(Base):

    __tablename__ = 'morphologicalparserbackup'

    def __repr__(self):
        return '<MorphologicalParserBackup (%s)>' % self.id

    id = Column(Integer, Sequence('morphologicalparserbackup_seq_id', optional=True), primary_key=True)
    morphologicalparser_id = Column(Integer)
    UUID = Column(Unicode(36))
    name = Column(Unicode(255))
    description = Column(UnicodeText)
    phonology = Column(UnicodeText)
    morphology = Column(UnicodeText)
    language_model = Column(UnicodeText)
    enterer = Column(UnicodeText)
    modifier = Column(UnicodeText)
    datetime_entered = Column(DateTime)
    datetime_modified = Column(DateTime, default=now)
    compile_succeeded = Column(Boolean, default=False)
    compile_message = Column(Unicode(255))
    compile_attempt = Column(Unicode(36)) # a UUID

    def vivify(self, morphological_parser_dict):
        """The vivify method gives life to a morphology_backup by specifying its
        attributes using the to-be-backed-up morphology (morphological_parser_dict) and the
        modifier (current user).  The relational attributes of the
        to-be-backed-up morphology are converted into (truncated) JSON objects.

        """
        self.UUID = morphological_parser_dict['UUID']
        self.morphologicalparser_id = morphological_parser_dict['id']
        self.name = morphological_parser_dict['name']
        self.description = morphological_parser_dict['description']
        self.phonology = unicode(json.dumps(morphological_parser_dict['phonology']))
        self.morphology = unicode(json.dumps(morphological_parser_dict['morphology']))
        self.language_model = unicode(json.dumps(morphological_parser_dict['language_model']))
        self.enterer = unicode(json.dumps(morphological_parser_dict['enterer']))
        self.modifier = unicode(json.dumps(morphological_parser_dict['modifier']))
        self.datetime_entered = morphological_parser_dict['datetime_entered']
        self.datetime_modified = morphological_parser_dict['datetime_modified']
        self.compile_succeeded = morphological_parser_dict['compile_succeeded']
        self.compile_message = morphological_parser_dict['compile_message']
        self.compile_attempt = morphological_parser_dict['compile_attempt']

    def get_dict(self):
        return {
            'id': self.id,
            'morphologicalparser_id': self.morphologicalparser_id,
            'UUID': self.UUID,
            'name': self.name,
            'phonology': self.get_mini_dict_for(self.phonology),
            'morphology': self.get_mini_dict_for(self.morphology),
            'language_model': self.get_mini_dict_for(self.language_model),
            'description': self.description,
            'enterer': self.get_mini_user_dict(self.enterer),
            'modifier': self.get_mini_user_dict(self.modifier),
            'datetime_entered': self.datetime_entered,
            'datetime_modified': self.datetime_modified,
            'compile_succeeded': self.compile_succeeded,
            'compile_message': self.compile_message,
            'compile_attempt': self.compile_attempt
        }
Example #34
0
 def test_execute(self, connection):
     s = Sequence("my_sequence")
     self._assert_seq_result(connection.execute(s))