Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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