query.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. #
  3. # cms.py - simple WSGI/Python based CMS script
  4. #
  5. # Copyright (C) 2011-2019 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #from cms.cython_support cimport * #@cy
  20. from cms.exception import *
  21. from cms.util import * #+cimport
  22. __all__ = [
  23. "CMSQuery",
  24. ]
  25. class CMSQuery(object):
  26. __slots__ = (
  27. "__queryDict",
  28. )
  29. def __init__(self, queryDict):
  30. self.__queryDict = {
  31. name : values[-1]
  32. for name, values in queryDict.items()
  33. }
  34. def items(self):
  35. return self.__queryDict.items()
  36. def get(self, name, default=""):
  37. return self.__queryDict.get(name, default)
  38. def getInt(self, name, default=0):
  39. try:
  40. return int(self.__queryDict.get(name, default))
  41. except (ValueError) as e:
  42. return default
  43. def getBool(self, name, default=False):
  44. return stringBool(self.__queryDict.get(name, str(default)), default)
  45. def __hash__(self):
  46. return hash(frozenset(self.__queryDict.items()))
  47. def __eq__(self, other):
  48. return (isinstance(other, self.__class__) and
  49. self.__queryDict == other.__queryDict)