Changing manage_addLexicon from ZCTextIndex to allow a list of dictionaries to be entered in the elements parameter

Bug #143809 reported by Josef Meile
2
Affects Status Importance Assigned to Milestone
Zope 2
Opinion
Wishlist
Unassigned

Bug Description

The manage_addLexicon function from the ZCTextIndex module works perfectly when
submiting from the ZMI, where you see entries like:

<input type="hidden" name="elements.group:records" ...>
<select name="elements.name:records">
  ...
</select>

If you want to call the manage_addLexicon from python, then you will have to define a
dummy class an assign the required attributes to it. Like this I guess (not tested):

class Dummy
  pass

records = []
record = Dummy()
record.group = 'some_group'
record.name = 'some_name'
records.append(record)

record = Dummy()
record.group = 'other_group'
record.name = 'other_name'
records.append(record)
manage_addLexicon(catalog,'Lexicon',elements=records)

I think it would be much cleaner to do this:
records = [{'name':'some_name', 'group':'some_group'},
           {'name':'other_name', 'group':'other_group'}]
manage_addLexicon(catalog,'Lexicon',elements=records)

So you don't need to define the ugly dummy class. I though it would work, but inside the
manage_addLexicon you use 'hasattr', which doesn't work with a dictionary. Here I just
can thing about two solutions:

1) Inside the manage_addLexicon you could wrapp the 'hasattr', ie: if elements is a list
of dictionaries, then use:
hasattrWrapper = dict.has_key

otherwise use
hasAttrWrapper = hasattr

For the el_record.group and el_record.name calls you could use:
getAttrWrapper = getattr

and for the dictionary:
getAttrWrapper = dict.get

2) The second solution would be to do another method (ie: manage_addLexicon_python),
which will use from python and where you define the ugly dummy class and assign it the
entered list of dictionaries. Although I preffer the first solution, I think this would
be also an alternative, for the user isn't forced to define such Dummy class him/herself

Regards
Josef

Revision history for this message
Josef Meile (jmeile) wrote :

Ok, I rethink the solucion with the dummy class and it would be better:

class Dummy:
  def __init__(self, group, name):
    self.group = group
    self.name = name

records = []
record = Dummy('some_group','some_name')
records.append(record)

record = Dummy('other_group','other_name')
records.append(record)
manage_addLexicon(catalog,'Lexicon',elements=records)

It looks better than my initial solution. However, I don't see
nice that each zope developer interested in using the manage_addLexicon has to define this class.

Revision history for this message
Josef Meile (jmeile) wrote :

Ok, I found this is related to:
http://www.zope.org/Collectors/Zope/1688

So, I guess the bugfix is similar.

Revision history for this message
Josef Meile (jmeile) wrote :

Finally, for the records, the real workaround instead of creating the dummy class is importing the "record class" from ZPublisher.HTTPRequest and set the attributes to your 'extra' record. The Dummy class approach seems not to work in all cases.

from ZPublisher.HTTPRequest import record
extra = record()
setattr(extra,'attribute','value')

This is really difficult to figure out. Anyway, I still the dictionary approach is better.

Changed in zope2:
importance: Medium → Wishlist
Changed in zope2:
status: New → Opinion
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.