tools.py 756 B

12345678910111213141516171819202122232425262728
  1. def parse_like_term(term):
  2. """
  3. Parse search term into (operation, term) tuple. Recognizes operators
  4. in the beginning of the search term.
  5. * = case insensitive (can precede other operators)
  6. ^ = starts with
  7. = = exact
  8. :param term:
  9. Search term
  10. """
  11. case_insensitive = term.startswith('*')
  12. if case_insensitive:
  13. term = term[1:]
  14. # apply operators
  15. if term.startswith('^'):
  16. oper = 'startswith'
  17. term = term[1:]
  18. elif term.startswith('='):
  19. oper = 'exact'
  20. term = term[1:]
  21. else:
  22. oper = 'contains'
  23. # add case insensitive flag
  24. if case_insensitive:
  25. oper = 'i' + oper
  26. return oper, term