74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
|
|
"""
|
||
|
|
Add contributed_by field to Event and backfill from overloaded source field.
|
||
|
|
|
||
|
|
The admin dashboard stores community contributor identifiers (EVT-XXXXXXXX or email)
|
||
|
|
in the source field. This migration:
|
||
|
|
1. Adds a dedicated contributed_by CharField
|
||
|
|
2. Copies user identifiers from source → contributed_by
|
||
|
|
3. Normalizes source back to its intended choices ('eventify', 'community', 'partner')
|
||
|
|
"""
|
||
|
|
|
||
|
|
from django.db import migrations, models
|
||
|
|
|
||
|
|
|
||
|
|
def backfill_contributed_by(apps, schema_editor):
|
||
|
|
"""Move user identifiers from source to contributed_by."""
|
||
|
|
Event = apps.get_model('events', 'Event')
|
||
|
|
|
||
|
|
STANDARD_SOURCES = {'eventify', 'community', 'partner', 'eventify_team', 'official', ''}
|
||
|
|
|
||
|
|
for event in Event.objects.all().iterator():
|
||
|
|
source_val = (event.source or '').strip()
|
||
|
|
changed = False
|
||
|
|
|
||
|
|
# User identifier: contains @ (email) or starts with EVT- (eventifyId)
|
||
|
|
if source_val and source_val not in STANDARD_SOURCES and not source_val.startswith('partner:'):
|
||
|
|
event.contributed_by = source_val
|
||
|
|
event.source = 'community'
|
||
|
|
changed = True
|
||
|
|
|
||
|
|
# Normalize eventify_team → eventify
|
||
|
|
elif source_val == 'eventify_team':
|
||
|
|
event.source = 'eventify'
|
||
|
|
changed = True
|
||
|
|
|
||
|
|
# Normalize official → eventify
|
||
|
|
elif source_val == 'official':
|
||
|
|
event.source = 'eventify'
|
||
|
|
changed = True
|
||
|
|
|
||
|
|
if changed:
|
||
|
|
event.save(update_fields=['source', 'contributed_by'])
|
||
|
|
|
||
|
|
|
||
|
|
def reverse_backfill(apps, schema_editor):
|
||
|
|
"""Reverse: move contributed_by back to source."""
|
||
|
|
Event = apps.get_model('events', 'Event')
|
||
|
|
for event in Event.objects.exclude(contributed_by__isnull=True).exclude(contributed_by='').iterator():
|
||
|
|
event.source = event.contributed_by
|
||
|
|
event.contributed_by = None
|
||
|
|
event.save(update_fields=['source', 'contributed_by'])
|
||
|
|
|
||
|
|
|
||
|
|
class Migration(migrations.Migration):
|
||
|
|
|
||
|
|
dependencies = [
|
||
|
|
('events', '0010_merge_20260324_1443'),
|
||
|
|
]
|
||
|
|
|
||
|
|
operations = [
|
||
|
|
# Step 1: Add the field
|
||
|
|
migrations.AddField(
|
||
|
|
model_name='event',
|
||
|
|
name='contributed_by',
|
||
|
|
field=models.CharField(
|
||
|
|
blank=True,
|
||
|
|
help_text='Eventify ID (EVT-XXXXXXXX) or email of the community contributor',
|
||
|
|
max_length=100,
|
||
|
|
null=True,
|
||
|
|
),
|
||
|
|
),
|
||
|
|
# Step 2: Backfill data
|
||
|
|
migrations.RunPython(backfill_contributed_by, reverse_backfill),
|
||
|
|
]
|