new_contents: # Generated by Django 2.2.1 on 2019-05-21 20:33
  
  import logging
  
  from django.db import migrations
  
  logger = logging.getLogger(__name__)
  
  # NOTE: this migration only needs to run once.
  
  
  def _getattr(submission, key, model_lookup):
      if key in model_lookup:
          model = model_lookup[key]
          return model.objects.filter(activity_submission=submission)
      return getattr(submission, key)
  
  
  def create_fields_provided_instances(apps, schema_editor):
      """ creates new instances of the FieldsProvided model for each submission.
      This helps track which form fields were filled in (or edited/removed) when users
      submit activity reports or staff edits. 
      Each submission should have one related instance of FieldsProvided.
      """
  
      ActivitySubmission = apps.get_model('wells', 'ActivitySubmission')
      FieldsProvided = apps.get_model('wells', 'FieldsProvided')
  
      foreign_key_models = {
          'casing_set': apps.get_model('wells', 'Casing'),
          'screen_set': apps.get_model('wells', 'Screen'),
          'linerperforation_set': apps.get_model('wells', 'ActivitySubmissionLinerPerforation'),
          'decommission_description_set': apps.get_model('wells', 'DecommissionDescription'),
          'lithologydescription_set': apps.get_model('wells', 'LithologyDescription')
      }
  
      instances_created = 0
  
      # find reports that do not have a "fields_provided" object. This means we were
      # not recording the fields that were explicitly provided by the user
      # at the time the report was submitted.
      for report in ActivitySubmission.objects.filter(fields_provided__isnull=True, well_activity_type='STAFF_EDIT'):
  
          data_submitted = {}
  
          # we gather values from each report using the same logic in place in stack.py on 2019/05/21,
          # and create a "fields provided" mapping.  By moving toward using the mapping to determine
          # which fields were explicitly filled in or altered by the user, we can be determine what
          # fields to update rather than guessing based on the value.  After this migration is run,
          # it should no longer be necessary to inspect the value to decide whether to update a piece of
          # well data or not.
          for field in FieldsProvided._meta.get_fields():
  
              if field.name == 'activity_submission':
                  continue
  
              source_key = field.name
              value = _getattr(report, source_key, foreign_key_models)
  
              if value or value is False or value == 0 or value == '':
                  data_submitted[source_key] = True
  
          data_submitted.pop('filing_number', None)
  
          FieldsProvided.objects.create(activity_submission=report, **data_submitted)
          instances_created += 1
  
      logger.info("created {} fields_provided mappings for submission reports".format(instances_created))
  
  
  def create_fields_provided_instances_down(apps, schema_editor):
      pass
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('wells', '0089_merge_20190517_2255'),
      ]
  
      operations = [
          migrations.RunPython(create_fields_provided_instances, create_fields_provided_instances_down),
      ]
  
subject: add a migration for previous staff edits