149 self.cw_linkto[(rtype, role)] = linkedto |
149 self.cw_linkto[(rtype, role)] = linkedto |
150 return linkedto |
150 return linkedto |
151 |
151 |
152 # server side helpers ##################################################### |
152 # server side helpers ##################################################### |
153 |
153 |
154 # XXX: store a reference to the AnyEntity class since it is hijacked in goa |
154 def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
155 # configuration and we need the actual reference to avoid infinite loops |
155 """function to ease basic configuration of an entity class ORM. Basic usage |
156 # in mro |
156 is: |
157 ANYENTITY = AnyEntity |
|
158 |
157 |
159 def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
158 .. sourcecode:: python |
160 if pclass is ANYENTITY: |
159 |
161 pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes |
160 class MyEntity(AnyEntity): |
|
161 |
|
162 fetch_attrs, cw_fetch_order = fetch_config(['attr1', 'attr2']) |
|
163 # uncomment line below if you want the same sorting for 'unrelated' entities |
|
164 # cw_fetch_unrelated_order = cw_fetch_order |
|
165 |
|
166 Using this, when using ORM methods retrieving this type of entity, 'attr1' |
|
167 and 'attr2' will be automatically prefetched and results will be sorted on |
|
168 'attr1' ascending (ie the first attribute in the list). |
|
169 |
|
170 This function will automatically add to fetched attributes those defined in |
|
171 parent class given using the `pclass` argument. |
|
172 |
|
173 Also, You can use `mainattr` and `order` argument to have a different |
|
174 sorting. |
|
175 """ |
162 if pclass is not None: |
176 if pclass is not None: |
163 fetchattrs += pclass.fetch_attrs |
177 fetchattrs += pclass.fetch_attrs |
164 if mainattr is None: |
178 if mainattr is None: |
165 mainattr = fetchattrs[0] |
179 mainattr = fetchattrs[0] |
166 @classmethod |
180 @classmethod |
167 def fetch_order(cls, attr, var): |
181 def fetch_order(cls, select, attr, var): |
168 if attr == mainattr: |
182 if attr == mainattr: |
169 return '%s %s' % (var, order) |
183 select.add_sort_var(var, order=='ASC') |
170 return None |
|
171 return fetchattrs, fetch_order |
184 return fetchattrs, fetch_order |