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.
 
 
 
 
 
 

94 line
2.4 KiB

  1. from __future__ import unicode_literals
  2. import frappe
  3. from frappe.utils import now_datetime, getdate
  4. from frappe.installer import update_site_config
  5. from frappe.utils.data import formatdate
  6. from frappe import _
  7. class SiteExpiredError(frappe.ValidationError):
  8. pass
  9. EXPIRY_WARNING_DAYS = 10
  10. def load_limits(bootinfo):
  11. bootinfo["frappe_limits"] = get_limits()
  12. bootinfo["expiry_message"] = get_expiry_message()
  13. def has_expired():
  14. if frappe.session.user=="Administrator":
  15. return False
  16. expires_on = get_limits().get("expiry")
  17. if not expires_on:
  18. return False
  19. if now_datetime().date() <= getdate(expires_on):
  20. return False
  21. return True
  22. def check_if_expired():
  23. """check if account is expired. If expired, do not allow login"""
  24. if not has_expired():
  25. return
  26. # if expired, stop user from logging in
  27. expires_on = formatdate(get_limits().get("expiry"))
  28. support_email = get_limits().get("support_email") or _("your provider")
  29. frappe.throw(_("""Your subscription expired on {0}.
  30. To extend please send an email to {1}""").format(expires_on, support_email),
  31. SiteExpiredError)
  32. def get_expiry_message():
  33. if "System Manager" not in frappe.get_roles():
  34. return ""
  35. if not get_limits().get("expiry"):
  36. return ""
  37. expires_on = getdate(get_limits().get("expiry"))
  38. today = now_datetime().date()
  39. message = ""
  40. if today > expires_on:
  41. message = _("Your subscription has expired")
  42. else:
  43. days_to_expiry = (expires_on - today).days
  44. if days_to_expiry == 0:
  45. message = _("Your subscription will expire today")
  46. elif days_to_expiry == 1:
  47. message = _("Your subscription will expire tomorrow")
  48. elif days_to_expiry <= EXPIRY_WARNING_DAYS:
  49. message = _("Your subscription will expire on") + " " + formatdate(expires_on)
  50. return message
  51. @frappe.whitelist()
  52. def get_limits():
  53. limits = frappe.get_conf().get("limits") or {}
  54. day = frappe.utils.add_months(frappe.utils.today(), -1)
  55. limits["emails_sent"] = frappe.db.count("Email Queue", filters={'creation': ['>', day]})
  56. return limits
  57. def set_limits(limits):
  58. # Add/Update current config options in site_config
  59. frappe_limits = get_limits() or {}
  60. for key in limits.keys():
  61. frappe_limits[key] = limits[key]
  62. update_site_config("limits", frappe_limits, validate=False)
  63. def clear_limit(limit):
  64. frappe_limits = get_limits()
  65. if limit in frappe_limits:
  66. del frappe_limits[limit]
  67. update_site_config("limits", frappe_limits, validate=False)