[PATCH 4 of 5 saem_ref v2] Use a jQuery widget to make some lists sortable

Denis Laxalde denis.laxalde at logilab.fr
Fri Oct 5 14:58:12 CEST 2018


Frank Bessou a écrit :
> 
> 
> On 05/10/2018 14:17, Denis Laxalde wrote:
>> Frank Bessou a écrit :
>>> # HG changeset patch
>>> # User Frank Bessou <frank.bessou at logilab.fr>
>>> # Date 1537867860 -7200
>>> #      Tue Sep 25 11:31:00 2018 +0200
>>> # Node ID c4c90465753714453b5766ce43afba59322e770c
>>> # Parent  b59c34f39b95a7c1a84889d9c2b4ab38821546a9
>>> # Available At http://hg.logilab.org/review/cubes/saem_ref
>>> #              hg pull http://hg.logilab.org/review/cubes/saem_ref -r
>>> c4c904657537
>>> # EXP-Topic CONSEJIRA-578-584
>>> Use a jQuery widget to make some lists sortable
>>>
>>> This jQuery widget calls a new ajax controller which uses ISortable
>>> properties to sort elements in database.
>>>
>>> We add a 'collection' property to Sortable adapter which returns a list
>>> the list of entities which are in the same collection (related to a
>>> common parent through the same relation).
>>>
>>> We make RelatedEntitiesListView use a listvid property to select which
>>> view must be used to display a given list.
>>> The SortableListView adds js corresponding to sorting and adds the
>>> needed attributes on the generated markup.
>>>
>>> diff --git a/cubicweb_saem_ref/data/cubes.saem_ref.js
>>> b/cubicweb_saem_ref/data/cubes.saem_ref.js
>>> --- a/cubicweb_saem_ref/data/cubes.saem_ref.js
>>> +++ b/cubicweb_saem_ref/data/cubes.saem_ref.js
>>> @@ -160,7 +160,18 @@ saem = {
>>>         canMoveTo : function(moved_node, target_node, position){
>>>           return true;
>>> -    }
>>> +    },
>>> +
>>> +    makeSortable: function (listid, rtype) {
>>> +        $('#' + listid).sortable({
>>> +            axis: 'y',
>>> +            stop: ({target}) => {
>>> +                const eids = $(target).children().map((i, e) =>
>>
>> This file is not transpiled, perhaps "var" should be used instead of
>> "const"?
>>
>>> +                    $(e).attr('data-eid')).get();
>>> +                asyncRemoteExec('sort_relation_targets', rtype, eids);
>>> +            }
>>> +        });
>>> +    },
>>>     };
>>>   diff --git a/cubicweb_saem_ref/entities/__init__.py
>>> b/cubicweb_saem_ref/entities/__init__.py
>>> --- a/cubicweb_saem_ref/entities/__init__.py
>>> +++ b/cubicweb_saem_ref/entities/__init__.py
>>> @@ -17,6 +17,7 @@
>>>     from logilab.common.decorators import monkeypatch
>>>   +from cubicweb import ProgrammingError
>>>   from cubicweb.view import Adapter, EntityAdapter
>>>   from cubicweb.predicates import match_kwargs, relation_possible,
>>> is_instance
>>>   from cubicweb.entities import AnyEntity, fetch_config, authobjs, lib
>>> @@ -235,10 +236,39 @@ class ISortable(EntityAdapter):
>>>       __regid__ = 'ISortable'
>>>       __abstract__ = True
>>>   +    role = 'subject'
>>> +
>>> +    @property
>>> +    def rtype(self):
>>> +        """Return the relation making this entity a component of a
>>> composite
>>> +        entity"""
>>> +        raise NotImplementedError
>>
>> Why making "rtype" a property and "role" a class attribute? Usually, the
>> later (simpler) implementation is chosen.
> 
> This is because I don't really know the conventional way to declare that
> a class attribute should be overriden in subclasses.
> Do you recommend to just replace it by an attribute with a None value.

Yes, that's the "convention" :)

> 
>>
>>> +
>>> +    @property
>>> +    def _reverse_role(self):
>>> +        if self.role == 'subject':
>>> +            return 'object'
>>> +        elif self.role == 'object':
>>> +            return 'subject'
>>> +        else:
>>> +            raise ProgrammingError('Invalid role: ' + self.role)
>>
>> ProgrammingError is for "errors that are related to the database's
>> operation" (quoting the docstring).
>>
>> Also, there's a neg_role() function in cubicweb that just does this.
> 
> Thanks :)
> 
>>
>>
>> Otherwise, this series looks good to me. I now need to test this in
>> the UI.
>>
>>> +
>>> +    @property
>>> +    def parent(self):
>>> +        return self.entity.related(self.rtype, self.role,
>>> entities=True)[0]
>>> +
>>> +    @property
>>> +    def collection(self):
>>> +        return self.parent.related(self.rtype, self._reverse_role,
>>> entities=True)
>>> +
>>>     class AuthorityRecordFunctionSortable(ISortable):
>>>       __select__ = is_instance('AgentFunction')
>>>   +    rtype = 'function_agent'
>>> +
>>>     class AuthorityRecordMandateSortable(ISortable):
>>>       __select__ = is_instance('Mandate')
>>> +
>>> +    rtype = 'mandate_agent'
>>
> 



More information about the saem-devel mailing list