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.

236 lines
7.9 KiB

10 years ago
8 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. from base64 import urlsafe_b64encode
  2. import os
  3. from datetime import timedelta
  4. from django.conf import settings
  5. from django.contrib.auth.hashers import check_password, make_password
  6. from django.contrib.auth.models import User
  7. from django.core.signing import TimestampSigner
  8. from django.db import models
  9. from django.urls import reverse
  10. from django.utils import timezone
  11. from hc.lib import emails
  12. NO_NAG = timedelta()
  13. NAG_PERIODS = ((NO_NAG, "Disabled"),
  14. (timedelta(hours=1), "Hourly"),
  15. (timedelta(days=1), "Daily"))
  16. def month(dt):
  17. """ For a given datetime, return the matching first-day-of-month date. """
  18. return dt.date().replace(day=1)
  19. class ProfileManager(models.Manager):
  20. def for_user(self, user):
  21. try:
  22. return user.profile
  23. except Profile.DoesNotExist:
  24. profile = Profile(user=user)
  25. if not settings.USE_PAYMENTS:
  26. # If not using payments, set high limits
  27. profile.check_limit = 500
  28. profile.sms_limit = 500
  29. profile.team_limit = 500
  30. profile.save()
  31. return profile
  32. class Profile(models.Model):
  33. # Owner:
  34. user = models.OneToOneField(User, models.CASCADE, blank=True, null=True)
  35. team_name = models.CharField(max_length=200, blank=True)
  36. next_report_date = models.DateTimeField(null=True, blank=True)
  37. reports_allowed = models.BooleanField(default=True)
  38. nag_period = models.DurationField(default=NO_NAG, choices=NAG_PERIODS)
  39. next_nag_date = models.DateTimeField(null=True, blank=True)
  40. ping_log_limit = models.IntegerField(default=100)
  41. check_limit = models.IntegerField(default=20)
  42. token = models.CharField(max_length=128, blank=True)
  43. api_key_id = models.CharField(max_length=128, blank=True)
  44. api_key = models.CharField(max_length=128, blank=True)
  45. api_key_readonly = models.CharField(max_length=128, blank=True)
  46. current_team = models.ForeignKey("self", models.SET_NULL, null=True)
  47. last_sms_date = models.DateTimeField(null=True, blank=True)
  48. sms_limit = models.IntegerField(default=0)
  49. sms_sent = models.IntegerField(default=0)
  50. team_limit = models.IntegerField(default=2)
  51. sort = models.CharField(max_length=20, default="created")
  52. objects = ProfileManager()
  53. def __str__(self):
  54. return self.team_name or self.user.email
  55. def notifications_url(self):
  56. return settings.SITE_ROOT + reverse("hc-notifications")
  57. def reports_unsub_url(self):
  58. signer = TimestampSigner(salt="reports")
  59. signed_username = signer.sign(self.user.username)
  60. path = reverse("hc-unsubscribe-reports", args=[signed_username])
  61. return settings.SITE_ROOT + path
  62. def team(self):
  63. # compare ids to avoid SQL queries
  64. if self.current_team_id and self.current_team_id != self.id:
  65. return self.current_team
  66. return self
  67. def prepare_token(self, salt):
  68. token = urlsafe_b64encode(os.urandom(24)).decode()
  69. self.token = make_password(token, salt)
  70. self.save()
  71. return token
  72. def check_token(self, token, salt):
  73. return salt in self.token and check_password(token, self.token)
  74. def send_instant_login_link(self, inviting_profile=None, redirect_url=None):
  75. token = self.prepare_token("login")
  76. path = reverse("hc-check-token", args=[self.user.username, token])
  77. if redirect_url:
  78. path += "?next=%s" % redirect_url
  79. ctx = {
  80. "button_text": "Sign In",
  81. "button_url": settings.SITE_ROOT + path,
  82. "inviting_profile": inviting_profile
  83. }
  84. emails.login(self.user.email, ctx)
  85. def send_set_password_link(self):
  86. token = self.prepare_token("set-password")
  87. path = reverse("hc-set-password", args=[token])
  88. ctx = {
  89. "button_text": "Set Password",
  90. "button_url": settings.SITE_ROOT + path
  91. }
  92. emails.set_password(self.user.email, ctx)
  93. def send_change_email_link(self):
  94. token = self.prepare_token("change-email")
  95. path = reverse("hc-change-email", args=[token])
  96. ctx = {
  97. "button_text": "Change Email",
  98. "button_url": settings.SITE_ROOT + path
  99. }
  100. emails.change_email(self.user.email, ctx)
  101. def set_api_keys(self, key_id=""):
  102. self.api_key_id = key_id
  103. self.api_key = urlsafe_b64encode(os.urandom(24)).decode()
  104. self.api_key_readonly = urlsafe_b64encode(os.urandom(24)).decode()
  105. self.save()
  106. def checks_from_all_teams(self):
  107. """ Return a queryset of checks from all teams we have access for. """
  108. team_ids = set(self.user.memberships.values_list("team_id", flat=True))
  109. team_ids.add(self.id)
  110. from hc.api.models import Check
  111. return Check.objects.filter(user__profile__id__in=team_ids)
  112. def send_report(self, nag=False):
  113. checks = self.checks_from_all_teams()
  114. # Has there been a ping in last 6 months?
  115. result = checks.aggregate(models.Max("last_ping"))
  116. last_ping = result["last_ping__max"]
  117. six_months_ago = timezone.now() - timedelta(days=180)
  118. if last_ping is None or last_ping < six_months_ago:
  119. return False
  120. # Is there at least one check that is down?
  121. num_down = checks.filter(status="down").count()
  122. if nag and num_down == 0:
  123. return False
  124. # Sort checks by owner. Need this because will group by owner in
  125. # template.
  126. checks = checks.select_related("user", "user__profile")
  127. checks = checks.order_by("user_id")
  128. # list() executes the query, to avoid DB access while
  129. # rendering the template
  130. checks = list(checks)
  131. unsub_url = self.reports_unsub_url()
  132. headers = {
  133. "List-Unsubscribe": unsub_url,
  134. "X-Bounce-Url": unsub_url
  135. }
  136. ctx = {
  137. "checks": checks,
  138. "sort": self.sort,
  139. "now": timezone.now(),
  140. "unsub_link": unsub_url,
  141. "notifications_url": self.notifications_url(),
  142. "nag": nag,
  143. "nag_period": self.nag_period.total_seconds(),
  144. "num_down": num_down
  145. }
  146. emails.report(self.user.email, ctx, headers)
  147. return True
  148. def can_invite(self):
  149. return self.member_set.count() < self.team_limit
  150. def invite(self, user):
  151. member = Member(team=self, user=user)
  152. member.save()
  153. # Switch the invited user over to the new team so they
  154. # notice the new team on next visit:
  155. user.profile.current_team = self
  156. user.profile.save()
  157. user.profile.send_instant_login_link(self)
  158. def sms_sent_this_month(self):
  159. # IF last_sms_date was never set, we have not sent any messages yet.
  160. if not self.last_sms_date:
  161. return 0
  162. # If last sent date is not from this month, we've sent 0 this month.
  163. if month(timezone.now()) > month(self.last_sms_date):
  164. return 0
  165. return self.sms_sent
  166. def authorize_sms(self):
  167. """ If monthly limit not exceeded, increase counter and return True """
  168. sent_this_month = self.sms_sent_this_month()
  169. if sent_this_month >= self.sms_limit:
  170. return False
  171. self.sms_sent = sent_this_month + 1
  172. self.last_sms_date = timezone.now()
  173. self.save()
  174. return True
  175. def set_next_nag_date(self):
  176. """ Set next_nag_date for all members of this team. """
  177. is_owner = models.Q(id=self.id)
  178. is_member = models.Q(user__memberships__team=self)
  179. q = Profile.objects.filter(is_owner | is_member)
  180. q = q.exclude(nag_period=NO_NAG)
  181. # Exclude profiles with next_nag_date already set
  182. q = q.filter(next_nag_date__isnull=True)
  183. q.update(next_nag_date=timezone.now() + models.F("nag_period"))
  184. class Member(models.Model):
  185. team = models.ForeignKey(Profile, models.CASCADE)
  186. user = models.ForeignKey(User, models.CASCADE, related_name="memberships")