您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

101 行
3.1 KiB

  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. clear_cache(user)
  32. webnotes.response['message'] = "Cache Cleared"
  33. def clear_cache(user=''):
  34. """clear cache"""
  35. if user:
  36. webnotes.conn.sql("delete from __SessionCache where user=%s", user)
  37. webnotes.conn.sql("update tabSessions set sessiondata=NULL where user=%s", user)
  38. else:
  39. webnotes.conn.sql("delete from __SessionCache")
  40. webnotes.conn.sql("update tabSessions set sessiondata=NULL")
  41. # rebuild a cache for guest
  42. if webnotes.session:
  43. webnotes.session['data'] = {}
  44. def get():
  45. """get session boot info"""
  46. import webnotes
  47. import conf
  48. # get country
  49. country = webnotes.session['data'].get('ipinfo', {}).get('countryName', 'Unknown Country')
  50. # check if cache exists
  51. if not getattr(conf,'auto_cache_clear',None):
  52. cache = load(country)
  53. if cache:
  54. return cache
  55. # if not create it
  56. import webnotes.boot
  57. bootinfo = webnotes.boot.get_bootinfo()
  58. add_to_cache(bootinfo, country)
  59. return bootinfo
  60. def load(country):
  61. """load from cache"""
  62. try:
  63. sd = webnotes.conn.sql("select cache from __SessionCache where user='%s' %s" % (webnotes.session['user'], (country and (" and country='%s'" % country) or '')))
  64. if sd:
  65. return eval(sd[0][0])
  66. else:
  67. return None
  68. except Exception, e:
  69. if e.args[0]==1146:
  70. make_cache_table()
  71. else:
  72. raise e
  73. def add_to_cache(bootinfo, country):
  74. """add to cache"""
  75. import webnotes.model.utils
  76. if bootinfo.get('docs'):
  77. bootinfo['docs'] = webnotes.model.utils.compress(bootinfo['docs'])
  78. # delete earlier (?)
  79. webnotes.conn.sql("""delete from __SessionCache where user=%s
  80. and country=%s""", (webnotes.session['user'], country))
  81. # make new
  82. webnotes.conn.sql("""insert into `__SessionCache`
  83. (user, country, cache) VALUES (%s, %s, %s)""", \
  84. (webnotes.session['user'], country, str(bootinfo)))