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.

92 lines
2.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
  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 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. class Profile(models.Model):
  14. # Owner:
  15. user = models.OneToOneField(User, blank=True, null=True)
  16. team_name = models.CharField(max_length=200, blank=True)
  17. team_access_allowed = models.BooleanField(default=False)
  18. next_report_date = models.DateTimeField(null=True, blank=True)
  19. reports_allowed = models.BooleanField(default=True)
  20. ping_log_limit = models.IntegerField(default=100)
  21. token = models.CharField(max_length=128, blank=True)
  22. api_key = models.CharField(max_length=128, blank=True)
  23. current_team = models.ForeignKey("self", null=True)
  24. def __str__(self):
  25. return self.team_name or self.user.email
  26. def send_instant_login_link(self, inviting_profile=None):
  27. token = str(uuid.uuid4())
  28. self.token = make_password(token)
  29. self.save()
  30. path = reverse("hc-check-token", args=[self.user.username, token])
  31. ctx = {
  32. "button_text": "Log In",
  33. "button_url": settings.SITE_ROOT + path,
  34. "inviting_profile": inviting_profile
  35. }
  36. emails.login(self.user.email, ctx)
  37. def send_set_password_link(self):
  38. token = str(uuid.uuid4())
  39. self.token = make_password(token)
  40. self.save()
  41. path = reverse("hc-set-password", args=[token])
  42. ctx = {
  43. "button_text": "Set Password",
  44. "button_url": settings.SITE_ROOT + path
  45. }
  46. emails.set_password(self.user.email, ctx)
  47. def set_api_key(self):
  48. self.api_key = base64.urlsafe_b64encode(os.urandom(24))
  49. self.save()
  50. def send_report(self):
  51. # reset next report date first:
  52. now = timezone.now()
  53. self.next_report_date = now + timedelta(days=30)
  54. self.save()
  55. token = signing.Signer().sign(uuid.uuid4())
  56. path = reverse("hc-unsubscribe-reports", args=[self.user.username])
  57. unsub_link = "%s%s?token=%s" % (settings.SITE_ROOT, path, token)
  58. ctx = {
  59. "checks": self.user.check_set.order_by("created"),
  60. "now": now,
  61. "unsub_link": unsub_link
  62. }
  63. emails.report(self.user.email, ctx)
  64. def invite(self, user):
  65. member = Member(team=self, user=user)
  66. member.save()
  67. # Switch the invited user over to the new team so they
  68. # notice the new team on next visit:
  69. user.profile.current_team = self
  70. user.profile.save()
  71. user.profile.send_instant_login_link(self)
  72. class Member(models.Model):
  73. team = models.ForeignKey(Profile)
  74. user = models.ForeignKey(User)