# Generated by Django 3.1.6 on 2021-02-17 11:09

import django.core.validators
from django.db import migrations, models
import dynamic_filenames
import stdimage.models


class Migration(migrations.Migration):
    dependencies = [
        ("people", "0003_segments"),
    ]

    operations = [
        migrations.AddField(
            model_name="person",
            name="display_name",
            field=models.CharField(
                default="", max_length=255, verbose_name="nom d'affichage"
            ),
            preserve_default=False,
        ),
        migrations.RunSQL(
            """
                UPDATE
                  people_person
                SET
                  display_name = UPPER(
                    CASE
                    WHEN first_name <> '' AND last_name <> '' THEN concat(
                      SUBSTRING(first_name, 1, 1),
                      SUBSTRING(last_name, 1, 1)
                    )
                    WHEN first_name <> '' THEN SUBSTRING(first_name, 1, 2)
                    WHEN last_name <> '' THEN SUBSTRING(last_name, 1, 2)
                    WHEN (
                      SELECT address FROM people_personemail
                      WHERE people_personemail.person_id = people_person.id
                      LIMIT 1
                    ) IS NOT NULL THEN (
                      SELECT SUBSTRING(address, 1, 2)
                      FROM people_personemail
                      WHERE people_personemail.person_id = people_person.id
                      LIMIT 1
                    )
                    ELSE '?' END
                  )
            """,
            "",
        ),
        migrations.AddField(
            model_name="person",
            name="image",
            field=stdimage.models.StdImageField(
                blank=True,
                help_text="Vous pouvez ajouter une image publique de profil",
                upload_to=dynamic_filenames.FilePattern(
                    filename_pattern="{app_label}/{model_name}/{instance.id}{ext}"
                ),
                validators=[
                    django.core.validators.FileExtensionValidator(
                        allowed_extensions=["jpg", "jpeg", "gif", "png"]
                    )
                ],
                verbose_name="image de profil",
            ),
        ),
    ]
