Dear all,

I'm developing a website aimed at handling tournaments' results and
subscriptions.
One subscription is bound to one user and one tournament.

subscriptions_table = Table('SUBSCRIPTIONS', metadata,
                      ...
                      Column('tournament_id', Integer,
ForeignKey('TOURNAMENTS.id')),
                      Column('user_id', Integer, ForeignKey('USERS.id')),

Here are the mappers (I'm not sure I actually need the backref) :

mapper(Tournament, tournaments_table, properties={
    "subscriptions": relationship(Subscription, backref="tournament")
})

mapper(Subscription, subscriptions_table, properties={
    "user": relationship(User)
})

When the user clicks "Subscribe" on the page, the model is checking if the
user already subscribed to the current tournament or not. If yes,
SUBSCRIPTIONS.UPDATE should be issued, otherwise SUBSCRIPTIONS.INSERT

As you can see, it's a very classical scenario.

Here's how I implemented the "subscribe" method... but I don't like it at
all :

class Tournament(Base):

    def subscribe(self, user, status):

        # Works, but dirty ! Why should I manually query RESULTS since I
have access to self.subscriptions ?
        # Should I manually look the user in [subscription .user for
subscription in self.subscriptions] ?
        current_subscription = orm.query(Result).filter(Result.tournament ==
self).filter(Result.user == user).first()

        if current_subscription :
            current_subscription.status = status
        else :
            self.subscriptions.append(Subscription(user, status))

        # Commit / rollback logic


What do you think ? Since self.subscriptions is already bound, how should I
properly filter it by user ?

Thanks very much for your help !
Franck

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

Reply via email to