コード例 #1
0
ファイル: roleable.py プロジェクト: zidarsk8/ggrc-core
 def _access_control_list(cls):  # pylint: disable=no-self-argument
   """access_control_list"""
   return db.relationship(
       'AccessControlList',
       primaryjoin=lambda: and_(
           remote(AccessControlList.object_id) == cls.id,
           remote(AccessControlList.object_type) == cls.__name__),
       foreign_keys='AccessControlList.object_id',
       backref='{0}_object'.format(cls.__name__),
       cascade='all, delete-orphan')
コード例 #2
0
 def _join_fixture_o2m_composite_selfref_func_annotated(self, **kw):
     return relationships.JoinCondition(
         self.composite_selfref,
         self.composite_selfref,
         self.composite_selfref,
         self.composite_selfref,
         primaryjoin=and_(
             remote(self.composite_selfref.c.group_id) == func.foo(self.composite_selfref.c.group_id),
             remote(self.composite_selfref.c.parent_id) == self.composite_selfref.c.id,
         ),
         **kw
     )
コード例 #3
0
ファイル: generic_fk.py プロジェクト: aburan28/sqlalchemy
def setup_listener(mapper, class_):
    name = class_.__name__
    discriminator = name.lower()
    class_.addresses = relationship(Address,
                        primaryjoin=and_(
                                        class_.id == foreign(remote(Address.parent_id)),
                                        Address.discriminator == discriminator
                                    ),
                        backref=backref(
                                "parent_%s" % discriminator,
                                primaryjoin=remote(class_.id) == foreign(Address.parent_id)
                                )
                        )
    @event.listens_for(class_.addresses, "append")
    def append_address(target, value, initiator):
        value.discriminator = discriminator
コード例 #4
0
ファイル: schema.py プロジェクト: chrisbura/pythoncad-legacy
def setup_listener(mapper, class_):
    name = class_.__name__
    content_type = name.lower()
    # Reverse relationship (ie. Segment.entities)
    class_.entities = relationship(Entity,
        primaryjoin = and_(
            class_.id == foreign(remote(Entity.object_id)),
            Entity.content_type == content_type
        ),
        backref = backref(
            'parent_{0}'.format(content_type),
            primaryjoin=remote(class_.id) == foreign(Entity.object_id)
        )
    )
    @event.listens_for(class_.entities, 'append')
    def append_entity(target, value, initiator):
        value.content_type = content_type
コード例 #5
0
    def rel_text_entry(self):
        """
Relation to TextEntry

:return: (object) SQLAlchemy relationship description
:since:  v0.2.00
        """

        return relationship(TextEntry, primaryjoin = (foreign(self.id) == remote(TextEntry.id)), uselist = False)
コード例 #6
0
ファイル: assessment.py プロジェクト: zidarsk8/ggrc-core
  def object_level_definitions(cls):  # pylint: disable=no-self-argument
    """Set up a backref so that we can create an object level custom
       attribute definition without the need to do a flush to get the
       assessment id.

      This is used in the relate_ca method in hooks/assessment.py.
    """
    return db.relationship(
        'CustomAttributeDefinition',
        primaryjoin=lambda: and_(
            remote(CustomAttributeDefinition.definition_id) == cls.id,
            remote(CustomAttributeDefinition.definition_type) == "assessment"),
        foreign_keys=[
            CustomAttributeDefinition.definition_id,
            CustomAttributeDefinition.definition_type
        ],
        backref='assessment_definition',
        cascade='all, delete-orphan')
コード例 #7
0
ファイル: test_rel_fn.py プロジェクト: zzzap/sqlalchemy
 def _join_fixture_purely_single_o2m(self, **kw):
     return relationships.JoinCondition(
         self.purely_single_col,
         self.purely_single_col,
         self.purely_single_col,
         self.purely_single_col,
         support_sync=False,
         primaryjoin=self.purely_single_col.c.path.like(
             remote(foreign(self.purely_single_col.c.path.concat('%')))))
コード例 #8
0
    def rel_acl(self):
        """
Relation to AclEntry

:return: (object) SQLAlchemy relationship description
:since:  v0.2.00
        """

        return relationship(AclEntry, primaryjoin = (foreign(self.id) == remote(AclEntry.owned_id)), uselist = True)
コード例 #9
0
ファイル: assessment.py プロジェクト: vitfri/ggrc-core
    def object_level_definitions(self):
        """Set up a backref so that we can create an object level custom
       attribute definition without the need to do a flush to get the
       assessment id.

      This is used in the relate_ca method in hooks/assessment.py.
    """
        return db.relationship('CustomAttributeDefinition',
                               primaryjoin=lambda: and_(
                                   remote(CustomAttributeDefinition.
                                          definition_id) == Assessment.id,
                                   remote(CustomAttributeDefinition.
                                          definition_type) == "assessment"),
                               foreign_keys=[
                                   CustomAttributeDefinition.definition_id,
                                   CustomAttributeDefinition.definition_type
                               ],
                               backref='assessment_definition',
                               cascade='all, delete-orphan')
コード例 #10
0
ファイル: media.py プロジェクト: guptaarth87/weasyl
class UserMediaLink(Base, _LinkMixin):
    __table__ = tables.user_media_links

    _identity = 'userid'
    _linkname = 'user_links'

    user = relationship(
        Profile, backref='media_links',
        primaryjoin=foreign(__table__.c.userid) == remote(Profile.userid))
    media_item = relationship(MediaItem, backref='user_links')
コード例 #11
0
class Node(Base):
    __tablename__ = "node"

    id = Column(Integer, primary_key=True, autoincrement=False)
    path = Column(String(500), nullable=False, index=True)

    # To find the descendants of this node, we look for nodes whose path
    # starts with this node's path.
    descendants = relationship(
        "Node",
        viewonly=True,
        order_by=path,
        primaryjoin=remote(foreign(path)).like(path.concat(".%")),
    )

    # Finding the ancestors is a little bit trickier. We need to create a fake
    # secondary table since this behaves like a many-to-many join.
    secondary = select(
        id.label("id"),
        func.unnest(
            cast(
                func.string_to_array(func.regexp_replace(path, r"\.?\d+$", ""),
                                     "."),
                ARRAY(Integer),
            )).label("ancestor_id"),
    ).alias()
    ancestors = relationship(
        "Node",
        viewonly=True,
        secondary=secondary,
        primaryjoin=id == secondary.c.id,
        secondaryjoin=secondary.c.ancestor_id == id,
        order_by=path,
    )

    @property
    def depth(self):
        return len(self.path.split(".")) - 1

    def __repr__(self):
        return "Node(id={})".format(self.id)

    def __str__(self):
        root_depth = self.depth
        s = [str(self.id)]
        s.extend(((n.depth - root_depth) * "  " + str(n.id))
                 for n in self.descendants)
        return "\n".join(s)

    def move_to(self, new_parent):
        new_path = new_parent.path + "." + str(self.id)
        for n in self.descendants:
            n.path = new_path + n.path[len(self.path):]
        self.path = new_path
コード例 #12
0
ファイル: table.py プロジェクト: phenom-films/dayu_database
def setup_thumbnail_listener(mapper, _class):
    '''
    利用sqlalchemy 的监听机制,实现对 继承InfoMixin 的class 动态添加.infos 属性
    :param mapper:
    :param _class: 继承InfoMixin 的class
    :return:
    '''
    hook_type = _class.__name__.lower()
    _class.thumbnail = relationship('THUMBNAIL',
                                    primaryjoin=and_(_class.id == foreign(remote(THUMBNAIL.hook_id)),
                                                     THUMBNAIL.hook_table == hook_type),
                                    uselist=False,
                                    backref=backref('hook_{0}'.format(hook_type),
                                                    primaryjoin=remote(_class.id) == foreign(
                                                            THUMBNAIL.hook_id))
                                    )

    @listens_for(_class.thumbnail, 'set')
    def set_thumbnail(target, value, old_value, initiator):
        value.hook_table = hook_type
コード例 #13
0
ファイル: test_rel_fn.py プロジェクト: BY-jk/sqlalchemy
 def _join_fixture_purely_single_m2o(self, **kw):
     return relationships.JoinCondition(
         self.purely_single_col,
         self.purely_single_col,
         self.purely_single_col,
         self.purely_single_col,
         support_sync=False,
         primaryjoin=remote(self.purely_single_col.c.path).like(
             foreign(self.purely_single_col.c.path.concat("%"))
         ),
     )
コード例 #14
0
ファイル: table.py プロジェクト: phenom-films/dayu_database
def setup_info_listener(mapper, _class):
    '''
    利用sqlalchemy 的监听机制,实现对 继承InfoMixin 的class 动态添加.infos 属性
    :param mapper:
    :param _class: 继承InfoMixin 的class
    :return:
    '''
    hook_type = _class.__name__.lower()
    _class.infos = relationship('INFO',
                                primaryjoin=and_(_class.id == foreign(remote(INFO.hook_id)),
                                                 INFO.hook_table == hook_type),
                                order_by='INFO.name',
                                lazy='dynamic',
                                backref=backref('hook_{0}'.format(hook_type),
                                                primaryjoin=remote(_class.id) == foreign(INFO.hook_id))
                                )

    @listens_for(_class.infos, 'append')
    def append_infos(target, value, initiator):
        value.hook_table = hook_type
class OneDimensionPoint(Base, sam.ExtendedBase):
    __tablename__ = "one_dimension_point"

    coordinator = sa.Column(sa.Integer, primary_key=True)

    neighbors = relationship(
        "OneDimensionPoint",
        primaryjoin=sa.func.abs(remote(coordinator) - foreign(coordinator)) <=
        2,
        # primaryjoin="func.abs(OneDimensionPoint.coordinator - foreign(OneDimensionPoint.coordinator)) <= 2",
        viewonly=True,
    )
コード例 #16
0
ファイル: table.py プロジェクト: phenom-films/dayu_database
def setup_symbol_listener(mapper, _class):
    '''
    利用sqlalchemy 的监听机制,实现为 继承SymbolMixin 的class 动态添加symbols 属性
    :param mapper:
    :param _class: 继承 SymbolMixin 的class
    :return:
    '''
    symbol_type = _class.__name__.lower()
    _class.symbols = \
        relationship('SYMBOL',
                     primaryjoin=and_(_class.id == foreign(remote(SYMBOL.origin_id)),
                                      SYMBOL.origin_table == symbol_type),
                     order_by=('SYMBOL.origin_table, SYMBOL.origin_id'),
                     lazy='dynamic',
                     backref=backref('origin_{0}'.format(symbol_type),
                                     primaryjoin=remote(_class.id) == foreign(SYMBOL.origin_id))
                     )

    @listens_for(_class.symbols, 'append')
    def append_symbols(target, value, initiator):
        value.origin_table = symbol_type
コード例 #17
0
ファイル: models.py プロジェクト: vyadzmak/OTA.Api
class ProductCategories(Base):
    __tablename__ = 'product_categories'
    id = Column('id', Integer, primary_key=True)
    name = Column('name', String(64))
    short_description = Column('short_description', String(500))
    full_description = Column('full_description', String(1000))
    images = Column('images', postgresql.ARRAY(Integer))
    user_creator_id = Column('user_creator_id', ForeignKey('users.id'))
    creation_date = Column('creation_date',
                           DateTime,
                           default=datetime.datetime.now(
                               datetime.timezone.utc))
    is_lock = Column('is_lock', Boolean, default=False)
    parent_category_id = Column('parent_category_id', Integer)
    default_image_id = Column('default_image_id', ForeignKey('attachments.id'))
    is_delete = Column('is_delete', Boolean, default=False)

    default_image_data = relationship(
        'Attachments', backref="default_image_data_product_categories")
    child_categories = relationship(
        "ProductCategories",
        primaryjoin=and_(
            remote(parent_category_id) == foreign(id),
            foreign(is_delete) == False),
        uselist=True)

    internal_products_count = column_property(
        select([func.count(Products.id)]). \
            where(and_(Products.category_id == id, Products.is_delete == False, Products.not_show_in_catalog == False)). \
            correlate_except(Products)
    )

    @property
    def internal_categories_count(self):
        return object_session(self). \
            scalar(
            select([func.count(ProductCategories.id)]). \
                where(and_(ProductCategories.parent_category_id == self.id, ProductCategories.is_delete == False))
        )

    @property
    def child_products_count(self):
        child_ids = [x.id for x in self.child_categories]
        return object_session(self). \
            scalar(
            select([func.count(Products.id)]) \
            .where(and_(Products.category_id.in_(child_ids), Products.is_delete == False, Products.not_show_in_catalog == False))
        )

    def __init__(self, *args):
        db_tranformer.transform_constructor_params(self, args)
        self.creation_date = datetime.datetime.now(datetime.timezone.utc)
        self.is_lock = False
コード例 #18
0
ファイル: models.py プロジェクト: pybender/cloudml-ui
def setup_listener(mapper, class_):
    import_handler_type = class_.TYPE
    class_.test_import_handler = relationship(
        Model,
        primaryjoin=and_(
            class_.id == foreign(remote(Model.test_import_handler_id)),
            Model.test_import_handler_type == import_handler_type),
        backref=backref("rel_test_import_handler_%s" % import_handler_type,
                        primaryjoin=(remote(class_.id) == foreign(
                            Model.test_import_handler_id))))
    class_.train_import_handler = relationship(
        Model,
        primaryjoin=and_(
            class_.id == foreign(remote(Model.train_import_handler_id)),
            Model.train_import_handler_type == import_handler_type),
        backref=backref("rel_train_import_handler_%s" % import_handler_type,
                        primaryjoin=(remote(class_.id) == foreign(
                            Model.train_import_handler_id))))
    class_.transformers = relationship(
        Transformer,
        primaryjoin=and_(
            class_.id == foreign(remote(Transformer.train_import_handler_id)),
            Transformer.train_import_handler_type == import_handler_type),
        backref=backref("rel_train_import_handler_%s" % import_handler_type,
                        primaryjoin=(remote(class_.id) == foreign(
                            Transformer.train_import_handler_id))))
コード例 #19
0
ファイル: model.py プロジェクト: coecms/dusql
class Inode(Base):
    __tablename__ = "dusql_inode"

    basename = sa.Column("basename", sa.Text, primary_key=True)
    inode = sa.Column("inode", sa.BigInteger)
    device = sa.Column("device", sa.BigInteger, primary_key=True)
    mode = sa.Column("mode", sa.Integer)
    uid = sa.Column("uid", sa.Integer)
    gid = sa.Column("gid", sa.Integer)
    size = sa.Column("size", sa.BigInteger)
    mtime = sa.Column("mtime", sa.Float)
    scan_time = sa.Column("scan_time", sa.Float)
    root_inode = sa.Column("root_inode", sa.BigInteger,
                           sa.ForeignKey("dusql_inode.inode"))
    parent_inode = sa.Column(
        "parent_inode",
        sa.BigInteger,
        sa.ForeignKey("dusql_inode.inode"),
        primary_key=True,
    )

    root = orm.relationship(
        "Inode",
        primaryjoin=sa.and_(
            orm.remote(inode) == orm.foreign(root_inode),
            orm.remote(device) == orm.foreign(device),
        ),
    )
    parent = orm.relationship(
        "Inode",
        primaryjoin=sa.and_(
            orm.remote(inode) == orm.foreign(parent_inode),
            orm.remote(device) == orm.foreign(device),
        ),
    )

    path = orm.column_property(sa.func.dusql_path_func(parent_inode, device,
                                                       basename),
                               deferred=True)
コード例 #20
0
class UserMediaLink(Base, _LinkMixin):
    __table__ = tables.user_media_links

    _identity = 'userid'

    user = relationship(Profile,
                        primaryjoin=foreign(__table__.c.userid) == remote(
                            Profile.userid))
    media_item = relationship(MediaItem)

    @classmethod
    def get_media_query(cls):
        return cls.query.options(joinedload(cls.media_item), )
コード例 #21
0
 def _join_fixture_remote_local_multiple_ref(self, **kw):
     fn = lambda a, b: ((a == b) | (b == a))
     return relationships.JoinCondition(
         self.selfref, self.selfref,
         self.selfref, self.selfref,
         support_sync=False,
         primaryjoin=fn(
             # we're putting a do-nothing annotation on
             # "a" so that the left/right is preserved;
             # annotation vs. non seems to affect __eq__ behavior
             self.selfref.c.sid._annotate({"foo": "bar"}),
             foreign(remote(self.selfref.c.sid)))
     )
class HostEntry(Base, sam.ExtendedBase):
    __tablename__ = "host_entry"

    id = sa.Column(sa.Integer, primary_key=True)
    ip_address = sa.Column(INET)
    content = sa.Column(sa.String(50))

    # relationship() using explicit foreign() and remote() annotations
    # in lieu (代替) of separate arguments
    parent_host = relationship(
        "HostEntry",
        primaryjoin=remote(ip_address) == sa.cast(foreign(content), INET),
    )
コード例 #23
0
def _create_orm_relation(rel):
    """
    ref_class_attr_name is based on the name of data_class if not provided. It adds an "s"
    ref_class.ref_class_attr_name = rel

    >>> print('{}.{} = rel'.format(rel.ref_class.__name__, rel.ref_class_attr_name))
    """
    orm_relation = relationship(
        rel.data_class,
        primaryjoin=and_(
            rel.ref_class.id == foreign(
                remote(
                    getattr(rel.data_class,
                            "{}_id".format(rel.data_class_attr)))),
            getattr(rel.data_class, "{}_type".format(
                rel.data_class_attr)) == rel.ref_class_name),
        backref=backref(rel.data_class_alchemy_attr,
                        primaryjoin=remote(rel.ref_class.id) == foreign(
                            getattr(rel.data_class,
                                    "{}_id".format(rel.data_class_attr)))))

    setattr(rel.ref_class, rel.ref_class_attr_name, orm_relation)
コード例 #24
0
ファイル: models.py プロジェクト: jaym93/gtplaces
class Category(db.Model):
    """
    DB model representing a category associated with a building
    """
    __tablename__ = 'categories'

    cat_id = db.Column('cat_id', Integer, primary_key=True, autoincrement=True)
    # TODO: DB table doesn't but should have foriegn key- lying here to SQL Alchemy
    b_id = db.Column('b_id', Text, ForeignKey("buildings.b_id"), nullable=False)
    cat_name = db.Column('cat_name', Text, nullable=False)

    # TODO: update when the table has a proper foriegn key constraint
    # building relationship - this is a more complex as there is not a proper foreign key and the b_id column types differ
    buildings = db.relationship('Building', backref=('categories'), primaryjoin=cast(remote(Building.b_id), Text) == foreign(b_id))
コード例 #25
0
ファイル: models.py プロジェクト: bookmarktools/quarchive
class Tag(Base):
    __tablename__ = "tags"
    __tableargs__ = (CheckConstraint("tag_name ~ '^[-a-z0-9]+$'"), )

    # Presumably 4bn tags is enough
    tag_id = Column(satypes.Integer, primary_key=True, autoincrement=True)
    tag_name = Column(satypes.String(length=40),
                      nullable=False,
                      index=True,
                      unique=True)

    bookmarks_objs: "RelationshipProperty[SQLABookmark]" = relationship(
        SQLABookmark,
        backref="tag_objs",
        secondary=BookmarkTag.__table__,
        primaryjoin=tag_id == BookmarkTag.tag_id,
        secondaryjoin=and_(
            foreign(BookmarkTag.__table__.c.url_uuid) == remote(
                SQLABookmark.url_uuid),
            foreign(BookmarkTag.__table__.c.user_uuid) == remote(
                SQLABookmark.user_uuid),
        ),
    )
コード例 #26
0
 def _join_fixture_remote_local_multiple_ref(self, **kw):
     fn = lambda a, b: ((a == b) | (b == a))
     return relationships.JoinCondition(
         self.selfref,
         self.selfref,
         self.selfref,
         self.selfref,
         support_sync=False,
         primaryjoin=fn(
             # we're putting a do-nothing annotation on
             # "a" so that the left/right is preserved;
             # annotation vs. non seems to affect __eq__ behavior
             self.selfref.c.sid._annotate({"foo": "bar"}),
             foreign(remote(self.selfref.c.sid))))
コード例 #27
0
class Node(Base):
    __tablename__ = 'node'

    id = Column(Integer, primary_key=True, autoincrement=False)
    path = Column(String(500), nullable=False, index=True)

    # 想要找到这个node的后代,我们需要搜索以当前node的path为path前缀的node
    descendants = relationship('Node',
                               viewonly=True,
                               order_by=path,
                               primaryjoin=remote(foreign(path)).like(
                                   path.concat(".%")))

    # 想要找到这个node的祖先有点复杂。
    # 我们需要创建一个伪secondary表,因为这个行为有些像many-to-many
    secondary = select([
        id.label('id'),
        func.unnest(
            cast(
                func.string_to_array(func.regexp_replace(path, r"\.?\d+$",
                                                         ""), "."),
                ARRAY(Integer))).label('ancestor_id')
    ]).alias()

    ancestor = relationship("Node",
                            viewonly=True,
                            secondary=secondary,
                            primaryjoin=id == secondary.c.id,
                            secondaryjoin=secondary.c.ancestor_id == id,
                            order_by=path)

    @property
    def depth(self):
        return len(self.path.split(".")) - 1

    def __repr__(self):
        return "Node(id={})".format(self.id)

    def __str__(self):
        root_depth = self.depth
        s = [str(self.id)]
        s.extend(((n.depth - root_depth) * "  " + str(n.id))
                 for n in self.descendants)
        return "\n".join(s)

    def move_to(self, new_parent):
        new_path = new_parent + "." + str(self.id)
        for n in self.descendants:
            n.path = new_path + n.path[len(self.path):]
        self.path = new_path
コード例 #28
0
def setup_listener(mapper, class_):
    name = class_.__name__
    discriminator = name.lower()

    # 添加关系
    class_.images = db.relationship(
        Image,
        primaryjoin=and_(
            class_.id == foreign(remote(Image.object_id)),
            Image.discriminator == discriminator
        ),
        backref=backref(
            'object_%s' % discriminator,
            # uselist=False,
            primaryjoin=remote(class_.id) == foreign(Image.object_id)
        ),
        # 当该数据删除后,关联的图片也删除
        cascade='all'
    )

    @listens_for(class_.images, 'append')
    def append_image(target, value, initiator):
        value.discriminator = discriminator
コード例 #29
0
 def register_o2m(self, parent, child_infos):
     col_name, child, inverse = child_infos
     if inverse == 'create_uid':
         return
     print("Tentative O2M {} {} {}".format(col_name, child, inverse))
     # foreign_keys = [getattr(self[child], inverse)] if inverse and type(getattr(self[child], inverse)) != AssociationProxy else None
     # setattr(self[parent], col_name, relationship(self[child], foreign_keys=foreign_keys))
     fkey = getattr(self[child], inverse) if inverse and type(
         getattr(self[child], inverse)) != AssociationProxy else None
     if not fkey:
         return
     setattr(
         self[parent], col_name,
         relationship(self[child],
                      primaryjoin=foreign(self[parent].id) == remote(fkey),
                      viewonly=True))
コード例 #30
0
class Access(Base):
    __tablename__ = 'imp_access'
    id = id_pkey()
    description = Column(String)
    building_shortname = Column('building', String)
    building: Building = relationship(
        Building,
        primaryjoin=foreign(building_shortname) == remote(Building.short_name),
        uselist=False)
    floor = Column(String)
    flat = Column(String)
    room = Column(String)
    switch = Column(String)
    port = Column(String)
    account = relationship('Account',
                           primaryjoin='Account.access_id == Access.id')
コード例 #31
0
ファイル: attribute.py プロジェクト: ryncsn/metadash
    def build_relationship(self):
        attribute = self
        entities = ([
            entity for entity in EntityRegistry.values()
            if attribute.ref_name not in entity.attribute_models.keys()
        ])
        for model in entities:
            model.attribute_models[attribute.ref_name] = attribute
            parentname = _get_alias_dict(model.__dict__)
            attribute.entity_models.append(parentname)

            # TODO: autocache = attribute.__autocache__
            cacheable = attribute.__cacheable__
            if cacheable:
                model.__cacheable_attributes__.add(attribute.ref_name)

            backref_name = attribute.__backref_name__
            proxy_name = attribute.__proxy_name__
            collector = attribute.__collector__
            outline = attribute.__outline__
            unique_attribute = attribute.__unique_attr__

            relationship = db.relationship(
                model,
                primaryjoin=foreign(attribute.entity_uuid) == remote(
                    model.uuid),
                backref=backref(backref_name,
                                uselist=not unique_attribute,
                                collection_class=collector,
                                cascade="all, delete-orphan"),
                uselist=False,
                single_parent=True,
            )

            # will call _add_attribute of SQLAlchmey
            setattr(attribute, parentname, relationship)

            if outline:
                if hasattr(collector, '__proxy_args__'):
                    setattr(
                        model, proxy_name,
                        association_proxy(backref_name, outline,
                                          **collector.__proxy_args__))
                else:
                    setattr(model, proxy_name,
                            association_proxy(backref_name, outline))
コード例 #32
0
class PermissionScopeRetail(db.Model, BaseModel):
    __tablename__ = 'permission_scope_detail'
    __table_args__ = {'extend_existing': True, 'schema': db_schema}

    id = Column(INTEGER(11), primary_key=True)
    permission_key = Column(ForeignKey(db_schema + '.permission.key'),
                            index=True)
    permission_scope_key = Column(ForeignKey(db_schema +
                                             '.permission_scope.key'),
                                  index=True)

    permission_scope = relationship(
        PermissionScope,
        primaryjoin=PermissionScope.key == foreign(permission_scope_key))
    permission = relationship(Permission,
                              primaryjoin=remote(
                                  Permission.key) == foreign(permission_key))
コード例 #33
0
ファイル: models.py プロジェクト: shibutd/ozon-parser
class Category(db.Model):
    __tablename__ = 'categories'

    name = db.Column(db.String(128), nullable=False)
    slug = db.Column(db.String(128), primary_key=True)
    url = db.Column(db.String(128), unique=True, nullable=False)
    path = db.Column(LtreeType, nullable=False)
    parent = db.relationship(
        'Category',
        primaryjoin=remote(path) == foreign(db.func.subpath(path, 0, -1)),
        backref='children',
        sync_backref=False,
        viewonly=True
    )

    __table_args__ = (
        db.Index('ix_categories_path', path, postgresql_using='gist'),
    )

    def __init__(self, *args, **kwargs):
        if 'slug' not in kwargs:
            url = kwargs.get('url')
            slug = re.search(r'/category/(\D+)-\d+', url).group(1)
            slug = slug.replace('-', '_')
            kwargs['slug'] = slug
        else:
            slug = kwargs.get('slug')
        ltree_slug = Ltree(slug)
        parent = kwargs.get('parent')
        kwargs['path'] = ltree_slug if not parent else parent.path + ltree_slug
        super().__init__(*args, **kwargs)

    @classmethod
    def is_parent(cls):
        return db.func.nlevel(cls.path) == 1

    @classmethod
    def has_no_children(cls):
        c2 = aliased(Category)
        return ~exists().where(
            and_(cls.path.ancestor_of(c2.path), cls.path != c2.path))

    def __repr__(self):
        return '<Category %r>' % self.name
コード例 #34
0
ファイル: models.py プロジェクト: jaym93/gtplaces
class Tag(db.Model):
    """
    DB model representing a tag associated with a building
    """
    __tablename__ = 'tags'
    __table_args__ = (UniqueConstraint('b_id', 'tag_name'),)

    tag_id = db.Column('tag_id', Integer, primary_key=True, autoincrement=True)
    # TODO: DB table doesn't but should have foriegn key- lying here to SQL Alchemy
    b_id = db.Column('b_id', Text, ForeignKey("buildings.b_id"), nullable=False)
    tag_name = db.Column('tag_name', Text, nullable=False)
    gtuser = db.Column('gtuser', Text, nullable=False)
    auth = db.Column('auth', Integer, default=0)
    times_tag = db.Column('times_tag', Integer, default=1, nullable=False)
    flag_users = db.Column('flag_users', Text, default='', nullable=False)
    times_flagged = db.Column('times_flagged', Integer, default=0, nullable=False)

    # TODO: update when the table has a proper foreign key constraint
    # building relationship - this is a more complex as there is not a proper foreign key and the b_id column types differ
    building = db.relationship('Building', backref=('tags'), primaryjoin=cast(remote(Building.b_id), Text) == foreign(b_id))
コード例 #35
0
class Model(db.Model):
    __tablename__ = 'models_v2'
    __table_args__ = (default_table_args('model'))

    id = db.Column(db.Integer, primary_key=True, comment='id')
    name = db.Column(db.String(255), comment='model_name')
    version = db.Column(db.Integer, comment='model_version')
    parent_id = db.Column(db.String(255), comment='parent_id')
    job_name = db.Column(db.String(255), comment='job_name')
    type = db.Column(db.Enum(ModelType, native_enum=False), comment='type')
    state = db.Column(db.Enum(ModelState, native_enum=False), comment='state')
    create_time = db.Column(db.DateTime(timezone=True), comment='create_time')
    params = db.Column(db.Text(), comment='params')
    metrics = db.Column(db.Text(), comment='metrics')
    output_base_dir = db.Column(db.String(255),
                                comment='model checkpoint/export path')
    parent = db.relationship('Model',
                             primaryjoin=remote(id) == foreign(parent_id),
                             backref='children')
    job = db.relationship('Job', primaryjoin=Job.name == foreign(job_name))

    def __init__(self):
        self.create_time = datetime.now()
        self.version = 0

    def commit(self):
        db.session.add(self)
        db.session.commit()

    def get_eval_model(self):
        """
        Get the evaluation model inherited model

        Returns:
             a list of evaluation model
        """
        eval_model = [
            child for child in self.children if child.type in
            [ModelType.NN_EVALUATION, ModelType.TREE_EVALUATION]
        ]
        return eval_model
コード例 #36
0
ファイル: ballot.py プロジェクト: eric-wieser/caius-rooms
class BallotSeason(Base):
    """ A year in which a ballot occurs """
    __tablename__ = 'ballot_seasons'

    year = Column(Integer, primary_key=True)

    previous = relationship(lambda: BallotSeason,
                            primaryjoin=lambda: remote(BallotSeason.year) ==
                            foreign(BallotSeason.year) - 1,
                            uselist=False,
                            viewonly=True)

    def __repr__(self):
        return "BallotSeason(year={})".format(self.year)

    def __str__(self):
        return u"{} \u2012 {}".format(self.year, self.year + 1)

    if sys.version_info.major < 3:
        __unicode__ = __str__
        del __str__
コード例 #37
0
class UserOldId(TimestampMixin, db.Model):
    __tablename__ = 'useroldid'
    __bind_key__ = 'lastuser'
    query_class = CoasterQuery

    # userid here is NOT a foreign key since it has to continue to exist
    # even if the User record is removed
    userid = db.Column(db.String(22), nullable=False, primary_key=True)
    olduser = db.relationship(User, primaryjoin=foreign(userid) == remote(User.userid),
        backref=db.backref('oldid', uselist=False))
    user_id = db.Column(None, db.ForeignKey('user.id'), nullable=False)
    user = db.relationship(User, primaryjoin=user_id == User.id,
        backref=db.backref('oldids', cascade='all, delete-orphan'))

    def __repr__(self):
        return u'<UserOldId {userid} of {user}>'.format(
            userid=self.userid, user=repr(self.user)[1:-1])

    @classmethod
    def get(cls, userid):
        return cls.query.filter_by(userid=userid).one_or_none()
コード例 #38
0
class Tags(Base):
    """Used a materialized path pattern to generate a tag list.

    We only need to be able to move tags around and to find tags that
    are higher in the hierachy, so we can skip some of the
    functionality that is normally implemented.

    see http://docs.sqlalchemy.org/en/rel_1_0/_modules/examples/materialized_paths/materialized_paths.html

    """
    __tablename__ = 'tags'

    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)
    icon = Column(BLOB)
    path = Column(String(500), index=True)

    # To find the descendants of this node, we look for nodes whose path
    # starts with this node's path.
    childs = relationship("Tags",
                          viewonly=True,
                          order_by=path,
                          primaryjoin=remote(foreign(path)).like(
                              path.concat(".%")))

    def __repr__(self):
        return "Tag: {} ({})".format(self.name, self.id)

    def move_to(self, new_parent):
        if new_parent is not None:
            new_path = new_parent.path + "." + str(self.id)
            for n in self.childs:
                n.path = new_path + n.path[len(self.path):]
            self.path = new_path
        else:
            self.path = str(self.id)

    def all_tags(self):
        """This tag and all its childrens"""
        return self.childs + [self]
コード例 #39
0
class Lock(Base):
    __tablename__ = "pg_locks"

    locktype = Column(Text, primary_key=True)
    database = Column(Oid, primary_key=True)
    relation = Column(Oid, ForeignKey("pg_class.oid"), primary_key=True)
    page = Column(Integer, primary_key=True)
    tuple = Column(SmallInteger, primary_key=True)
    virtualxid = Column(Text, primary_key=True)
    transactionid = Column(Text, primary_key=True)  # type - xid
    classid = Column(Oid, ForeignKey("pg_class.oid"), primary_key=True)
    objid = Column(Oid, primary_key=True)  # any OID column
    objsubid = Column(SmallInteger, primary_key=True)
    virtualtransaction = Column(Text, primary_key=True)
    pid = Column(Integer, primary_key=True)
    mode = Column(Text, primary_key=True)
    granted = Column(Boolean)
    fastpath = Column(Boolean)

    relation_obj = relationship(Class,
                                backref="relation_locks",
                                foreign_keys=relation)
    class_obj = relationship(Class,
                             backref="class_locks",
                             foreign_keys=classid)
    activity = relationship(Activity,
                            backref="locks",
                            primaryjoin=(foreign(pid) == remote(Activity.pid)))

    def tuple_data(self):
        """Get the locked row, for tuple locks"""

        if self.locktype != "tuple":
            raise ValueError("Not a tuple lock")

        return self._sa_instance_state.session.execute(
            "SELECT * FROM {}.{} WHERE ctid = '({},{})'".format(
                self.relation_obj.namespace.nspname, self.relation_obj.relname,
                self.page, self.tuple)).first()
コード例 #40
0
    Column('visibility', types.Unicode, default=u'visible'),
    Column('abuse_status',
           types.Integer,
           default=AbuseStatus.unmoderated.value),
)

meta.mapper(
    Issue,
    issue_table,
    properties={
        'user': relation(
            model.User,
            backref=backref('issues',
                            cascade='all, delete-orphan',
                            single_parent=True),
            primaryjoin=foreign(issue_table.c.user_id) == remote(User.id),
            uselist=False
        ),
        'assignee': relation(
            model.User,
            backref=backref('resolved_issues',
                            cascade='all'),
            primaryjoin=foreign(issue_table.c.assignee_id) == remote(User.id)
        ),
        'dataset': relation(
            model.Package,
            backref=backref('issues',
                            cascade='all, delete-orphan',
                            single_parent=True),
            primaryjoin=foreign(issue_table.c.dataset_id) == remote(Package.id),
            uselist=False