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.

88 lines
2.8 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
  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. "login_link": settings.SITE_ROOT + path,
  33. "inviting_profile": inviting_profile
  34. }
  35. emails.login(self.user.email, ctx)
  36. def send_set_password_link(self):
  37. token = str(uuid.uuid4())
  38. self.token = make_password(token)
  39. self.save()
  40. path = reverse("hc-set-password", args=[token])
  41. ctx = {"set_password_link": settings.SITE_ROOT + path}
  42. emails.set_password(self.user.email, ctx)
  43. def set_api_key(self):
  44. self.api_key = base64.urlsafe_b64encode(os.urandom(24))
  45. self.save()
  46. def send_report(self):
  47. # reset next report date first:
  48. now = timezone.now()
  49. self.next_report_date = now + timedelta(days=30)
  50. self.save()
  51. token = signing.Signer().sign(uuid.uuid4())
  52. path = reverse("hc-unsubscribe-reports", args=[self.user.username])
  53. unsub_link = "%s%s?token=%s" % (settings.SITE_ROOT, path, token)
  54. ctx = {
  55. "checks": self.user.check_set.order_by("created"),
  56. "now": now,
  57. "unsub_link": unsub_link
  58. }
  59. emails.report(self.user.email, ctx)
  60. def invite(self, user):
  61. member = Member(team=self, user=user)
  62. member.save()
  63. # Switch the invited user over to the new team so they
  64. # notice the new team on next visit:
  65. user.profile.current_team = self
  66. user.profile.save()
  67. user.profile.send_instant_login_link(self)
  68. class Member(models.Model):
  69. team = models.ForeignKey(Profile)
  70. user = models.ForeignKey(User)