Hello everyone!

I have an application with a "BaseVisibilizer"  class that is base
class for two other classes:

class BaseVisibilizer(declarativeBase):
        __tablename__ = "base_visibilizers"
        _polymorphicIdentity = Column("polymorphic_identity", String(20),
key="polymorphicIdentity")
        __mapper_args__ = {
                'polymorphic_on': _polymorphicIdentity,
                'polymorphic_identity': None
        }

        def __init__(self):
                super(BaseVisibilizer, self).__init__()
---------------------------------

This is the base class for my User and UserGroup classes: both classes
can be used to control the visibility of certain items.

class UserGroup(BaseVisibilizer.BaseVisibilizer):
        __tablename__ = "user_groups"
        _id = Column("id", Integer,
ForeignKey(BaseVisibilizer.BaseVisibilizer.id), primary_key=True)

        __mapper_args__ = {
                'polymorphic_identity': 'UserGroup',
                'inherit_condition': _id == BaseVisibilizer.BaseVisibilizer._id,
        }
        
        _name = Column("name", String(50), nullable=False)

----------------------------

class User(BaseVisibilizer.BaseVisibilizer):
        __tablename__ = "users"
        _id = Column("id", Integer,
ForeignKey(BaseVisibilizer.BaseVisibilizer.id), primary_key=True)

        __mapper_args__ = {
                'polymorphic_identity': 'User',
                'inherit_condition': _id == BaseVisibilizer.BaseVisibilizer._id,
        }
        _firstName = Column("first_name", String(50), key="fistName")
        _lastName = Column("last_name", String(50), key="lastName")
        _userName = Column("user_name", String(50), unique=True, nullable=False)

------------------------------

In many of the other classes of my system I have a relationship that
can contain "BaseVisibilizers" (Users or UserGroups).

class Whatever(declarativeBase):
        allowedVisibilizers = relationship("BaseVisibilizer",
                secondary="intermediate_whatevers_to_base_visibilizers",
                collection_class=set)
-------------------------
So in the instances of Whatever, the set allowedVisibilizers can
contain instances of User of UserGroup (because both are
BaseVisibilizers). If the logged user or the UserGroup the logged user
belongs to is among the "allowedVisibilizers" , that user will be able
to access (or "see") that instance of Whatever.

At a certain point, I would like to create a method in the Whatever
class that would give me only the allowed visibilizers that are
actually a UserGroup (discarding the Users) but I haven't been able to
do it.

I've tried several things, but obviously, not the right one
(subqueries, joins of "allowedvisibilizers"...):

What I'd like is something like:
class Whatever(declarativeBase):
        [ . . . ]
        def getAllowedUserGroups(self):
                session = Database.Session()
                query = 
session.query(UserGroup.UserGroup).filter(UserGroup.UserGroup.in_(self.allowedVisibilizers))
                return query.all()

(but obviously, UserGroup is a class and doesn't have an in_)

Of course, I could always get all the "allowedVisibilizers" and remove
the Users from the returned value, but I was trying to make it a
little bit optimized than that and avoid unnecessary calls to the
database.

Any hint will be very appreciated. Thank you in advance!

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to