|
| 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