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.

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