You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

session_cache.py 3.3 KiB

13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. """
  23. Boot session from cache or build
  24. Session bootstraps info needed by common client side activities including
  25. permission, homepage, control panel variables, system defaults etc
  26. """
  27. import webnotes
  28. @webnotes.whitelist()
  29. def clear(user=None):
  30. """clear all cache"""
  31. import webnotes
  32. clear_cache(user)
  33. webnotes.response['message'] = "Cache Cleared"
  34. def clear_cache(user=''):
  35. """clear cache"""
  36. import webnotes
  37. if user:
  38. webnotes.conn.sql("delete from __SessionCache where user=%s", user)
  39. webnotes.conn.sql("update tabSessions set sessiondata=NULL where user=%s", user)
  40. else:
  41. webnotes.conn.sql("delete from __SessionCache")
  42. webnotes.conn.sql("update tabSessions set sessiondata=NULL")
  43. # doctype cache
  44. import webnotes.utils.cache
  45. webnotes.utils.cache.clear()
  46. # rebuild a cache for guest
  47. if webnotes.session:
  48. webnotes.session['data'] = {}
  49. def get():
  50. """get session boot info"""
  51. import webnotes
  52. import conf
  53. # get country
  54. country = webnotes.session['data'].get('ipinfo', {}).get('countryName', 'Unknown Country')
  55. # check if cache exists
  56. if not getattr(conf,'auto_cache_clear',None):
  57. cache = load(country)
  58. if cache:
  59. return cache
  60. # if not create it
  61. import webnotes.boot
  62. bootinfo = webnotes.boot.get_bootinfo()
  63. add_to_cache(bootinfo, country)
  64. return bootinfo
  65. def load(country):
  66. """load from cache"""
  67. import json
  68. try:
  69. sd = webnotes.conn.sql("select cache from __SessionCache where user='%s' %s" % (webnotes.session['user'], (country and (" and country='%s'" % country) or '')))
  70. if sd:
  71. return json.loads(sd[0][0])
  72. else:
  73. return None
  74. except Exception, e:
  75. if e.args[0]==1146:
  76. make_cache_table()
  77. else:
  78. raise e
  79. def add_to_cache(bootinfo, country):
  80. """add to cache"""
  81. import json
  82. import webnotes.model.utils
  83. if bootinfo.get('docs'):
  84. bootinfo['docs'] = webnotes.model.utils.compress(bootinfo['docs'])
  85. # delete earlier (?)
  86. webnotes.conn.sql("""delete from __SessionCache where user=%s
  87. and country=%s""", (webnotes.session['user'], country))
  88. # make new
  89. webnotes.conn.sql("""insert into `__SessionCache`
  90. (user, country, cache) VALUES (%s, %s, %s)""", \
  91. (webnotes.session['user'], country, json.dumps(bootinfo)))