# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2017-08-26 13:12
from __future__ import unicode_literals
import os.path

from django.db import migrations

from settings.settings import MEDIA_ROOT
from ticket.models import TicketAttachment
from api_v3.models import Attachment


def generate_and_copy_old_file_names(apps, schema_editor):
    old_attachments = TicketAttachment.objects.all()

    for att in old_attachments:
        new_path = att.local_path.replace(MEDIA_ROOT + '/', '')

        if 'attachments/' in new_path:
            continue

        file_name = os.path.basename(new_path)
        Attachment.objects.filter(upload=file_name).update(upload=new_path)


class Migration(migrations.Migration):

    dependencies = [
        ('api_v3', '0007_v1_to_v2_tickets'),
    ]

    operations = [
        migrations.RunPython(generate_and_copy_old_file_names),
        migrations.RunSQL(
            'update api_v3_ticket set sent_notifications_at=now() '
            'where sent_notifications_at is null;'
        )
    ]

Migrate v1 attachment file names.
Migrate v1 attachment file names.
