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.

68 lines
2.1 KiB

10 years ago
10 years ago
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.contrib.auth.hashers import make_password
  4. from django.contrib.auth.models import User
  5. from django.core import signing
  6. from django.core.urlresolvers import reverse
  7. from django.db import models
  8. from django.utils import timezone
  9. from hc.lib import emails
  10. import uuid
  11. class ProfileManager(models.Manager):
  12. def for_user(self, user):
  13. try:
  14. profile = self.get(user_id=user.id)
  15. except Profile.DoesNotExist:
  16. profile = Profile(user=user)
  17. profile.save()
  18. return profile
  19. class Profile(models.Model):
  20. user = models.OneToOneField(User, blank=True, null=True)
  21. next_report_date = models.DateTimeField(null=True, blank=True)
  22. reports_allowed = models.BooleanField(default=True)
  23. ping_log_limit = models.IntegerField(default=100)
  24. token = models.CharField(max_length=128, blank=True)
  25. objects = ProfileManager()
  26. def send_instant_login_link(self):
  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 = {"login_link": settings.SITE_ROOT + path}
  32. emails.login(self.user.email, ctx)
  33. def send_set_password_link(self):
  34. token = str(uuid.uuid4())
  35. self.token = make_password(token)
  36. self.save()
  37. path = reverse("hc-set-password", args=[token])
  38. ctx = {"set_password_link": settings.SITE_ROOT + path}
  39. emails.set_password(self.user.email, ctx)
  40. def send_report(self):
  41. # reset next report date first:
  42. now = timezone.now()
  43. self.next_report_date = now + timedelta(days=30)
  44. self.save()
  45. token = signing.Signer().sign(uuid.uuid4())
  46. path = reverse("hc-unsubscribe-reports", args=[self.user.username])
  47. unsub_link = "%s%s?token=%s" % (settings.SITE_ROOT, path, token)
  48. ctx = {
  49. "checks": self.user.check_set.order_by("created"),
  50. "now": now,
  51. "unsub_link": unsub_link
  52. }
  53. emails.report(self.user.email, ctx)