|
1 | | -from simple_history.utils import bulk_create_with_history |
| 1 | +from simple_history.utils import bulk_create_with_history, bulk_update_with_history |
2 | 2 |
|
| 3 | +import app |
3 | 4 |
|
4 | | -def bulk_chunk_import(media_list, model, user): |
5 | | - """Bulk import media in chunks. |
6 | 5 |
|
7 | | - Fixes bulk_create_with_history limit |
8 | | - https://github.com/jazzband/django-simple-history/issues/1216#issuecomment-1903240831 |
9 | | - """ |
10 | | - chunk_size = 500 |
11 | | - for i in range(0, len(media_list), chunk_size): |
12 | | - bulk_create_with_history( |
13 | | - media_list[i : i + chunk_size], |
| 6 | +def bulk_chunk_import(bulk_media, model, user, mode): |
| 7 | + """Bulk import media in chunks.""" |
| 8 | + if mode == "new": |
| 9 | + num_imported = bulk_create_new_with_history(bulk_media, model, user) |
| 10 | + |
| 11 | + elif mode == "overwrite": |
| 12 | + num_imported = bulk_create_update_with_history( |
| 13 | + bulk_media, |
14 | 14 | model, |
15 | | - ignore_conflicts=True, |
| 15 | + user, |
| 16 | + ) |
| 17 | + |
| 18 | + return num_imported |
| 19 | + |
| 20 | + |
| 21 | +def bulk_create_new_with_history(bulk_media, model, user): |
| 22 | + """Filter out existing records and bulk create only new ones.""" |
| 23 | + # Get existing records' unique IDs since bulk_create_with_history |
| 24 | + # returns all objects even if they weren't created due to conflicts |
| 25 | + unique_fields = app.database.get_unique_constraint_fields(model) |
| 26 | + existing_combos = set( |
| 27 | + model.objects.values_list(*unique_fields), |
| 28 | + ) |
| 29 | + |
| 30 | + new_records = [ |
| 31 | + record |
| 32 | + for record in bulk_media |
| 33 | + if tuple(getattr(record, field + "_id") for field in unique_fields) |
| 34 | + not in existing_combos |
| 35 | + ] |
| 36 | + |
| 37 | + bulk_create_with_history( |
| 38 | + new_records, |
| 39 | + model, |
| 40 | + batch_size=500, |
| 41 | + default_user=user, |
| 42 | + ) |
| 43 | + |
| 44 | + return len(new_records) |
| 45 | + |
| 46 | + |
| 47 | +def bulk_create_update_with_history( |
| 48 | + bulk_media, |
| 49 | + model, |
| 50 | + user, |
| 51 | +): |
| 52 | + """Bulk create new records and update existing ones with history tracking.""" |
| 53 | + unique_fields = app.database.get_unique_constraint_fields(model) |
| 54 | + model_fields = app.database.get_fields(model) |
| 55 | + update_fields = [ |
| 56 | + field for field in model_fields if field not in unique_fields and field != "id" |
| 57 | + ] |
| 58 | + |
| 59 | + # Get existing objects with their unique fields and id |
| 60 | + existing_objs = model.objects.filter( |
| 61 | + **{ |
| 62 | + f"{field}__in": [getattr(obj, field + "_id") for obj in bulk_media] |
| 63 | + for field in unique_fields |
| 64 | + }, |
| 65 | + ).values(*unique_fields, "id") |
| 66 | + |
| 67 | + # Create lookup dictionary using unique field combinations |
| 68 | + existing_lookup = { |
| 69 | + tuple(obj[field] for field in unique_fields): obj["id"] for obj in existing_objs |
| 70 | + } |
| 71 | + |
| 72 | + # Split records into new and existing based on unique constraints |
| 73 | + create_objs = [] |
| 74 | + update_objs = [] |
| 75 | + |
| 76 | + for record in bulk_media: |
| 77 | + record_key = tuple(getattr(record, field + "_id") for field in unique_fields) |
| 78 | + if record_key in existing_lookup: |
| 79 | + # Set the primary key for update |
| 80 | + record.id = existing_lookup[record_key] |
| 81 | + update_objs.append(record) |
| 82 | + else: |
| 83 | + create_objs.append(record) |
| 84 | + |
| 85 | + # Bulk create new records |
| 86 | + num_created = 0 |
| 87 | + if create_objs: |
| 88 | + created_objs = bulk_create_with_history( |
| 89 | + create_objs, |
| 90 | + model, |
| 91 | + batch_size=500, |
16 | 92 | default_user=user, |
17 | 93 | ) |
| 94 | + num_created = len(created_objs) |
| 95 | + |
| 96 | + # Bulk update existing records |
| 97 | + num_updated = 0 |
| 98 | + if update_objs and update_fields: |
| 99 | + num_updated = bulk_update_with_history( |
| 100 | + update_objs, |
| 101 | + model, |
| 102 | + fields=update_fields, |
| 103 | + batch_size=500, |
| 104 | + default_user=user, |
| 105 | + ) |
| 106 | + |
| 107 | + return num_created + num_updated |
0 commit comments