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.

161 lines
5.2 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
  1. import base64
  2. import os
  3. import uuid
  4. from datetime import timedelta
  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 import signing
  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. def month(dt):
  14. """ For a given datetime, return the matching first-day-of-month date. """
  15. return dt.date().replace(day=1)
  16. class ProfileManager(models.Manager):
  17. def for_user(self, user):
  18. try:
  19. return user.profile
  20. except Profile.DoesNotExist:
  21. profile = Profile(user=user, team_access_allowed=user.is_superuser)
  22. if not settings.USE_PAYMENTS:
  23. # If not using payments, set high limits
  24. profile.check_limit = 500
  25. profile.sms_limit = 500
  26. profile.save()
  27. return profile
  28. class Profile(models.Model):
  29. # Owner:
  30. user = models.OneToOneField(User, models.CASCADE, blank=True, null=True)
  31. team_name = models.CharField(max_length=200, blank=True)
  32. team_access_allowed = models.BooleanField(default=False)
  33. next_report_date = models.DateTimeField(null=True, blank=True)
  34. reports_allowed = models.BooleanField(default=True)
  35. ping_log_limit = models.IntegerField(default=100)
  36. check_limit = models.IntegerField(default=20)
  37. token = models.CharField(max_length=128, blank=True)
  38. api_key = models.CharField(max_length=128, blank=True)
  39. current_team = models.ForeignKey("self", models.SET_NULL, null=True)
  40. bill_to = models.TextField(blank=True)
  41. last_sms_date = models.DateTimeField(null=True, blank=True)
  42. sms_limit = models.IntegerField(default=0)
  43. sms_sent = models.IntegerField(default=0)
  44. objects = ProfileManager()
  45. def __str__(self):
  46. return self.team_name or self.user.email
  47. def team(self):
  48. # compare ids to avoid SQL queries
  49. if self.current_team_id and self.current_team_id != self.id:
  50. return self.current_team
  51. return self
  52. def prepare_token(self, salt):
  53. token = str(uuid.uuid4())
  54. self.token = make_password(token, salt)
  55. self.save()
  56. return token
  57. def check_token(self, token, salt):
  58. return salt in self.token and check_password(token, self.token)
  59. def send_instant_login_link(self, inviting_profile=None):
  60. token = self.prepare_token("login")
  61. path = reverse("hc-check-token", args=[self.user.username, token])
  62. ctx = {
  63. "button_text": "Log In",
  64. "button_url": settings.SITE_ROOT + path,
  65. "inviting_profile": inviting_profile
  66. }
  67. emails.login(self.user.email, ctx)
  68. def send_set_password_link(self):
  69. token = self.prepare_token("set-password")
  70. path = reverse("hc-set-password", args=[token])
  71. ctx = {
  72. "button_text": "Set Password",
  73. "button_url": settings.SITE_ROOT + path
  74. }
  75. emails.set_password(self.user.email, ctx)
  76. def send_change_email_link(self):
  77. token = self.prepare_token("change-email")
  78. path = reverse("hc-change-email", args=[token])
  79. ctx = {
  80. "button_text": "Change Email",
  81. "button_url": settings.SITE_ROOT + path
  82. }
  83. emails.change_email(self.user.email, ctx)
  84. def set_api_key(self):
  85. self.api_key = base64.urlsafe_b64encode(os.urandom(24))
  86. self.save()
  87. def send_report(self):
  88. # reset next report date first:
  89. now = timezone.now()
  90. self.next_report_date = now + timedelta(days=30)
  91. self.save()
  92. token = signing.Signer().sign(uuid.uuid4())
  93. path = reverse("hc-unsubscribe-reports", args=[self.user.username])
  94. unsub_link = "%s%s?token=%s" % (settings.SITE_ROOT, path, token)
  95. ctx = {
  96. "checks": self.user.check_set.order_by("created"),
  97. "now": now,
  98. "unsub_link": unsub_link
  99. }
  100. emails.report(self.user.email, ctx)
  101. def invite(self, user):
  102. member = Member(team=self, user=user)
  103. member.save()
  104. # Switch the invited user over to the new team so they
  105. # notice the new team on next visit:
  106. user.profile.current_team = self
  107. user.profile.save()
  108. user.profile.send_instant_login_link(self)
  109. def sms_sent_this_month(self):
  110. # IF last_sms_date was never set, we have not sent any messages yet.
  111. if not self.last_sms_date:
  112. return 0
  113. # If last sent date is not from this month, we've sent 0 this month.
  114. if month(timezone.now()) > month(self.last_sms_date):
  115. return 0
  116. return self.sms_sent
  117. def authorize_sms(self):
  118. """ If monthly limit not exceeded, increase counter and return True """
  119. sent_this_month = self.sms_sent_this_month()
  120. if sent_this_month >= self.sms_limit:
  121. return False
  122. self.sms_sent = sent_this_month + 1
  123. self.last_sms_date = timezone.now()
  124. self.save()
  125. return True
  126. class Member(models.Model):
  127. team = models.ForeignKey(Profile, models.CASCADE)
  128. user = models.ForeignKey(User, models.CASCADE)