Skip to content

Commit 20d8569

Browse files
feat(rgpd): fills previous activity at better
1 parent 36b2224 commit 20d8569

2 files changed

Lines changed: 73 additions & 12 deletions

File tree

recoco/apps/home/migrations/0040_fill_profile_previous_activity_site.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
# Generated by Django 5.1.15 on 2026-02-04 13:21
22

33
from django.db import migrations
4-
from django.db.models import Subquery, OuterRef, Count, F
4+
from django.db.models import Subquery, OuterRef, Count
55

66

77
def fill_previous_activity_site(apps, schema_editor):
88
UserProfile = apps.get_model("home", "UserProfile")
9-
User = apps.get_model("auth", "User")
10-
11-
(
12-
UserProfile.all.filter(previous_activity_at__lt=F("user__last_login")).update(
13-
previous_activity_at=Subquery(
14-
User.objects.filter(pk=OuterRef("user_id")).values_list("last_login")[
15-
:1
16-
]
17-
)
18-
)
19-
)
209

2110
Site = apps.get_model("sites", "Site")
2211
counted_sites_profiles = UserProfile.objects.annotate(count_site=Count("sites"))
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Generated by Django 5.1.15 on 2026-02-04 13:21
2+
3+
from django.db import migrations
4+
from django.db.models import Subquery, OuterRef, F, Q
5+
from tqdm import tqdm
6+
7+
8+
def fill_previous_activity_site(apps, schema_editor):
9+
UserProfile = apps.get_model("home", "UserProfile")
10+
User = apps.get_model("auth", "User")
11+
12+
# ensures there is no None previous_activity_at
13+
(
14+
UserProfile.objects.filter(
15+
Q(previous_activity_at__isnull=True)
16+
| Q(previous_activity_at__lt=F("user__last_login"))
17+
).update(
18+
previous_activity_at=Subquery(
19+
User.objects.filter(pk=OuterRef("user_id")).values_list("last_login")[
20+
:1
21+
]
22+
)
23+
)
24+
)
25+
UserProfile.objects.filter(previous_activity_at__isnull=True).update(
26+
previous_activity_at=Subquery(
27+
User.objects.filter(pk=OuterRef("user_id")).values_list("date_joined")[:1]
28+
)
29+
)
30+
31+
# tries to set later dates based on activity
32+
Action = apps.get_model("actstream", "Action")
33+
for profile in tqdm(UserProfile.objects.all()):
34+
last_trace = (
35+
Action.objects.filter(actor_object_id=profile.user.id)
36+
.filter(timestamp__gt=profile.previous_activity_at)
37+
.order_by("-timestamp")
38+
.first()
39+
)
40+
if last_trace is not None:
41+
profile.previous_activity_at = last_trace.timestamp
42+
43+
last_msg = (
44+
profile.user.project_messages.filter(
45+
created__gt=profile.previous_activity_at
46+
)
47+
.order_by("-created")
48+
.first()
49+
)
50+
if last_msg is not None:
51+
profile.previous_activity_at = last_msg.created
52+
53+
profile.save()
54+
55+
56+
class Migration(migrations.Migration):
57+
dependencies = [
58+
(
59+
"home",
60+
"0040_fill_profile_previous_activity_site",
61+
),
62+
("actstream", "0004_add_multisite_support"),
63+
("conversations", "0007_auto_20251230_1707"),
64+
("auth", "0012_alter_user_first_name_max_length"),
65+
]
66+
67+
operations = [
68+
migrations.RunPython(
69+
fill_previous_activity_site,
70+
migrations.RunPython.noop,
71+
),
72+
]

0 commit comments

Comments
 (0)