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.
 
 
 
 
 
 

71 lines
1.9 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. def clear():
  8. """clear all cache"""
  9. clear_cache()
  10. webnotes.response['message'] = "Cache Cleared"
  11. def clear_cache(user=''):
  12. """clear cache"""
  13. if user:
  14. webnotes.conn.sql("delete from __SessionCache where user=%s", user)
  15. webnotes.conn.sql("update tabSessions set sessiondata=NULL where user=%s", user)
  16. else:
  17. webnotes.conn.sql("delete from __SessionCache")
  18. webnotes.conn.sql("update tabSessions set sessiondata=NULL")
  19. webnotes.session['data'] = {}
  20. def get():
  21. """get session boot info"""
  22. import webnotes.defs
  23. # get country
  24. country = webnotes.session['data'].get('ipinfo', {}).get('countryName', 'Unknown Country')
  25. # check if cache exists
  26. if not getattr(webnotes.defs,'auto_cache_clear',None):
  27. cache = load(country)
  28. if cache:
  29. return cache
  30. # if not create it
  31. import webnotes.boot
  32. bootinfo = webnotes.boot.get_bootinfo()
  33. add_to_cache(bootinfo, country)
  34. return bootinfo
  35. def load(country):
  36. """load from cache"""
  37. try:
  38. sd = webnotes.conn.sql("select cache from __SessionCache where user='%s' %s" % (webnotes.session['user'], (country and (" and country='%s'" % country) or '')))
  39. if sd:
  40. return eval(sd[0][0])
  41. else:
  42. return None
  43. except Exception, e:
  44. if e.args[0]==1146:
  45. make_cache_table()
  46. else:
  47. raise e
  48. def add_to_cache(sd, country):
  49. """add to cache"""
  50. import webnotes.model.utils
  51. if sd.get('docs'):
  52. sd['docs'] = webnotes.model.utils.compress(sd['docs'])
  53. # delete earlier (?)
  54. webnotes.conn.sql("delete from __SessionCache where user=%s and country=%s", (webnotes.session['user'], country))
  55. # make new
  56. webnotes.conn.sql("insert into `__SessionCache` (user, country, cache) VALUES (%s, %s, %s)", (webnotes.session['user'], country, str(sd)))