選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

237 行
6.7 KiB

  1. from __future__ import unicode_literals
  2. import frappe
  3. from frappe import _
  4. from frappe.utils import now_datetime, getdate, flt, cint, get_fullname
  5. from frappe.installer import update_site_config
  6. from frappe.utils.data import formatdate
  7. from frappe.utils.user import get_enabled_system_users, disable_users
  8. import os, subprocess
  9. from six.moves.urllib.parse import parse_qsl, urlsplit, urlunsplit, urlencode
  10. class SiteExpiredError(frappe.ValidationError):
  11. http_status_code = 417
  12. EXPIRY_WARNING_DAYS = 10
  13. def check_if_expired():
  14. """check if account is expired. If expired, do not allow login"""
  15. if not has_expired():
  16. return
  17. limits = get_limits()
  18. expiry = limits.get("expiry")
  19. if not expiry:
  20. return
  21. expires_on = formatdate(limits.get("expiry"))
  22. support_email = limits.get("support_email")
  23. if limits.upgrade_url:
  24. message = _("""Your subscription expired on {0}. To renew, {1}.""").format(expires_on, get_upgrade_link(limits.upgrade_url))
  25. elif support_email:
  26. message = _("""Your subscription expired on {0}. To renew, please send an email to {1}.""").format(expires_on, support_email)
  27. else:
  28. # no recourse just quit
  29. return
  30. frappe.throw(message, SiteExpiredError)
  31. def has_expired():
  32. if frappe.session.user=="Administrator":
  33. return False
  34. expires_on = get_limits().expiry
  35. if not expires_on:
  36. return False
  37. if now_datetime().date() <= getdate(expires_on):
  38. return False
  39. return True
  40. def get_expiry_message():
  41. if "System Manager" not in frappe.get_roles():
  42. return ""
  43. limits = get_limits()
  44. if not limits.expiry:
  45. return ""
  46. expires_on = getdate(get_limits().get("expiry"))
  47. today = now_datetime().date()
  48. message = ""
  49. if today > expires_on:
  50. message = _("Your subscription has expired.")
  51. else:
  52. days_to_expiry = (expires_on - today).days
  53. if days_to_expiry == 0:
  54. message = _("Your subscription will expire today.")
  55. elif days_to_expiry == 1:
  56. message = _("Your subscription will expire tomorrow.")
  57. elif days_to_expiry <= EXPIRY_WARNING_DAYS:
  58. message = _("Your subscription will expire on {0}.").format(formatdate(expires_on))
  59. if message and limits.upgrade_url:
  60. upgrade_link = get_upgrade_link(limits.upgrade_url)
  61. message += ' ' + _('To renew, {0}.').format(upgrade_link)
  62. return message
  63. @frappe.whitelist()
  64. def get_usage_info():
  65. '''Get data to show for Usage Info'''
  66. # imported here to prevent circular import
  67. from frappe.email.queue import get_emails_sent_this_month
  68. limits = get_limits()
  69. if not (limits and any([limits.users, limits.space, limits.emails, limits.expiry])):
  70. # no limits!
  71. return
  72. limits.space = (limits.space or 0) * 1024.0 # to MB
  73. if not limits.space_usage:
  74. # hack! to show some progress
  75. limits.space_usage = {
  76. 'database_size': 26,
  77. 'files_size': 1,
  78. 'backup_size': 1,
  79. 'total': 28
  80. }
  81. usage_info = frappe._dict({
  82. 'limits': limits,
  83. 'enabled_users': len(get_enabled_system_users()),
  84. 'emails_sent': get_emails_sent_this_month(),
  85. 'space_usage': limits.space_usage['total'],
  86. })
  87. if limits.expiry:
  88. usage_info['expires_on'] = formatdate(limits.expiry)
  89. usage_info['days_to_expiry'] = (getdate(limits.expiry) - getdate()).days
  90. if limits.upgrade_url:
  91. usage_info['upgrade_url'] = get_upgrade_url(limits.upgrade_url)
  92. return usage_info
  93. def get_upgrade_url(upgrade_url):
  94. parts = urlsplit(upgrade_url)
  95. params = dict(parse_qsl(parts.query))
  96. params.update({
  97. 'site': frappe.local.site,
  98. 'email': frappe.session.user,
  99. 'full_name': get_fullname(),
  100. 'country': frappe.db.get_value("System Settings", "System Settings", 'country')
  101. })
  102. query = urlencode(params, doseq=True)
  103. url = urlunsplit((parts.scheme, parts.netloc, parts.path, query, parts.fragment))
  104. return url
  105. def get_upgrade_link(upgrade_url, label=None):
  106. upgrade_url = get_upgrade_url(upgrade_url)
  107. upgrade_link = '<a href="{upgrade_url}" target="_blank">{click_here}</a>'.format(upgrade_url=upgrade_url, click_here=label or _('click here'))
  108. return upgrade_link
  109. def get_limits():
  110. '''
  111. "limits": {
  112. "users": 1,
  113. "space": 0.5, # in GB
  114. "emails": 1000 # per month
  115. "expiry": "2099-12-31"
  116. }
  117. '''
  118. return frappe._dict(frappe.local.conf.limits or {})
  119. def update_limits(limits_dict):
  120. '''Add/Update limit in site_config'''
  121. limits = get_limits()
  122. limits.update(limits_dict)
  123. update_site_config("limits", limits, validate=False)
  124. disable_users(limits)
  125. frappe.local.conf.limits = limits
  126. def clear_limit(key):
  127. '''Remove a limit option from site_config'''
  128. limits = get_limits()
  129. to_clear = [key] if isinstance(key, basestring) else key
  130. for key in to_clear:
  131. if key in limits:
  132. del limits[key]
  133. update_site_config("limits", limits, validate=False)
  134. frappe.conf.limits = limits
  135. def validate_space_limit(file_size):
  136. """Stop from writing file if max space limit is reached"""
  137. from frappe.utils.file_manager import MaxFileSizeReachedError
  138. limits = get_limits()
  139. if not limits.space:
  140. return
  141. # to MB
  142. space_limit = flt(limits.space * 1024.0, 2)
  143. # in MB
  144. usage = frappe._dict(limits.space_usage or {})
  145. if not usage:
  146. # first time
  147. usage = frappe._dict(update_space_usage())
  148. file_size = file_size / (1024.0 ** 2)
  149. if flt(flt(usage.total) + file_size, 2) > space_limit:
  150. # Stop from attaching file
  151. frappe.throw(_("You have exceeded the max space of {0} for your plan. {1}.").format(
  152. "<b>{0}MB</b>".format(cint(space_limit)) if (space_limit < 1024) else "<b>{0}GB</b>".format(limits.space),
  153. '<a href="#usage-info">{0}</a>'.format(_("Click here to check your usage or upgrade to a higher plan"))),
  154. MaxFileSizeReachedError)
  155. # update files size in frappe subscription
  156. usage.files_size = flt(usage.files_size) + file_size
  157. update_limits({ 'space_usage': usage })
  158. def update_space_usage():
  159. # public and private files
  160. files_size = get_folder_size(frappe.get_site_path("public", "files"))
  161. files_size += get_folder_size(frappe.get_site_path("private", "files"))
  162. backup_size = get_folder_size(frappe.get_site_path("private", "backups"))
  163. database_size = get_database_size()
  164. usage = {
  165. 'files_size': flt(files_size, 2),
  166. 'backup_size': flt(backup_size, 2),
  167. 'database_size': flt(database_size, 2),
  168. 'total': flt(flt(files_size) + flt(backup_size) + flt(database_size), 2)
  169. }
  170. update_limits({ 'space_usage': usage })
  171. return usage
  172. def get_folder_size(path):
  173. '''Returns folder size in MB if it exists'''
  174. if os.path.exists(path):
  175. return flt(subprocess.check_output(['du', '-ms', path]).split()[0], 2)
  176. def get_database_size():
  177. '''Returns approximate database size in MB'''
  178. db_name = frappe.conf.db_name
  179. # This query will get the database size in MB
  180. db_size = frappe.db.sql('''
  181. SELECT table_schema "database_name", sum( data_length + index_length ) / 1024 / 1024 "database_size"
  182. FROM information_schema.TABLES WHERE table_schema = %s GROUP BY table_schema''', db_name, as_dict=True)
  183. return flt(db_size[0].get('database_size'), 2)