cubicweb #1409785 API to set and test "flag" on entity [open]


There is several case where it's useful to mark that an involved entity is temporary in a specific state. This marker can affect selection of hooks or change the behaviour of piece of code.

  • For performing maintenance operation via the web UI of the cubicweb-ctl shell which require specific.
  • When you process the output of special operation done with javascript in the browser.
  • During a complex execution flow involving several hooks

We are using the concept extensively in a project to we enforce the size of image and generate thumbnail of different size.

  • Fresh assignment to must trigger hook that generate thumbnail to check the dimension for resize the image if necessary.
  • Resize image are assigned to This assignment must not trigger the hook for thumbnail and resize.
  • The user can choose to crop a part of the original image to the right dimension. The resize hook must not be called but the thumbnail must be generated

Such flag are usually achieve by putting the entity eid into a set stored in the transaction_data when the entity enter this state and removing it when the entity leave this state.

However, write access to transaction_data are not available at the webserver side. Making a generic write access to transaction_data available at the web-server side is hard and error prone because of the pyro proxy.

Getting a specific API to read and write flag data is much more easy. By hiding access to transaction_data and reentrant logic, A specific API clarify the all part of the code.


the "flag" API will have 3 components

  • A cw_flag context manager on every entity. The use of a context manager ensure that no flag stay active by mistake:

    with entity.cw_flag('babar'):
        print 'some code where the entity is flagged'
    print 'entity not flagged anymore'
  • A cw_flagged method to test if an entity have a given flag enabled:

  • A has_flag selector for easy selection of AppObject:

    class BabarHook(Hook):
        __select__ = Hook.__select__ & has_flag('babar')

The cw_flag and cw_flagged method could be available thought a selector.


The cw_flag and cw_flagged methods are added to AnyEntity (or an adapter)

An EntityFlag class will handle the context management part, with necessary reentrant capability.

The Request -> Connection -> Repository chain needs to implement three methods flag_entity, unflag_entity, flagged_entity to read and write flag information. Those methods shall not be used directly. They will be call through the cw_flag and cw_flagged of AnyEntity.

Flag key will get necessary prefix not to conflict with other content of transaction_data.

See attached python files for current implementation in our project.

done in<not specified>
load left0.500
closed by<not specified>