Thanks for the tip.
now I've modified the code like this and it works.
-----------
    @property
    def permissions_str(self):
        permissions = sorted([p.permission_name for p in self.permissions])
        p_str = u', '.join(permissions)
        return p_str
------------

I'm confused if this is a model code or presentation logic. I'm using this property in lots of places so it seems model code is the natural place for me.

This is one of the controller code I use that:
--------

        rows = [{
                'cell':[
                            group.group_id,
                            group.group_name,
                            group.display_name,
                            group.permissions_str,
                            group.users_str,
                        ],
                'id':str(group.group_id)
                } for group in q_group]

--------
I'm returning this rows variable as json to an ajax javascript (actually, its jqgrid) to show records in a grid. if I put this code in the controller function, that above code would be more complicated.

And again, the above code that builds rovs variable, I have another model object property: users_str and the code to build that is :

-----------

class Group(DeclarativeBase):
    """
    Group definition for :mod:`repoze.what`.

    Only the ``group_name`` column is required by :mod:`repoze.what`.

    """

    __tablename__ = 'tg_group'

    #{ Columns

    group_id = Column(Integer, autoincrement=True, primary_key=True)
    group_name = Column(Unicode(16), unique=True, nullable=False)
    display_name = Column(Unicode(255))
    created = Column(DateTime, default=datetime.now)

    #{ Relations

users = relation('User', secondary=user_group_table, backref='groups', order_by='User.user_name') #permissions = relation('Permission', secondary=group_permission_table, backref='groups', order_by='Permission.permission_name')

    @property
    def permissions_str(self):
        permissions = sorted([p.permission_name for p in self.permissions])
        p_str = u', '.join(permissions)
        return p_str

    @property
    def users_str(self):
        users = [u.user_name for u in self.users]
        u_str = u', '.join(users)
        return u_str
-----------

There is a users relation in the Group object, I've added order_by parameter to relation and I can get correct sort order with this code. But the relation for permissions is in the Permissions object so I can only define order by to get Permission.groups. How can I define ordery by for both side of a relation?


Diez B. Roggisch yazmış:
On Monday, June 14, 2010 18:13:13 Timuçin Kızılay wrote:
I'm trying this on version tg2.b1, The auth.py file I use is the default
one created by quickstart.

I'm trying to modify auth.py model file like this:

in the object:
class Group(DeclarativeBase):
I'm trying to add a property to get a string of permission names in that
group.  I've added this function to achieve this.
-----------
    @property
    def permissions_str(self):
        permissions = [p.permission_name for p in self.permissions]
        p_str = u', '.join(permissions)
        return p_str
-----------

it works ok, gives me a string of permissions but I want them to be
sorted by permission_name field. what should I add to achieve this?

Use the builtin "sorted"?

sorted(["c", "a", "b"])

Just on a side note, to me it looks as if you are confusing model code with presentation logic here. Such kind of stuff shouldn't go to the model. Put it into the controller, if you need it only once. Or create some helper/wrapper function or object if you need it severeal times.

Diez


--
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en.

Reply via email to