text: # Generated by Django 2.2.9 on 2020-02-19 10:17
  
  from django.db import migrations
  import taggit.managers
  
  # There is a known issue that all tags' method cannot be used inside migrations.
  # https://github.com/jazzband/django-taggit/issues/454
  # Direct importing is not perfect however it is the easiest way and least likely to lead to bugs.
  from data.models import Officer, Area, PoliceUnit, AttachmentFile
  
  officer_tags = {}
  area_tags = {}
  police_unit_tags = {}
  attachment_file_tags = {}
  
  
  def backup_old_tags(apps, schema_editor):
      Officer = apps.get_model('data', 'Officer')
      Area = apps.get_model('data', 'Area')
      PoliceUnit = apps.get_model('data', 'PoliceUnit')
      AttachmentFile = apps.get_model('data', 'AttachmentFile')
      for officer in Officer.objects.exclude(tags=[]):
          officer_tags[officer.id] = officer.tags
  
      for area in Area.objects.exclude(tags=[]):
          area_tags[area.id] = area.tags
  
      for police_unit in PoliceUnit.objects.exclude(tags=[]):
          police_unit_tags[police_unit.id] = police_unit.tags
  
      for attachment_file in AttachmentFile.objects.exclude(tags=[]):
          attachment_file_tags[attachment_file.id] = attachment_file.tags
  
  
  def restore_old_tags(apps, schema_editor):
      for officer_id, tags in officer_tags.items():
          officer = Officer.objects.get(id=officer_id)
          officer.tags.set(*tags)
  
      for area_id, tags in area_tags.items():
          area = Area.objects.get(id=area_id)
          area.tags.set(*tags)
  
      for police_unit_id, tags in police_unit_tags.items():
          police_unit = PoliceUnit.objects.get(id=police_unit_id)
          police_unit.tags.set(*tags)
  
      for attachment_file_id, tags in attachment_file_tags.items():
          attachment_file = AttachmentFile.objects.get(id=attachment_file_id)
          attachment_file.tags.set(*tags)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('taggit', '0003_taggeditem_add_unique_index'),
          ('data', '0119_add_is_extracted_summary_field_to_allegation'),
      ]
  
      operations = [
          migrations.RunPython(
              backup_old_tags,
              reverse_code=migrations.RunPython.noop,
          ),
          migrations.RemoveField(
              model_name='attachmentfile',
              name='tags',
          ),
          migrations.AddField(
              model_name='attachmentfile',
              name='tags',
              field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
          ),
          migrations.RemoveField(
              model_name='area',
              name='tags',
          ),
          migrations.AddField(
              model_name='area',
              name='tags',
              field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
          ),
          migrations.RemoveField(
              model_name='officer',
              name='tags',
          ),
          migrations.AddField(
              model_name='officer',
              name='tags',
              field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
          ),
          migrations.RemoveField(
              model_name='policeunit',
              name='tags',
          ),
          migrations.AddField(
              model_name='policeunit',
              name='tags',
              field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
          ),
          migrations.RunPython(
              restore_old_tags,
              reverse_code=migrations.RunPython.noop,
          ),
      ]
  