Skip to content

Commit 619b20d

Browse files
committed
Data migration to assign email origin based on existing records (author, role, and more).
- Legacy-Id: 15148
1 parent f47e1ff commit 619b20d

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.12 on 2018-05-10 05:28
3+
from __future__ import unicode_literals
4+
5+
import sys
6+
7+
from django.db import migrations
8+
9+
import debug
10+
11+
def populate_email_origin(apps, schema_editor):
12+
Submission = apps.get_model('submit', 'Submission')
13+
Document = apps.get_model('doc', 'Document')
14+
DocHistory = apps.get_model('doc', 'DocHistory')
15+
DocumentAuthor = apps.get_model('doc', 'DocumentAuthor')
16+
DocHistoryAuthor= apps.get_model('doc', 'DocHistoryAuthor')
17+
Role = apps.get_model('group', 'Role')
18+
RoleHistory = apps.get_model('group', 'RoleHistory')
19+
ReviewRequest = apps.get_model('review', 'ReviewRequest')
20+
LiaisonStatement= apps.get_model('liaisons', 'LiaisonStatement')
21+
#
22+
Email = apps.get_model('person', 'Email')
23+
#
24+
sys.stdout.write("\n")
25+
#
26+
sys.stdout.write("\n ** This migration may take some time. Expect at least a few minutes **.\n\n")
27+
sys.stdout.write(" Initializing data structures...\n")
28+
emails = dict([ (e.address, e) for e in Email.objects.filter(origin='') ])
29+
30+
count = 0
31+
sys.stdout.write(" Assigning email origins from Submission records...\n")
32+
for o in Submission.objects.all().order_by('-submission_date'):
33+
for a in o.authors:
34+
addr = a['email']
35+
if addr in emails:
36+
e = emails[addr]
37+
if e.origin != o.name:
38+
e.origin = "author: %s" % o.name
39+
count += 1
40+
e.save()
41+
del emails[addr]
42+
sys.stdout.write(" Submission email origins assigned: %d\n" % count)
43+
44+
for model in (DocumentAuthor, DocHistoryAuthor, ):
45+
count = 0
46+
sys.stdout.write(" Assigning email origins from %s records...\n" % model.__name__)
47+
for o in model.objects.filter(email__origin=''):
48+
if not o.email.origin:
49+
o.email.origin = "author: %s" % o.document.name
50+
o.email.save()
51+
count += 1
52+
sys.stdout.write(" %s email origins assigned: %d\n" % (model.__name__, count))
53+
54+
for model in (Role, RoleHistory, ):
55+
count = 0
56+
sys.stdout.write(" Assigning email origins from %s records...\n" % model.__name__)
57+
for o in model.objects.filter(email__origin=''):
58+
if not o.email.origin:
59+
o.email.origin = "role: %s %s" % (o.group.acronym, o.name.slug)
60+
o.email.save()
61+
count += 1
62+
sys.stdout.write(" %s email origins assigned: %d\n" % (model.__name__, count))
63+
64+
for model in (ReviewRequest, ):
65+
count = 0
66+
sys.stdout.write(" Assigning email origins from %s records...\n" % model.__name__)
67+
for o in model.objects.filter(reviewer__origin=''):
68+
if not o.reviewer.origin:
69+
o.reviewer.origin = "reviewer: %s" % (o.doc.name)
70+
o.reviewer.save()
71+
count += 1
72+
sys.stdout.write(" %s email origins assigned: %d\n" % (model.__name__, count))
73+
74+
for model in (LiaisonStatement, ):
75+
count = 0
76+
sys.stdout.write(" Assigning email origins from %s records...\n" % model.__name__)
77+
for o in model.objects.filter(from_contact__origin=''):
78+
if not o.from_contact.origin:
79+
o.from_contact.origin = "liaison: %s" % (','.join([ g.acronym for g in o.from_groups.all() ]))
80+
o.from_contact.save()
81+
count += 1
82+
sys.stdout.write(" %s email origins assigned: %d\n" % (model.__name__, count))
83+
84+
for model in (Document, DocHistory, ):
85+
count = 0
86+
sys.stdout.write(" Assigning email origins from %s records...\n" % model.__name__)
87+
for o in model.objects.filter(shepherd__origin=''):
88+
if not o.shepherd.origin:
89+
o.shepherd.origin = "shepherd: %s" % o.name
90+
o.shepherd.save()
91+
count += 1
92+
sys.stdout.write(" %s email origins assigned: %d\n" % (model.__name__, count))
93+
94+
sys.stdout.write("\n")
95+
sys.stdout.write(" Email records with origin indication: %d\n" % Email.objects.exclude(origin='').count())
96+
sys.stdout.write(" Email records without origin indication: %d\n" % Email.objects.filter(origin='').count())
97+
98+
def reverse(apps, schema_editor):
99+
pass
100+
101+
class Migration(migrations.Migration):
102+
103+
dependencies = [
104+
('person', '0003_auto_20180504_1519'),
105+
]
106+
107+
operations = [
108+
migrations.RunPython(populate_email_origin, reverse)
109+
]

0 commit comments

Comments
 (0)