|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +#from django.db import models, migrations # pyflakes:ignore |
| 5 | +from django.db import migrations |
| 6 | +from django.conf import settings |
| 7 | + |
| 8 | +from ietf.utils.mail import send_mail_text |
| 9 | + |
| 10 | +# Tiny detail: making the text below look nice is of course dependent on |
| 11 | +# how long the email addresses and draft name which are inserted into the |
| 12 | +# text actually turn out to be. Rather than bothering to re-flow the text, |
| 13 | +# it can be noted that the average email address length, calculated over the |
| 14 | +# addresses this will be run for, was 21, and the length of the string |
| 15 | +# '%(inacive_email)s' is 18, so just formatting naturally should come out |
| 16 | +# pretty nicely in most cases. |
| 17 | + |
| 18 | +email_template = """ |
| 19 | +Hi, |
| 20 | +
|
| 21 | +As part of a review of active and inactive email addresses in the datatracker, |
| 22 | +it has been found that your address <%(inactive_email)s>, which is mentioned |
| 23 | +in the active draft %(draft_name)s, was set to inactive. |
| 24 | +
|
| 25 | +As a consequence of this, it would not be receiving notifications related to |
| 26 | +that draft. The notifications would instead have gone to <%(primary_email)s>. |
| 27 | +
|
| 28 | +This most likely was not what you intended when you used <%(inactive_email)s> |
| 29 | +in the draft, so that address has now been set to active. However, if you |
| 30 | +have manually set the status for <%(inactive_email)s> to inactive, we |
| 31 | +apologize for interfering and now having to ask you to go and set it to |
| 32 | +inactive again, at https://datatracker.ietf.org/accounts/profile/ . |
| 33 | +
|
| 34 | +Best regards, |
| 35 | +
|
| 36 | + Henrik, via the inactive email migration script |
| 37 | +
|
| 38 | +""" |
| 39 | + |
| 40 | +def activate_draft_email(apps, schema_editor): |
| 41 | + Document = apps.get_model("doc", "Document") |
| 42 | + print("Setting email addresses to active ...") |
| 43 | + count = 0 |
| 44 | + for doc in Document.objects.filter(type__slug='draft', states__slug='active'): |
| 45 | + for email in doc.authors.all(): |
| 46 | + if email.active == False: |
| 47 | + primary = email.person.email_set.filter(active=True).order_by('-time').first() |
| 48 | + email.active = True |
| 49 | + email.save() |
| 50 | + count += 1 |
| 51 | + # If there isn't a primary address, ther's no active |
| 52 | + # addresses, and it can't be other than right to change the |
| 53 | + # draft email address to active. Otherwise, notify the owner. |
| 54 | + if primary and settings.SERVER_MODE == 'production': |
| 55 | + primary_email = primary.address |
| 56 | + inactive_email = email.address |
| 57 | + context = dict( |
| 58 | + primary_email=primary_email, |
| 59 | + inactive_email=inactive_email, |
| 60 | + draft_name=doc.name, |
| 61 | + ) |
| 62 | + send_mail_text( |
| 63 | + request=None, |
| 64 | + to=[ primary_email, inactive_email ], |
| 65 | + frm="Henrik Levkowetz <henrik@levkowetz.com>", |
| 66 | + subject="Changed email settings for you in the datatracker", |
| 67 | + txt= email_template % context, |
| 68 | + extra={"Reply-To": "Secretariat <ietf-action@ietf.org>"}, |
| 69 | + ) |
| 70 | + print("Set %s email addresses to active" % count) |
| 71 | + |
| 72 | +def deactivate_draft_email(apps, scema_editor): |
| 73 | + """ |
| 74 | + The backwards migration doesn't touch the active field of any email addresses. |
| 75 | + We don't have the information to exactly undo what the forward migration did, |
| 76 | + and on 08 Mar 2015, there were 1931 inactive email addresses coupled to active |
| 77 | + drafts, and 4237 active addresses coupled to active drafts. The harm would |
| 78 | + be substantial if those active addresses were set to inactive. |
| 79 | + """ |
| 80 | +# Document = apps.get_model("doc", "Document") |
| 81 | +# print(" Not setting email addresses to inactive") |
| 82 | +# count = 0 |
| 83 | +# for doc in Document.objects.filter(type__slug='draft', states__slug='active'): |
| 84 | +# for email in doc.authors.all(): |
| 85 | +# if email.active == True: |
| 86 | +# #print email.address |
| 87 | +# count += 1 |
| 88 | +# print("Left %s active email addresses untouched " % count) |
| 89 | + |
| 90 | +class Migration(migrations.Migration): |
| 91 | + |
| 92 | + dependencies = [ |
| 93 | + ('person', '0003_auto_20150304_0829'), |
| 94 | + ('doc', '0002_auto_20141222_1749'), |
| 95 | + ] |
| 96 | + |
| 97 | + operations = [ |
| 98 | + migrations.RunPython( |
| 99 | + activate_draft_email, |
| 100 | + deactivate_draft_email), |
| 101 | + ] |
0 commit comments