rug-website/rugwebsite/models.py
2018-05-04 15:09:27 +02:00

72 lines
2.5 KiB
Python
Executable File

from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_out
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
from django.db import models
from django.utils import timezone
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.conf import settings
class GDPRAgreed(models.Model):
user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
agreed_at = models.DateTimeField()
class PendingGDPRAgree(models.Model):
user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
token = models.TextField()
def agree(self):
GDPRAgreed(user=self.user, agreed_at=timezone.now()).save()
self.delete()
def email(self):
html_content = get_template('rugwebsite/email/gdpr_consent.html').render(
{'user': self.user, 'token': self.token, 'url': settings.SITE_URL}
)
text_content = get_template('rugwebsite/email/gdpr_consent.txt').render(
{'user': self.user, 'token': self.token, 'url': settings.SITE_URL}
)
msg = EmailMultiAlternatives(
'Over uw persoonsgegevens',
text_content,
'no-reply@cosmo.service.rug.nl',
[self.user.email])
msg.attach_alternative(html_content, "text/html")
msg.send()
class PendingGDPRDelete(models.Model):
user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
token = models.TextField()
def forget(self, request):
self.user.username = 'deleted_at_{}'.format(timezone.now())
self.user.first_name = ''
self.user.last_name = ''
self.user.email = ''
self.user.is_active = False
self.user.save()
user_logged_out.send(sender=self.user.__class__, request=request, user=self.user)
self.delete()
def email(self):
html_content = get_template('rugwebsite/email/gdpr_delete.html').render(
{'user': self.user, 'token': self.token, 'url': settings.SITE_URL}
)
text_content = get_template('rugwebsite/email/gdpr_delete.txt').render(
{'user': self.user, 'token': self.token, 'url': settings.SITE_URL}
)
msg = EmailMultiAlternatives(
'Verzoek verwijdering persoonsgegevens',
text_content,
'cosmo@rug.nl',
[self.user.email])
msg.attach_alternative(html_content, "text/html")
msg.send(fail_silently=False)