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.

233 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):
  75. token = self.prepare_token("login")
  76. path = reverse("hc-check-token", args=[self.user.username, token])
  77. ctx = {
  78. "button_text": "Log In",
  79. "button_url": settings.SITE_ROOT + path,
  80. "inviting_profile": inviting_profile
  81. }
  82. emails.login(self.user.email, ctx)
  83. def send_set_password_link(self):
  84. token = self.prepare_token("set-password")
  85. path = reverse("hc-set-password", args=[token])
  86. ctx = {
  87. "button_text": "Set Password",
  88. "button_url": settings.SITE_ROOT + path
  89. }
  90. emails.set_password(self.user.email, ctx)
  91. def send_change_email_link(self):
  92. token = self.prepare_token("change-email")
  93. path = reverse("hc-change-email", args=[token])
  94. ctx = {
  95. "button_text": "Change Email",
  96. "button_url": settings.SITE_ROOT + path
  97. }
  98. emails.change_email(self.user.email, ctx)
  99. def set_api_keys(self, key_id=""):
  100. self.api_key_id = key_id
  101. self.api_key = urlsafe_b64encode(os.urandom(24)).decode()
  102. self.api_key_readonly = urlsafe_b64encode(os.urandom(24)).decode()
  103. self.save()
  104. def checks_from_all_teams(self):
  105. """ Return a queryset of checks from all teams we have access for. """
  106. team_ids = set(self.user.memberships.values_list("team_id", flat=True))
  107. team_ids.add(self.id)
  108. from hc.api.models import Check
  109. return Check.objects.filter(user__profile__id__in=team_ids)
  110. def send_report(self, nag=False):
  111. checks = self.checks_from_all_teams()
  112. # Has there been a ping in last 6 months?
  113. result = checks.aggregate(models.Max("last_ping"))
  114. last_ping = result["last_ping__max"]
  115. six_months_ago = timezone.now() - timedelta(days=180)
  116. if last_ping is None or last_ping < six_months_ago:
  117. return False
  118. # Is there at least one check that is down?
  119. num_down = checks.filter(status="down").count()
  120. if nag and num_down == 0:
  121. return False
  122. # Sort checks by owner. Need this because will group by owner in
  123. # template.
  124. checks = checks.select_related("user", "user__profile")
  125. checks = checks.order_by("user_id")
  126. # list() executes the query, to avoid DB access while
  127. # rendering the template
  128. checks = list(checks)
  129. unsub_url = self.reports_unsub_url()
  130. headers = {
  131. "List-Unsubscribe": unsub_url,
  132. "X-Bounce-Url": unsub_url
  133. }
  134. ctx = {
  135. "checks": checks,
  136. "sort": self.sort,
  137. "now": timezone.now(),
  138. "unsub_link": unsub_url,
  139. "notifications_url": self.notifications_url(),
  140. "nag": nag,
  141. "nag_period": self.nag_period.total_seconds(),
  142. "num_down": num_down
  143. }
  144. emails.report(self.user.email, ctx, headers)
  145. return True
  146. def can_invite(self):
  147. return self.member_set.count() < self.team_limit
  148. def invite(self, user):
  149. member = Member(team=self, user=user)
  150. member.save()
  151. # Switch the invited user over to the new team so they
  152. # notice the new team on next visit:
  153. user.profile.current_team = self
  154. user.profile.save()
  155. user.profile.send_instant_login_link(self)
  156. def sms_sent_this_month(self):
  157. # IF last_sms_date was never set, we have not sent any messages yet.
  158. if not self.last_sms_date:
  159. return 0
  160. # If last sent date is not from this month, we've sent 0 this month.
  161. if month(timezone.now()) > month(self.last_sms_date):
  162. return 0
  163. return self.sms_sent
  164. def authorize_sms(self):
  165. """ If monthly limit not exceeded, increase counter and return True """
  166. sent_this_month = self.sms_sent_this_month()
  167. if sent_this_month >= self.sms_limit:
  168. return False
  169. self.sms_sent = sent_this_month + 1
  170. self.last_sms_date = timezone.now()
  171. self.save()
  172. return True
  173. def set_next_nag_date(self):
  174. """ Set next_nag_date for all members of this team. """
  175. is_owner = models.Q(id=self.id)
  176. is_member = models.Q(user__memberships__team=self)
  177. q = Profile.objects.filter(is_owner | is_member)
  178. q = q.exclude(nag_period=NO_NAG)
  179. # Exclude profiles with next_nag_date already set
  180. q = q.filter(next_nag_date__isnull=True)
  181. q.update(next_nag_date=timezone.now() + models.F("nag_period"))
  182. class Member(models.Model):
  183. team = models.ForeignKey(Profile, models.CASCADE)
  184. user = models.ForeignKey(User, models.CASCADE, related_name="memberships")