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.
 
 
 
 
 
 

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