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.

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