Ignore:
Timestamp:
01/18/12 11:59:51 (8 years ago)
Author:
pcosquer
Message:

3D branch: merge changes from trunk (rev [661])

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3D/openPLM/plmapp/query_parser.py

    r460 r662  
    3030    pass 
    3131 
     32 
     33def convert_number(query, qualifier): 
     34    """ If query represents a number, replaces it with an OR query built with 
     35        several formatting of the number: for example, it replaces 51 with 51 
     36        or 051 or 0051... so that "51" matches "part-0051". 
     37 
     38        If *query* does not represent a number, it returns a simple 
     39        SQ(qualifier -> query) object. 
     40    """ 
     41    sq = SQ() 
     42    if query.isdigit(): 
     43        or_ = SQ() 
     44        numbers = ["0" * x + query for x in range(10)] 
     45        for nb in numbers: 
     46            or_ |= SQ(**{ qualifier : nb }) 
     47        sq &= or_ 
     48    else: 
     49        sq &= SQ(**{ qualifier : query }) 
     50    return sq 
     51 
    3252class Text(List): 
    33      
     53    
    3454    def to_SQ(self): 
    3555        if len(self) == 2: 
     
    4161        text = text.strip().lower() 
    4262        filters = {} 
     63        # here we replace a number with an OR query built with several formatting 
     64        # of the number: 
     65        # for example, we replace 51 with 51 or 051 or 0051... 
     66        sq = SQ() 
    4367        if text.endswith("*"): 
    44             sq = SQ() 
    4568            text = text.rstrip("*") 
    4669            items = split(text) 
    4770            for item in items[:-1]: 
    48                 sq &= SQ(**{ qualifier: item }) 
     71                sq &= convert_number(item, qualifier) 
    4972            suffix = "*" if qualifier == "content" else "" 
    5073            sq &= SQ(**{ qualifier + "__startswith" : items[-1]+suffix}) 
    51             return sq 
    5274        else: 
    53             return SQ(**{ qualifier : text }) 
     75            sq = convert_number(text, qualifier) 
     76        return sq 
    5477 
    5578class Not(List): 
Note: See TracChangeset for help on using the changeset viewer.