# Path: api_v3/models/attachment.py
# class Attachment(models.Model):
#     """Ticket attachment model."""
# 
#     ticket = models.ForeignKey(
#         Ticket, blank=False, related_name='attachments', db_index=True,
#         on_delete=models.DO_NOTHING)
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL, blank=False, db_index=True,
#         on_delete=models.DO_NOTHING)
#     upload = models.FileField(upload_to='attachments/%Y/%m/%d', max_length=255)
#     created_at = models.DateTimeField(auto_now_add=True)
# 
#     @classmethod
#     def filter_by_user(cls, user, queryset=None):
#         """Returns any user accessible attachments.
# 
#         Ones he has access to through the tickets.
#         """
#         if queryset is None:
#             queryset = cls.objects
# 
#         return queryset.filter(ticket__in=Ticket.filter_by_user(user))


import os.path
from django.db import migrations
from django.conf import settings
from api_v3.models import Attachment
    from ticket.models import TicketAttachment
# Generated by Django 1.10.1 on 2017-08-26 13:12




try:
except ImportError:
    TicketAttachment = None


def generate_and_copy_old_file_names(apps, schema_editor):
    if not TicketAttachment:
        return

    old_attachments = TicketAttachment.objects.all()

    for att in old_attachments:
        new_path = att.local_path.replace(settings.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'),

# Path: api_v3/models/attachment.py
# class Attachment(models.Model):
#     """Ticket attachment model."""
# 
#     ticket = models.ForeignKey(
#         Ticket, blank=False, related_name='attachments', db_index=True,
#         on_delete=models.DO_NOTHING)
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL, blank=False, db_index=True,
#         on_delete=models.DO_NOTHING)
#     upload = models.FileField(upload_to='attachments/%Y/%m/%d', max_length=255)
#     created_at = models.DateTimeField(auto_now_add=True)
# 
#     @classmethod
#     def filter_by_user(cls, user, queryset=None):
#         """Returns any user accessible attachments.
# 
#         Ones he has access to through the tickets.
#         """
#         if queryset is None:
#             queryset = cls.objects
# 
#         return queryset.filter(ticket__in=Ticket.filter_by_user(user))


# Path: api_v3/migrations/0008_v1_to_v2_attachments.py
import os.path
from django.db import migrations
from django.conf import settings
from api_v3.models import Attachment
    from ticket.models import TicketAttachment

# Generated by Django 1.10.1 on 2017-08-26 13:12




try:
except ImportError:
    TicketAttachment = None


def generate_and_copy_old_file_names(apps, schema_editor):
    if not TicketAttachment:
        return

    old_attachments = TicketAttachment.objects.all()

    for att in old_attachments:
        new_path = att.local_path.replace(settings.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'),

    ]