This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdpr.py
339 lines (296 loc) · 11.5 KB
/
gdpr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from __future__ import annotations
import datetime
import functools
import io
import itertools
import json
import math
import zipfile
from pathlib import Path
from typing import TYPE_CHECKING, Any, Optional, cast
import graphene
from django.conf import settings
from django.core.files.storage import default_storage
from django.core.mail import EmailMultiAlternatives, get_connection
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Case, CharField, F, QuerySet, Value, When
from django.db.models.fields.files import FieldFile
from django.db.models.functions import Concat, Replace
from django.http import HttpRequest
from django.template.loader import render_to_string
from django.utils import timezone, translation
from django.utils.html import strip_tags
from graphene_django.types import ErrorType
from skole.models import (
Activity,
Badge,
Comment,
DailyVisit,
SkoleModel,
Star,
Thread,
User,
Vote,
)
from skole.overridden import login_required
from skole.schemas.base import SkoleObjectType
from skole.types import JsonList, ResolveInfo
from skole.utils.constants import Errors, Messages, Notifications, VoteConstants
from skole.utils.files import override_s3_file_age
from skole.utils.shortcuts import to_form_error
if TYPE_CHECKING: # pragma: no cover
# https://stackoverflow.com/a/63520010/9835872
from django.db.models.query import ( # pylint: disable=no-name-in-module
ValuesQuerySet,
)
class DjangoQuerySetJSONEncoder(DjangoJSONEncoder):
"""JSON encoder which can serialize QuerySets."""
def default(self, o: Any) -> Any:
if isinstance(o, QuerySet):
return list(o)
else:
return super().default(o)
class JSONString(graphene.JSONString):
"""A graphene JSON field which handles serializing datetimes, QuerySets and more."""
@staticmethod
def serialize(dt: Any) -> str:
return json.dumps(dt, cls=DjangoQuerySetJSONEncoder)
class MyDataMutation(SkoleObjectType, graphene.Mutation):
"""Email the user with a link to a zip file containing **all** of their data."""
# Can't inherit `SkoleCreateUpdateMutationMixin` since this doesn't use a form.
# Can't use `SuccessMessageMixin` since it only works with `BaseDjangoFormMutation`.
success_message = graphene.String()
errors = graphene.List(ErrorType, default_value=[])
@classmethod
@login_required
def mutate(cls, root: None, info: ResolveInfo) -> MyDataMutation:
# TODO: this could use fire-and-forget style async execution.
user = cast(User, info.context.user)
if remaining := cls.__time_until_can_request_again(user):
return cls(errors=to_form_error(Errors.RATE_LIMITED.format(remaining)))
with translation.override(settings.LANGUAGE_CODE):
# Everything in EN for consistency.
data = {
# Model fields
"id": user.id,
"username": user.username,
"email": user.email,
"title": user.title,
"bio": user.bio,
"avatar": user.avatar.name if user.avatar else None,
"score": user.score,
"verified": user.verified,
"is_active": user.is_active,
"last_login": user.last_login,
"modified": user.modified,
"created": user.created,
"last_my_data_query": user.last_my_data_query,
# Related models
"last_visit": cls._last_visit(user),
"daily_visits": cls._daily_visits(user),
"created_comments": cls._created_comments(user),
"created_threads": cls._created_threads(user),
"badges": cls._badges(user),
"badgeProgresses": cls._badge_progresses(user),
"selectedBadgeProgress": cls._selected_badge_progress(user),
"votes": cls._votes(user),
"stars": cls._stars(user),
"activities": cls._activities(user),
"caused_activities": cls._caused_activities(user),
}
json_data = json.dumps(
data,
indent=4,
ensure_ascii=False,
cls=DjangoQuerySetJSONEncoder,
)
data_url = cls.__create_zip(user, json_data, request=info.context)
cls.__send_email(user, data_url)
return cls(success_message=Messages.DATA_REQUEST_RECEIVED)
@classmethod
def _created_comments(cls, user: User) -> ValuesQuerySet[Comment, Any]:
return user.comments.values(
"id",
"text",
"modified",
"created",
target=cls.__target_case(Comment),
uploaded_file=cls.__file_case("file"),
uploaded_image=cls.__file_case("image"),
)
@classmethod
def _created_threads(cls, user: User) -> ValuesQuerySet[Thread, Any]:
return user.created_threads.values(
"id",
"title",
"text",
"modified",
"created",
uploaded_image=cls.__file_case("image"),
)
@classmethod
def _activities(cls, user: User) -> ValuesQuerySet[Activity, Any]:
return user.activities.values(
"id",
"read",
target=cls.__target_case(Activity),
type=cls.__activity_type_name(),
)
@classmethod
def _caused_activities(cls, user: User) -> ValuesQuerySet[Activity, Any]:
return user.caused_activities.values(
"id",
"read",
type=cls.__activity_type_name(),
target=cls.__target_case(Activity),
)
@classmethod
def _votes(cls, user: User) -> ValuesQuerySet[Vote, Any]:
return user.votes.values(
"id",
target=cls.__target_case(Vote),
vote=Case(
*(
When(status=value, then=Value(display))
for value, display in VoteConstants.STATUS
),
output_field=CharField(),
),
)
@classmethod
def _stars(cls, user: User) -> QuerySet[Star]:
return user.stars.annotate(
target=cls.__target_case(Star),
).values_list("target", flat=True)
@staticmethod
def _badges(user: User) -> QuerySet[Badge]:
return (
Badge.objects.translated()
.filter(
badge_progresses__user=user, badge_progresses__acquired__isnull=False
)
.values_list("translations__name", flat=True)
)
@staticmethod
def _badge_progresses(user: User) -> JsonList:
badge_progresses = (
user.get_or_create_badge_progresses()
.filter(badge__translations__language_code=settings.LANGUAGE_CODE)
.values_list("badge__translations__name", "progress", "badge__steps")
)
return [
{"badge": badge, "progress": f"{progress} / {steps}"}
for (badge, progress, steps) in badge_progresses
]
@staticmethod
def _selected_badge_progress(user: User) -> str:
return (
user.selected_badge_progress.badge.name
if user.selected_badge_progress
else None
)
@staticmethod
def _uploaded_files(user: User) -> set[FieldFile]:
# Objects basically shouldn't have duplicate files, but our test data
# at least has those, so better to filter them away with a `set`.
return {
file
for file in itertools.chain(
(comment.file for comment in user.comments.all()),
(comment.image for comment in user.comments.all()),
(user.avatar,),
)
if file
}
@staticmethod
def _last_visit(user: User) -> Optional[datetime.datetime]:
return (
DailyVisit.objects.filter(user=user)
.order_by("pk")
.values_list("last_visit", flat=True)
.last()
)
@staticmethod
def _daily_visits(user: User) -> ValuesQuerySet[DailyVisit, Any]:
return (
DailyVisit.objects.filter(user=user).order_by("pk").values("date", "visits")
)
@classmethod
def __create_zip(cls, user: User, json_data: str, request: HttpRequest) -> str:
assert user.last_my_data_query is not None # Cannot be `None` anymore.
file = Path(
f"{user.username}_data_{user.last_my_data_query.strftime('%Y%m%d')}.zip"
)
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as f:
for uploaded in cls._uploaded_files(user):
f.writestr(f"{file.stem}/{uploaded.name}", uploaded.read())
f.writestr(f"{file.stem}/data.json", json_data)
with override_s3_file_age(settings.MY_DATA_FILE_AVAILABLE_FOR):
storage_name = default_storage.save(
name=f"generated/my_data/{file.name}", content=buffer
)
url = default_storage.url(name=storage_name)
if settings.DEBUG:
# In dev env this is just the file path, so let's make it an absolute one.
return request.build_absolute_uri(url)
else:
# In production this will already be a full absolute S3 URL.
return url
@classmethod
def __send_email(cls, user: User, data_url: str) -> None:
html_message = render_to_string(
"email/my_data.html",
context={
"user": user,
"url": data_url,
"days": settings.MY_DATA_FILE_AVAILABLE_FOR.days,
},
)
mail = EmailMultiAlternatives(
subject=Notifications.MY_DATA_SUBJECT,
body=strip_tags(html_message),
from_email=settings.EMAIL_ADDRESS,
to=[user.email],
connection=get_connection(),
)
mail.attach_alternative(html_message, "text/html")
mail.send()
@staticmethod
def __time_until_can_request_again(user: User) -> int:
# We could use something like django-ratelimit for this, but that would require
# us to setup a cross process cache solution (like memcached) and it just feels
# such an overkill for a simple tasks like this.
if last_queried := user.last_my_data_query:
now = timezone.now()
if last_queried > now - settings.MY_DATA_RATE_LIMIT:
remaining = settings.MY_DATA_RATE_LIMIT - (now - last_queried)
remaining_time_in_minutes = math.ceil(remaining.total_seconds() / 60)
return remaining_time_in_minutes
user.update_last_my_data_query()
return 0
@staticmethod
@functools.cache
def __target_case(model: type[SkoleModel]) -> Case:
cases = {
"comment": When(
comment__isnull=False, then=Concat(Value("comment "), "comment")
),
"thread": When(
thread__isnull=False, then=Concat(Value("thread "), "thread")
),
}
return Case(
*(value for key, value in cases.items() if hasattr(model, key)),
output_field=CharField(),
)
@staticmethod
@functools.cache
def __activity_type_name() -> Replace:
return Replace(F("activity_type__identifier"), Value("_"), Value(" "))
@staticmethod
@functools.cache
def __file_case(field: str) -> Case:
return Case(When(**{field: ""}, then=None), default=field)
class Mutation(SkoleObjectType):
my_data = MyDataMutation.Field()