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.
 
 
 
 
 
 

78 lines
2.0 KiB

  1. """
  2. Boot session from cache or build
  3. Session bootstraps info needed by common client side activities including
  4. permission, homepage, control panel variables, system defaults etc
  5. """
  6. import webnotes
  7. @webnotes.whitelist()
  8. def clear():
  9. """clear all cache"""
  10. clear_cache()
  11. webnotes.response['message'] = "Cache Cleared"
  12. def clear_cache(user=''):
  13. """clear cache"""
  14. if user:
  15. webnotes.conn.sql("delete from __SessionCache where user=%s", user)
  16. webnotes.conn.sql("update tabSessions set sessiondata=NULL where user=%s", user)
  17. else:
  18. webnotes.conn.sql("delete from __SessionCache")
  19. webnotes.conn.sql("update tabSessions set sessiondata=NULL")
  20. # rebuild a cache for guest
  21. if webnotes.session:
  22. webnotes.session['data'] = {}
  23. def get():
  24. """get session boot info"""
  25. import webnotes.defs
  26. # get country
  27. country = webnotes.session['data'].get('ipinfo', {}).get('countryName', 'Unknown Country')
  28. # check if cache exists
  29. if not getattr(webnotes.defs,'auto_cache_clear',None):
  30. cache = load(country)
  31. if cache:
  32. return cache
  33. # if not create it
  34. import webnotes.boot
  35. bootinfo = webnotes.boot.get_bootinfo()
  36. add_to_cache(bootinfo, country)
  37. return bootinfo
  38. def load(country):
  39. """load from cache"""
  40. try:
  41. sd = webnotes.conn.sql("select cache from __SessionCache where user='%s' %s" % (webnotes.session['user'], (country and (" and country='%s'" % country) or '')))
  42. if sd:
  43. return eval(sd[0][0])
  44. else:
  45. return None
  46. except Exception, e:
  47. if e.args[0]==1146:
  48. make_cache_table()
  49. else:
  50. raise e
  51. def add_to_cache(bootinfo, country):
  52. """add to cache"""
  53. import webnotes.model.utils
  54. if bootinfo.get('docs'):
  55. bootinfo['docs'] = webnotes.model.utils.compress(bootinfo['docs'])
  56. # delete earlier (?)
  57. webnotes.conn.sql("""delete from __SessionCache where user=%s
  58. and country=%s""", (webnotes.session['user'], country))
  59. # make new
  60. webnotes.conn.sql("""insert into `__SessionCache`
  61. (user, country, cache) VALUES (%s, %s, %s)""", \
  62. (webnotes.session['user'], country, str(bootinfo)))