|
| 1 | +# Copyright The IETF Trust 2016, All Rights Reserved |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from __future__ import unicode_literals, print_function |
| 4 | + |
| 5 | +import datetime |
| 6 | +from tqdm import tqdm |
| 7 | + |
| 8 | +from django.conf import settings |
| 9 | +from django.contrib.admin.utils import NestedObjects |
| 10 | +from django.contrib.auth.models import User |
| 11 | +from django.core.management.base import BaseCommand |
| 12 | +from django.db.models import F |
| 13 | + |
| 14 | +import debug # pyflakes:ignore |
| 15 | + |
| 16 | +from ietf.community.models import SearchRule |
| 17 | +from ietf.person.models import Person, Alias, PersonalApiKey, Email |
| 18 | +from ietf.person.name import unidecode_name |
| 19 | +from ietf.utils.log import log |
| 20 | + |
| 21 | +class Command(BaseCommand): |
| 22 | + help = (u""" |
| 23 | +
|
| 24 | + Delete data for which consent to store the data has not been given, |
| 25 | + where the data does not fall under the GDPR Legitimate Interest clause |
| 26 | + for the IETF. This includes full name, ascii name, bio, login, |
| 27 | + notification subscriptions and email addresses that are not derived from |
| 28 | + published drafts or ietf roles. |
| 29 | +
|
| 30 | + """) |
| 31 | + |
| 32 | + def add_arguments(self, parser): |
| 33 | + parser.add_argument('-n', '--dry-run', action='store_true', default=False, |
| 34 | + help="Don't delete anything, just list what would be done.") |
| 35 | +# parser.add_argument('-d', '--date', help="Date of deletion (mentioned in message)") |
| 36 | + parser.add_argument('-m', '--minimum-response-time', metavar='TIME', type=int, default=14, |
| 37 | + help="Minimum response time, default: %(default)s days. Persons to whom a " |
| 38 | + "consent request email has been sent more recently than this will not " |
| 39 | + "be affected by the run.") |
| 40 | +# parser.add_argument('-r', '--rate', type=float, default=1.0, |
| 41 | +# help='Rate of sending mail, default: %(default)s/s') |
| 42 | +# parser.add_argument('user', nargs='*') |
| 43 | + |
| 44 | + |
| 45 | + def handle(self, *args, **options): |
| 46 | + dry_run = options['dry_run'] |
| 47 | + verbosity = int(options['verbosity']) |
| 48 | + event_type = 'gdpr_notice_email' |
| 49 | + settings.DEBUG = False # don't log to console |
| 50 | + |
| 51 | + # users |
| 52 | + users = User.objects.filter(person__isnull=True, username__contains='@') |
| 53 | + self.stdout.write("Found %d users without associated person records" % (users.count(), )) |
| 54 | + emails = Email.objects.filter(address__in=users.values_list('username', flat=True)) |
| 55 | + # fix up users that don't have person records, but have a username matching a nown email record |
| 56 | + self.stdout.write("Checking usernames against email records ...") |
| 57 | + for email in tqdm(emails): |
| 58 | + user = users.get(username=email.address) |
| 59 | + if email.person.user_id: |
| 60 | + if dry_run: |
| 61 | + self.stdout.write("Would delete user #%-6s (%s) %s" % (user.id, user.last_login, user.username)) |
| 62 | + else: |
| 63 | + log("Deleting user #%-6s (%s) %s: no person record, matching email has other user" % (user.id, user.last_login, user.username)) |
| 64 | + user_id = user.id |
| 65 | + user.delete() |
| 66 | + Person.history.filter(user_id=user_id).delete() |
| 67 | + Email.history.filter(history_user=user_id).delete() |
| 68 | + else: |
| 69 | + if dry_run: |
| 70 | + self.stdout.write("Would connect user #%-6s %s to person #%-6s %s" % (user.id, user.username, email.person.id, email.person.ascii_name())) |
| 71 | + else: |
| 72 | + log("Connecting user #%-6s %s to person #%-6s %s" % (user.id, user.username, email.person.id, email.person.ascii_name())) |
| 73 | + email.person.user_id = user.id |
| 74 | + email.person.save() |
| 75 | + # delete users without person records |
| 76 | + users = users.exclude(username__in=emails.values_list('address', flat=True)) |
| 77 | + if dry_run: |
| 78 | + self.stdout.write("Would delete %d users without associated person records" % (users.count(), )) |
| 79 | + else: |
| 80 | + if users.count(): |
| 81 | + log("Deleting %d users without associated person records" % (users.count(), )) |
| 82 | + assert not users.filter(person__isnull=False).exists() |
| 83 | + user_ids = users.values_list('id', flat=True) |
| 84 | + users.delete() |
| 85 | + assert not Person.history.filter(user_id__in=user_ids).exists() |
| 86 | + |
| 87 | + |
| 88 | + # persons |
| 89 | + self.stdout.write('Querying the database for person records without given consent ...') |
| 90 | + notification_cutoff = datetime.datetime.now() - datetime.timedelta(days=options['minimum_response_time']) |
| 91 | + persons = Person.objects.exclude(consent=True) |
| 92 | + persons = persons.exclude(id=1) # make sure we don't delete System ;-) |
| 93 | + self.stdout.write("Found %d persons with information for which we don't have consent." % (persons.count(), )) |
| 94 | + |
| 95 | + # Narrow to persons we don't have Legitimate Interest in, and delete those fully |
| 96 | + persons = persons.exclude(docevent__by=F('pk')) |
| 97 | + persons = persons.exclude(documentauthor__person=F('pk')).exclude(dochistoryauthor__person=F('pk')) |
| 98 | + persons = persons.exclude(email__liaisonstatement__from_contact__person=F('pk')) |
| 99 | + persons = persons.exclude(email__reviewrequest__reviewer__person=F('pk')) |
| 100 | + persons = persons.exclude(email__shepherd_dochistory_set__shepherd__person=F('pk')) |
| 101 | + persons = persons.exclude(email__shepherd_document_set__shepherd__person=F('pk')) |
| 102 | + persons = persons.exclude(iprevent__by=F('pk')) |
| 103 | + persons = persons.exclude(meetingregistration__person=F('pk')) |
| 104 | + persons = persons.exclude(message__by=F('pk')) |
| 105 | + persons = persons.exclude(name_from_draft='') |
| 106 | + persons = persons.exclude(personevent__time__gt=notification_cutoff, personevent__type=event_type) |
| 107 | + persons = persons.exclude(reviewrequest__requested_by=F('pk')) |
| 108 | + persons = persons.exclude(role__person=F('pk')).exclude(rolehistory__person=F('pk')) |
| 109 | + persons = persons.exclude(session__requested_by=F('pk')) |
| 110 | + persons = persons.exclude(submissionevent__by=F('pk')) |
| 111 | + self.stdout.write("Found %d persons with information for which we neither have consent nor legitimate interest." % (persons.count(), )) |
| 112 | + if persons.count() > 0: |
| 113 | + self.stdout.write("Deleting records for persons for which we have with neither consent nor legitimate interest ...") |
| 114 | + for person in (persons if dry_run else tqdm(persons)): |
| 115 | + if dry_run: |
| 116 | + self.stdout.write(("Would delete record #%-6d: (%s) %-32s %-48s" % (person.pk, person.time, person.ascii_name(), "<%s>"%person.email())).encode('utf8')) |
| 117 | + else: |
| 118 | + if verbosity > 1: |
| 119 | + # development aids |
| 120 | + collector = NestedObjects(using='default') |
| 121 | + collector.collect([person,]) |
| 122 | + objects = collector.nested() |
| 123 | + related = [ o for o in objects[-1] if not isinstance(o, (Alias, Person, SearchRule, PersonalApiKey)) ] |
| 124 | + if len(related) > 0: |
| 125 | + self.stderr.write("Person record #%-6s %s has unexpected related records" % (person.pk, person.ascii_name())) |
| 126 | + |
| 127 | + # Historical records using simple_history has on_delete=DO_NOTHING, so |
| 128 | + # we have to do explicit deletions: |
| 129 | + id = person.id |
| 130 | + person.delete() |
| 131 | + Person.history.filter(id=id).delete() |
| 132 | + Email.history.filter(person_id=id).delete() |
| 133 | + |
| 134 | + # Deal with remaining persons (lacking consent, but with legitimate interest) |
| 135 | + persons = Person.objects.exclude(consent=True) |
| 136 | + persons = persons.exclude(id=1) |
| 137 | + self.stdout.write("Found %d remaining persons with information for which we don't have consent." % (persons.count(), )) |
| 138 | + if persons.count() > 0: |
| 139 | + self.stdout.write("Removing personal information requiring consent ...") |
| 140 | + for person in (persons if dry_run else tqdm(persons)): |
| 141 | + fields = ', '.join(person.needs_consent()) |
| 142 | + if dry_run: |
| 143 | + self.stdout.write(("Would remove info for #%-6d: (%s) %-32s %-48s %s" % (person.pk, person.time, person.ascii_name(), "<%s>"%person.email(), fields)).encode('utf8')) |
| 144 | + else: |
| 145 | + if person.name_from_draft: |
| 146 | + log("Using name info from draft for #%-6d %s: no consent, no roles" % (person.pk, person)) |
| 147 | + person.name = person.name_from_draft |
| 148 | + person.ascii = unidecode_name(person.name_from_draft) |
| 149 | + if person.biography: |
| 150 | + log("Deleting biography for #%-6d %s: no consent, no roles" % (person.pk, person)) |
| 151 | + person.biography = '' |
| 152 | + person.save() |
| 153 | + if person.user_id: |
| 154 | + if User.objects.filter(id=person.user_id).exists(): |
| 155 | + log("Deleting communitylist for #%-6d %s: no consent, no roles" % (person.pk, person)) |
| 156 | + person.user.communitylist_set.all().delete() |
| 157 | + for email in person.email_set.all(): |
| 158 | + if not email.origin.split(':')[0] in ['author', 'role', 'reviewer', 'liaison', 'shepherd', ]: |
| 159 | + log("Deleting email <%s> for #%-6d %s: no consent, no roles" % (email.address, person.pk, person)) |
| 160 | + address = email.address |
| 161 | + email.delete() |
| 162 | + Email.history.filter(address=address).delete() |
| 163 | + |
| 164 | + emails = Email.objects.filter(origin='', person__consent=False) |
| 165 | + self.stdout.write("Found %d emails without origin for which we lack consent." % (emails.count(), )) |
| 166 | + if dry_run: |
| 167 | + self.stdout.write("Would delete %d email records without origin and consent" % (emails.count(), )) |
| 168 | + else: |
| 169 | + if emails.count(): |
| 170 | + log("Deleting %d email records without origin and consent" % (emails.count(), )) |
| 171 | + addresses = emails.values_list('address', flat=True) |
| 172 | + emails.delete() |
| 173 | + Email.history.filter(address__in=addresses).delete() |
| 174 | + |
0 commit comments