# Generated by Django 2.2.9 on 2020-01-28 13:23

from django.db import migrations


def reformat_contacts(apps, schema_editor):
    """Format contacts as html.

    The contact field used to be raw text of this form:

      Nom : XXX
      Téléphone: XXX
      etc.

    But since we converted the field to html, old values now appear on
    a single line.

    Thus, we just convert those raw text lists to html.
    """
    Aid = apps.get_model("aids", "Aid")
    for aid in Aid.objects.all():
        contact = aid.contact
        if "\n" in contact and "<li>" not in contact:
            contact = contact.replace("\r", "")
            contact_elements = filter(None, contact.split("\n"))
            html_elements = ["<li>{}</li>".format(elt) for elt in contact_elements]

            aid.contact = "<ul>{}</ul>".format("".join(html_elements))
            aid.save()


class Migration(migrations.Migration):

    dependencies = [
        ("aids", "0098_auto_20191220_1103"),
    ]

    operations = [migrations.RunPython(reformat_contacts)]
