Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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