# Allow to replace some standard classes

**URL:** https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105
**Category:** Feature
**Created:** [April 2, 2016, 5:14pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105 "2016-04-02T17:14:05Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [April 2, 2016, 5:14pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/1 "2016-04-02T17:14:05Z")

</div>

## Rational

Some usages need to replace some standard classes, for example this [Redis Cache](https://github.com/coopengo/trytond/commit/0f8bc1b2ef88dd2ce35a1cbb09d8c3a5871f2635) implementation.  
It is better for everyone that users don’t need to fork Tryton to extend/customize some parts.

## Proposal

The proposal is inspired by the [logging.config](https://docs.python.org/2/library/logging.config.html) module where it is possible to define alternative classes.  
We could add similar behaviour for some classes where it makes sense to be able to replace them. The configuration could be extended to support new options. For example, we could add under the `cache` section an option `class` that will replace the `trytond.cache.Cache` class with this kind of pattern:

```
class Cache(object):
    ...

if config.get('cache', 'class'):
    from logging.config import _resolve
    Cache = _resolve(config.get('cache', 'class')) 

```

The possible classes to be replaced:

- `trytond.cache.Cache`
- `trytond.model.*`
- `trytond.model.fields.*`
- `trytond.report.Report`
- `trytond.wizard.*`

## Implemetation

cache: [Allow to override cache implementation (#5962) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue5962)  
filestore: [Allow to reuse the attachment file storage outside of attachment (#5686) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue5686)

---

<div class="post-metadata">

### Author: ![nicoe](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/nicoe/32/2880_2.png) [@nicoe](https://discuss.tryton.org/u/nicoe)
#### Post date: [April 3, 2016, 6:33pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/2 "2016-04-03T18:33:10Z")

</div>

Indeed there is a common need to override some of the classes Tryton defines.  
Moreover the [importlib](https://docs.python.org/3/library/importlib.html) features of python3 might render this feature quite elegant.

---

<div class="post-metadata">

### Author: ![JCavallo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@JCavallo](https://discuss.tryton.org/u/JCavallo)
#### Post date: [April 4, 2016, 9:13am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/3 "2016-04-04T09:13:28Z")

</div>

I was wondering if it would be possible to add a similar behaviour for trytond.pool.Pool

I had to do this :

> <https://github.com/coopengo/trytond/commit/e68b71fa0f4b8426d6353461098cba8043c04ff2>

because I wanted in a module to run some validation code once the pool was fully loaded.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [April 4, 2016, 5:31pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/4 "2016-04-04T17:31:19Z")

</div>

I don’t see what kind of valid needs would require such thing.

---

<div class="post-metadata">

### Author: ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)
#### Post date: [April 7, 2016, 9:50am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/5 "2016-04-07T09:50:21Z")

</div>

We also need that. In our case it is for BaBI module where we register “dynamic” models and need some way to initialize them.

I must say, however, that in our case, we need a way to create such “dynamic” model in one of the trytond instances and also ensure it is registered when needed in other trytond instances of the same database.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [April 7, 2016, 10:14am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/6 "2016-04-07T10:14:13Z")

</div>

When I read such requirements, it lights up a big ⚠ in my mind because this is a tendency that leads by finally coding into the application. It also raises the complexity in such way that it will generate Heisenberg bugs.  
Indeed, I think the same level of flexibility could be achieved using a `table_query` feature.

---

<div class="post-metadata">

### Author: ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)
#### Post date: [April 7, 2016, 10:30am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/7 "2016-04-07T10:30:44Z")

</div>

We did it this way because it was simple: trytond is in charge of creating the database structure, for example (babi materializes the report).

To achieve something similar using table\_query we should probably use the context to know which report the user is querying and make fields\_get (and fields\_view\_get) as well as table\_query return the required information. But we should create the table ourselves (probably not a big issue, though).

---

<div class="post-metadata">

### Author: ![aneolf](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aneolf](https://discuss.tryton.org/u/aneolf)
#### Post date: [April 7, 2016, 10:34am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/8 "2016-04-07T10:34:20Z")

</div>

> [@ced](#):
>
> Indeed, I think the same level of flexibility could be achieved using a table\_query feature.

But, IMHO even although the BABI module of NaN·tic were using table\_query feature, it is needed to setup the model with a dynamic model in order to show views and reports, isn’t it?

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [April 7, 2016, 10:57am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/9 "2016-04-07T10:57:13Z")

</div>

In some way, I don’t really care what will be needed to do. What I care about is to prevent to have bad design in Tryton. And for me, dynamic model is bad design. Tryton tries to be only declarative based because we believed it was more robust and have less side-effect (see: `PYSON`, states, `on_change_with` etc.).  
So if we come back to the [original request](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/3) which was to be able to have a post Pool.init, I don’t see any valid usage other than having dynamic models which is against the paradigm of Tryton.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [July 5, 2016, 5:11pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/10 "2016-07-05T17:11:24Z")

</div>

[issue5686](https://bugs.tryton.org/issue5686) implements this design for the FileStore class.  
I think it is a good example to follow.

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [October 20, 2016, 3:35pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/11 "2016-10-20T15:35:13Z")

</div>

[issue5962](https://bugs.tryton.org/issue5962) Added the implementation for the cache backend

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [October 24, 2016, 10:52pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/12 "2016-10-24T22:52:00Z")

</div>

> [@ced](#):
>
> trytond.model.\*  
> trytond.model.fields.\*  
> trytond.report.Report  
> trytond.wizard.\*

I think for those classes a better design should be found because usually this feature will be needed by modules. So we need a way in a module to register/replace base classes. This could be done by registering in a special register those subclasses and to force all standard classes to inherit from.

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [October 25, 2016, 7:15am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/13 "2016-10-25T07:15:13Z")

</div>

> [@ced](#):
>
> trytond.model.\*  
> trytond.model.fields.\*  
> trytond.report.Report  
> trytond.wizard.\*
> 
> I think for those classes a better design should be found because usually this feature will be needed by modules. So we need a way in a module to register/replace base classes. This could be done by registering in a special register those subclasses and to force all standard classes to inherit from.

I think this part should be solved by [issue4735](https://bugs.tryton.org/issue4735)

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [October 25, 2016, 7:51am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/14 "2016-10-25T07:51:29Z")

</div>

Probably but we should have a clean design and I think the extension should be conditional to being a subclass of one of the base classes.

---

<div class="post-metadata">

### Author: ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)
#### Post date: [October 28, 2016, 5:01pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/15 "2016-10-28T17:01:36Z")

</div>

I think the same or similar mechanism could be used to override the dispatcher or allowing at least to override the way exceptions are managed. This would allow, for example, using sentry without the need to patch trytond.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [October 28, 2016, 5:03pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/16 "2016-10-28T17:03:57Z")

</div>

For me, sentry should be managed with a specific logging Handler.

---

<div class="post-metadata">

### Author: ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)
#### Post date: [October 28, 2016, 5:17pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/17 "2016-10-28T17:17:02Z")

</div>

The idea is not only send it to sentry (or whatever service) but send the user a different message. That is replace a standard exception by a raise\_user\_error with custom message.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [October 28, 2016, 9:08pm UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/18 "2016-10-28T21:08:21Z")

</div>

I think this will be best managed with a configuration feature than a override/replacement.

---

<div class="post-metadata">

### Author: ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)
#### Post date: [October 29, 2016, 9:01am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/19 "2016-10-29T09:01:25Z")

</div>

I don’t see how this could be managed with a configuration feature. For example, with sentry what you do is send a message such as “There was an application error. We’ve given tracked the error and given it the ID xxxxxxx. If you want you can phone us and give this ID as reference.”. So this ID would be given by sentry.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [October 29, 2016, 9:05am UTC](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105/20 "2016-10-29T09:05:33Z")

</div>

So the solution will need to define a generic API.

[Next page](https://discuss.tryton.org/t/allow-to-replace-some-standard-classes/105.md?page=2)
