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

178 行
4.3 KiB

  1. # session_cache.py
  2. # clear cache
  3. # ==================================================
  4. def clear():
  5. clear_cache()
  6. import webnotes
  7. webnotes.response['message'] = "Cache Cleared"
  8. def clear_cache(user=''):
  9. import webnotes
  10. try:
  11. if user:
  12. webnotes.conn.sql("delete from __SessionCache where user=%s", user)
  13. else:
  14. webnotes.conn.sql("delete from __SessionCache")
  15. except Exception, e:
  16. if e.args[0]==1146:
  17. make_cache_table()
  18. else:
  19. raise e
  20. # load cache
  21. # ==================================================
  22. def get():
  23. import webnotes
  24. import webnotes.defs
  25. # get country
  26. country = webnotes.session['data'].get('ipinfo', {}).get('countryName', 'Unknown Country')
  27. # run patches
  28. try:
  29. import webnotes.modules.patch
  30. webnotes.modules.patch.run()
  31. except ImportError, e:
  32. pass # no patches - do nothing
  33. # check if cache exists
  34. if not getattr(webnotes.defs,'auto_cache_clear',None):
  35. cache = load(country)
  36. if cache:
  37. return cache
  38. # if not create it
  39. sd = build()
  40. dump(sd, country)
  41. # update profile from cache
  42. webnotes.session['data']['profile'] = sd['profile']
  43. return sd
  44. # load cache
  45. # ==================================================
  46. def load(country):
  47. import webnotes
  48. try:
  49. sd = webnotes.conn.sql("select cache from __SessionCache where user='%s' %s" % (webnotes.session['user'], (country and (" and country='%s'" % country) or '')))
  50. if sd:
  51. return eval(sd[0][0])
  52. else:
  53. return None
  54. except Exception, e:
  55. if e.args[0]==1146:
  56. make_cache_table()
  57. else:
  58. raise e
  59. # make the cache table
  60. # ==================================================
  61. def make_cache_table():
  62. import webnotes
  63. webnotes.conn.commit()
  64. webnotes.conn.sql("create table `__SessionCache` (user VARCHAR(120), country VARCHAR(120), cache LONGTEXT)")
  65. webnotes.conn.begin()
  66. # dump session to cache
  67. # ==================================================
  68. def dump(sd, country):
  69. import webnotes
  70. import webnotes.model.utils
  71. if sd.get('docs'):
  72. sd['docs'] = webnotes.model.utils.compress(sd['docs'])
  73. # delete earlier (?)
  74. webnotes.conn.sql("delete from __SessionCache where user=%s and country=%s", (webnotes.session['user'], country))
  75. # make new
  76. webnotes.conn.sql("insert into `__SessionCache` (user, country, cache) VALUES (%s, %s, %s)", (webnotes.session['user'], country, str(sd)))
  77. # ==================================================
  78. def get_letter_heads():
  79. import webnotes
  80. try:
  81. lh = {}
  82. ret = webnotes.conn.sql("select name, content from `tabLetter Head` where ifnull(disabled,0)=0")
  83. for r in ret:
  84. lh[r[0]] = r[1]
  85. return lh
  86. except Exception, e:
  87. if e.args[0]==1146:
  88. return {}
  89. else:
  90. raise Exception, e
  91. # ==================================================
  92. # load startup.js and startup.css from the modules/startup folder
  93. def load_startup(cp):
  94. from webnotes.modules import ModuleFile
  95. try: from webnotes.defs import modules_path
  96. except ImportError: return
  97. import os
  98. cp.startup_code = ModuleFile(os.path.join(modules_path, 'startup', 'startup.js')).load_content()
  99. cp.startup_css = ModuleFile(os.path.join(modules_path, 'startup', 'startup.css')).load_content()
  100. # build it
  101. # ==================================================
  102. def build():
  103. sd = {}
  104. import webnotes
  105. import webnotes.model
  106. import webnotes.model.doc
  107. import webnotes.model.doctype
  108. import webnotes.widgets.page
  109. import webnotes.widgets.menus
  110. import webnotes.profile
  111. import webnotes.defs
  112. sql = webnotes.conn.sql
  113. webnotes.conn.begin()
  114. sd['profile'] = webnotes.user.load_profile()
  115. doclist = []
  116. doclist += webnotes.model.doc.get('Control Panel')
  117. cp = doclist[0]
  118. load_startup(cp)
  119. doclist += webnotes.model.doctype.get('Event')
  120. doclist += webnotes.model.doctype.get('Search Criteria')
  121. home_page = webnotes.user.get_home_page()
  122. if home_page:
  123. doclist += webnotes.widgets.page.get(home_page)
  124. sd['account_name'] = cp.account_id or ''
  125. sd['sysdefaults'] = webnotes.utils.get_defaults()
  126. sd['n_online'] = int(sql("SELECT COUNT(DISTINCT user) FROM tabSessions")[0][0] or 0)
  127. sd['docs'] = doclist
  128. sd['letter_heads'] = get_letter_heads()
  129. sd['home_page'] = home_page or ''
  130. sd['start_items'] = webnotes.widgets.menus.get_menu_items()
  131. if webnotes.session['data'].get('ipinfo'):
  132. sd['ipinfo'] = webnotes.session['data']['ipinfo']
  133. webnotes.session['data']['profile'] = sd['profile']
  134. sd['dt_labels'] = webnotes.model.get_dt_labels()
  135. webnotes.conn.commit()
  136. return sd