Changeset 2951 in products


Ignore:
Timestamp:
Nov 1, 2010 1:20:51 PM (13 years ago)
Author:
mylan
Message:

#228: Added example of new filter creation in doctests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.plonegooglesitemaps/branches/blacklist/quintagroup/plonegooglesitemaps/filters.txt

    r2950 r2951  
    245245 
    246246 
     247Creation own filters 
     248==================== 
     249 
     250Suppouse we want to create own blackout filter,  
     251which behave like id-filter, but with some differencies. 
     252Our fitler has following format: 
     253 
     254  (+|-)<filtered id> 
     255 
     256  - when 1st sign "+" then only objects with <filtered id> 
     257    must leave after filetering, 
     258  - if 1st sign is "-" or  all objects with <filtered id> must be 
     259    filtered-out (like default id filter) 
     260 
     261You need create new IBlckoutFilter multi-adapter, 
     262and register it with unique name. 
     263 
     264    >>> from zope.component import adapts 
     265    >>> from zope.interface import Interface, implements 
     266    >>> from zope.publisher.interfaces.browser import IBrowserRequest 
     267    >>> from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilter 
     268    >>> class SignedIdFilter(object): 
     269    ...     adapts(Interface, IBrowserRequest) 
     270    ...     implements(IBlackoutFilter) 
     271    ...     def __init__(self, context, request): 
     272    ...         self.context = context 
     273    ...         self.request = request 
     274    ...     def filterOut(self, fdata, fargs): 
     275    ...         sign = fargs[0] 
     276    ...         fid = fargs[1:] 
     277    ...         if sign == "+": 
     278    ...             return [b for b in fdata if b.getId==fid] 
     279    ...         elif sign == "-": 
     280    ...             return [b for b in fdata if b.getId!=fid] 
     281    ...         return fdata 
     282 
     283 
     284Now register this new filter as named multiadapter ... 
     285 
     286    >>> from zope.component import provideAdapter 
     287    >>> provideAdapter(SignedIdFilter,  
     288    ...                name=u'signedid') 
     289 
     290So thet's all what neede to add new filter. 
     291No test if newly added filter really take into consideration. 
     292 
     293Check if white filtering ("+" prefix) work correctly. 
     294Go to the edit form of the sitemap and add "signedid:+doc1" 
     295to the "Blackout entries" field. 
     296 
     297    >>> browser.open(smedit_url) 
     298    >>> filtercontrol = browser.getControl("Blackout entries") 
     299    >>> filtercontrol.value = "signedid:+doc1" 
     300    >>> browser.getControl("Save").click() 
     301    >>> signedid_filter_content = browser.contents 
     302 
     303As result - only objects with "doc1" id must present in the sitemap. 
     304 
     305    >>> signedid_filter_res = reloc.findall(signedid_filter_content) 
     306    >>> len(signedid_filter_res) == 2 
     307    True 
     308    >>> signedid_filter_res.sort() 
     309    >>> print "\n".join(signedid_filter_res) 
     310    /Members/test_user_1_/doc1 
     311    /doc1 
     312 
     313 
     314An for the last - check black filtering ("-" prefix) is working. 
     315Go to the edit form of the sitemap and add "signedid:-doc1" 
     316to the "Blackout entries" field. 
     317 
     318    >>> browser.open(smedit_url) 
     319    >>> filtercontrol = browser.getControl("Blackout entries") 
     320    >>> filtercontrol.value = "signedid:-doc1" 
     321    >>> browser.getControl("Save").click() 
     322    >>> signedid_filter_content = browser.contents 
     323 
     324As result - all except objects with "doc1" id must present in the sitemap. 
     325 
     326    >>> signedid_filter_res = reloc.findall(signedid_filter_content) 
     327    >>> len(signedid_filter_res) == 3 
     328    True 
     329    >>> signedid_filter_res.sort() 
     330    >>> print "\n".join(signedid_filter_res) 
     331    /Members/test_user_1_/doc2 
     332    /doc2 
     333    /front-page 
Note: See TracChangeset for help on using the changeset viewer.