import base64
|
|
import os
|
|
import uuid
|
|
from datetime import timedelta
|
|
|
|
from django.conf import settings
|
|
from django.contrib.auth.hashers import make_password
|
|
from django.contrib.auth.models import User
|
|
from django.core import signing
|
|
from django.core.urlresolvers import reverse
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
from hc.lib import emails
|
|
|
|
|
|
class ProfileManager(models.Manager):
|
|
|
|
def for_user(self, user):
|
|
profile, created = Profile.objects.get_or_create(user_id=user.id)
|
|
return profile
|
|
|
|
|
|
class Profile(models.Model):
|
|
user = models.OneToOneField(User, blank=True, null=True)
|
|
next_report_date = models.DateTimeField(null=True, blank=True)
|
|
reports_allowed = models.BooleanField(default=True)
|
|
ping_log_limit = models.IntegerField(default=100)
|
|
token = models.CharField(max_length=128, blank=True)
|
|
api_key = models.CharField(max_length=128, blank=True)
|
|
|
|
objects = ProfileManager()
|
|
|
|
def send_instant_login_link(self):
|
|
token = str(uuid.uuid4())
|
|
self.token = make_password(token)
|
|
self.save()
|
|
|
|
path = reverse("hc-check-token", args=[self.user.username, token])
|
|
ctx = {"login_link": settings.SITE_ROOT + path}
|
|
emails.login(self.user.email, ctx)
|
|
|
|
def send_set_password_link(self):
|
|
token = str(uuid.uuid4())
|
|
self.token = make_password(token)
|
|
self.save()
|
|
|
|
path = reverse("hc-set-password", args=[token])
|
|
ctx = {"set_password_link": settings.SITE_ROOT + path}
|
|
emails.set_password(self.user.email, ctx)
|
|
|
|
def set_api_key(self):
|
|
self.api_key = base64.urlsafe_b64encode(os.urandom(24))
|
|
self.save()
|
|
|
|
def send_report(self):
|
|
# reset next report date first:
|
|
now = timezone.now()
|
|
self.next_report_date = now + timedelta(days=30)
|
|
self.save()
|
|
|
|
token = signing.Signer().sign(uuid.uuid4())
|
|
path = reverse("hc-unsubscribe-reports", args=[self.user.username])
|
|
unsub_link = "%s%s?token=%s" % (settings.SITE_ROOT, path, token)
|
|
|
|
ctx = {
|
|
"checks": self.user.check_set.order_by("created"),
|
|
"now": now,
|
|
"unsub_link": unsub_link
|
|
}
|
|
|
|
emails.report(self.user.email, ctx)
|