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.

48 lines
1.4 KiB

10 years ago
10 years ago
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.contrib.auth.models import User
  4. from django.core import signing
  5. from django.core.urlresolvers import reverse
  6. from django.db import models
  7. from django.utils import timezone
  8. from hc.lib import emails
  9. import uuid
  10. class ProfileManager(models.Manager):
  11. def for_user(self, user):
  12. try:
  13. profile = self.get(user_id=user.id)
  14. except Profile.DoesNotExist:
  15. profile = Profile(user=user)
  16. profile.save()
  17. return profile
  18. class Profile(models.Model):
  19. user = models.OneToOneField(User, blank=True, null=True)
  20. next_report_date = models.DateTimeField(null=True, blank=True)
  21. reports_allowed = models.BooleanField(default=True)
  22. ping_log_limit = models.IntegerField(default=100)
  23. objects = ProfileManager()
  24. def send_report(self):
  25. # reset next report date first:
  26. now = timezone.now()
  27. self.next_report_date = now + timedelta(days=30)
  28. self.save()
  29. token = signing.Signer().sign(uuid.uuid4())
  30. path = reverse("hc-unsubscribe-reports", args=[self.user.username])
  31. unsub_link = "%s%s?token=%s" % (settings.SITE_ROOT, path, token)
  32. ctx = {
  33. "checks": self.user.check_set.order_by("created"),
  34. "now": now,
  35. "unsub_link": unsub_link
  36. }
  37. emails.report(self.user.email, ctx)