|
| 1 | +# Generated by Django 2.2.17 on 2020-12-09 06:59 |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | +from ietf.person.name import plain_name |
| 6 | +from ietf.utils.mail import parseaddr |
| 7 | + |
| 8 | + |
| 9 | +def find_or_create_email(email_model, person_model, formatted_email, group): |
| 10 | + """Look up an email address or create if needed |
| 11 | + |
| 12 | + Also creates a Person if the email does not have one. Created Email will have |
| 13 | + the origin field set to the origin parameter to this method. |
| 14 | + """ |
| 15 | + name, address = parseaddr(formatted_email) |
| 16 | + if not address: |
| 17 | + raise ValueError('Could not parse email "%s"' % formatted_email) |
| 18 | + email, _ = email_model.objects.get_or_create( |
| 19 | + address=address, |
| 20 | + defaults=dict(origin='liaison contact: ' + group.acronym) |
| 21 | + ) |
| 22 | + |
| 23 | + if not email.person: |
| 24 | + person = person_model.objects.create(name=name if name else address) |
| 25 | + email.person = person |
| 26 | + email.save() |
| 27 | + |
| 28 | + # Display an alert if the formatted address sent from the Role will differ |
| 29 | + # from what was in the original contacts list |
| 30 | + if not email.person.plain and email.person.name == email.address: |
| 31 | + recreated_contact_email = email.address |
| 32 | + else: |
| 33 | + person_plain = email.person.plain if email.person.plain else plain_name(email.person.name) |
| 34 | + recreated_contact_email = "%s <%s>" % (person_plain, email.address) |
| 35 | + if recreated_contact_email != formatted_email: |
| 36 | + print('>> Note: address "%s" is now "%s" (%s)' % ( |
| 37 | + formatted_email, |
| 38 | + recreated_contact_email, |
| 39 | + group.acronym, |
| 40 | + )) |
| 41 | + return email |
| 42 | + |
| 43 | + |
| 44 | +def forward(apps, schema_editor): |
| 45 | + """Perform forward migration |
| 46 | + |
| 47 | + Creates liaison_contact and liaison_cc_contact Roles corresponding to existing |
| 48 | + LiaisonStatementGroupContact instances. |
| 49 | + """ |
| 50 | + Group = apps.get_model('group', 'Group') |
| 51 | + Role = apps.get_model('group', 'Role') |
| 52 | + Email = apps.get_model('person', 'Email') |
| 53 | + Person = apps.get_model('person', 'Person') |
| 54 | + |
| 55 | + RoleName = apps.get_model('name', 'RoleName') |
| 56 | + contact_role_name = RoleName.objects.get(slug='liaison_contact') |
| 57 | + cc_contact_role_name = RoleName.objects.get(slug='liaison_cc_contact') |
| 58 | + |
| 59 | + print() |
| 60 | + LiaisonStatementGroupContacts = apps.get_model('liaisons', 'LiaisonStatementGroupContacts') |
| 61 | + for lsgc in LiaisonStatementGroupContacts.objects.all(): |
| 62 | + group = lsgc.group |
| 63 | + for contact_email in lsgc.contacts.split(','): |
| 64 | + if contact_email: |
| 65 | + email = find_or_create_email(Email, Person, |
| 66 | + contact_email.strip(), |
| 67 | + group) |
| 68 | + Role.objects.create( |
| 69 | + group=group, |
| 70 | + name=contact_role_name, |
| 71 | + person=email.person, |
| 72 | + email=email, |
| 73 | + ) |
| 74 | + |
| 75 | + for contact_email in lsgc.cc_contacts.split(','): |
| 76 | + if contact_email: |
| 77 | + email = find_or_create_email(Email, Person, |
| 78 | + contact_email.strip(), |
| 79 | + group) |
| 80 | + Role.objects.create( |
| 81 | + group=group, |
| 82 | + name=cc_contact_role_name, |
| 83 | + person=email.person, |
| 84 | + email=email, |
| 85 | + ) |
| 86 | + |
| 87 | + # Now validate that we got them all. As much as possible, use independent code |
| 88 | + # to avoid replicating any bugs from the original migration. |
| 89 | + for group in Group.objects.all(): |
| 90 | + lsgc = LiaisonStatementGroupContacts.objects.filter(group_id=group.pk).first() |
| 91 | + |
| 92 | + if not lsgc: |
| 93 | + if group.role_set.filter(name__in=[contact_role_name, cc_contact_role_name]).exists(): |
| 94 | + raise ValueError('%s group has contact roles after migration but had no LiaisonStatementGroupContacts' % ( |
| 95 | + group.acronym, |
| 96 | + )) |
| 97 | + else: |
| 98 | + contacts = group.role_set.filter(name=contact_role_name) |
| 99 | + num_lsgc_contacts = len(lsgc.contacts.split(',')) if lsgc.contacts else 0 |
| 100 | + if len(contacts) != num_lsgc_contacts: |
| 101 | + raise ValueError( |
| 102 | + '%s group has %d contact(s) but only %d address(es) in its LiaisonStatementGroupContacts (contact addresses = "%s", LSGC.contacts="%s")' % ( |
| 103 | + group.acronym, len(contacts), num_lsgc_contacts, |
| 104 | + '","'.join([c.email.address for c in contacts]), |
| 105 | + lsgc.contacts, |
| 106 | + ) |
| 107 | + ) |
| 108 | + for contact in contacts: |
| 109 | + email = contact.email.address |
| 110 | + if email.lower() not in lsgc.contacts.lower(): |
| 111 | + raise ValueError( |
| 112 | + '%s group has "%s" contact but not found in LiaisonStatementGroupContacts.contacts = "%s"' % ( |
| 113 | + group.acronym, email, lsgc.contacts, |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + cc_contacts = group.role_set.filter(name=cc_contact_role_name) |
| 118 | + num_lsgc_cc_contacts = len(lsgc.cc_contacts.split(',')) if lsgc.cc_contacts else 0 |
| 119 | + if len(cc_contacts) != num_lsgc_cc_contacts: |
| 120 | + raise ValueError( |
| 121 | + '%s group has %d CC contact(s) but %d address(es) in its LiaisonStatementGroupContacts (cc_contact addresses = "%s", LSGC.cc_contacts="%s")' % ( |
| 122 | + group.acronym, len(cc_contacts), num_lsgc_cc_contacts, |
| 123 | + '","'.join([c.email.address for c in cc_contacts]), |
| 124 | + lsgc.cc_contacts, |
| 125 | + ) |
| 126 | + ) |
| 127 | + for cc_contact in cc_contacts: |
| 128 | + email = cc_contact.email.address |
| 129 | + if email.lower() not in lsgc.cc_contacts.lower(): |
| 130 | + raise ValueError( |
| 131 | + '%s group has "%s" CC contact but not found in LiaisonStatementGroupContacts.cc_contacts = "%s"' % ( |
| 132 | + group.acronym, email, lsgc.cc_contacts, |
| 133 | + ) |
| 134 | + ) |
| 135 | + |
| 136 | +def reverse(apps, schema_editor): |
| 137 | + """Perform reverse migration |
| 138 | + |
| 139 | + Removes liaison_contact and liaison_cc_contact Roles. The forward migration creates missing |
| 140 | + Email and Person instances, but these are not removed because it's difficult to do this |
| 141 | + safely and correctly. |
| 142 | + """ |
| 143 | + Role = apps.get_model('group', 'Role') |
| 144 | + Role.objects.filter( |
| 145 | + name_id__in=['liaison_contact', 'liaison_cc_contact'] |
| 146 | + ).delete() |
| 147 | + |
| 148 | + |
| 149 | +class Migration(migrations.Migration): |
| 150 | + |
| 151 | + dependencies = [ |
| 152 | + ('group', '0040_lengthen_used_roles_fields'), |
| 153 | + ('name', '0022_add_liaison_contact_rolenames'), |
| 154 | + ] |
| 155 | + |
| 156 | + operations = [ |
| 157 | + migrations.RunPython(forward, reverse), |
| 158 | + ] |
0 commit comments