from django.apps import AppConfig
from django.conf import settings
from common.helpers.db import db_is_initialized


class CommonConfig(AppConfig):
    name = 'common'

    def ready(self):
        self.display_missing_environment_variables()
        from common.helpers.tags import import_tags_from_csv
        if db_is_initialized():
            import_tags_from_csv()

    def display_missing_environment_variables(self):
        missing_required_variables = []
        for key, value in settings.ENVIRONMENT_VARIABLE_WARNINGS.items():
            if not (hasattr(settings, key)):
                if value['error']:
                    missing_required_variables.append(key)
                print(key + ' not set: ' + value['message'])
        if len(missing_required_variables) > 0:
            raise EnvironmentError('Required environment variables missing: ' + ','.join(missing_required_variables))

import sys
from django.apps import AppConfig
from django.conf import settings
from common.helpers.db import db_is_initialized


class CommonConfig(AppConfig):
    name = 'common'

    def ready(self):
        self.display_missing_environment_variables()
        from common.helpers.tags import import_tags_from_csv
        if 'loaddata' in sys.argv:
            self.loaddata_clean()
        elif db_is_initialized():
            import_tags_from_csv()

    def display_missing_environment_variables(self):
        missing_required_variables = []
        for key, value in settings.ENVIRONMENT_VARIABLE_WARNINGS.items():
            if not (hasattr(settings, key)):
                if value['error']:
                    missing_required_variables.append(key)
                print(key + ' not set: ' + value['message'])
        if len(missing_required_variables) > 0:
            raise EnvironmentError('Required environment variables missing: ' + ','.join(missing_required_variables))

    def loaddata_clean(self):
        from django.contrib.contenttypes.models import ContentType
        ContentType.objects.all().delete()

Clean content types table and don't load tags when running loaddata
Clean content types table and don't load tags when running loaddata

+ import sys
  from django.apps import AppConfig
  from django.conf import settings
  from common.helpers.db import db_is_initialized
  
  
  class CommonConfig(AppConfig):
      name = 'common'
  
      def ready(self):
          self.display_missing_environment_variables()
          from common.helpers.tags import import_tags_from_csv
+         if 'loaddata' in sys.argv:
+             self.loaddata_clean()
-         if db_is_initialized():
+         elif db_is_initialized():
              import_tags_from_csv()
  
      def display_missing_environment_variables(self):
          missing_required_variables = []
          for key, value in settings.ENVIRONMENT_VARIABLE_WARNINGS.items():
              if not (hasattr(settings, key)):
                  if value['error']:
                      missing_required_variables.append(key)
                  print(key + ' not set: ' + value['message'])
          if len(missing_required_variables) > 0:
              raise EnvironmentError('Required environment variables missing: ' + ','.join(missing_required_variables))
  
+     def loaddata_clean(self):
+         from django.contrib.contenttypes.models import ContentType
+         ContentType.objects.all().delete()
+ 
Clean content types table and don't load tags when running loaddata
## Code Before:
from django.apps import AppConfig
from django.conf import settings
from common.helpers.db import db_is_initialized


class CommonConfig(AppConfig):
    name = 'common'

    def ready(self):
        self.display_missing_environment_variables()
        from common.helpers.tags import import_tags_from_csv
        if db_is_initialized():
            import_tags_from_csv()

    def display_missing_environment_variables(self):
        missing_required_variables = []
        for key, value in settings.ENVIRONMENT_VARIABLE_WARNINGS.items():
            if not (hasattr(settings, key)):
                if value['error']:
                    missing_required_variables.append(key)
                print(key + ' not set: ' + value['message'])
        if len(missing_required_variables) > 0:
            raise EnvironmentError('Required environment variables missing: ' + ','.join(missing_required_variables))

## Instruction:
Clean content types table and don't load tags when running loaddata
## Code After:
import sys
from django.apps import AppConfig
from django.conf import settings
from common.helpers.db import db_is_initialized


class CommonConfig(AppConfig):
    name = 'common'

    def ready(self):
        self.display_missing_environment_variables()
        from common.helpers.tags import import_tags_from_csv
        if 'loaddata' in sys.argv:
            self.loaddata_clean()
        elif db_is_initialized():
            import_tags_from_csv()

    def display_missing_environment_variables(self):
        missing_required_variables = []
        for key, value in settings.ENVIRONMENT_VARIABLE_WARNINGS.items():
            if not (hasattr(settings, key)):
                if value['error']:
                    missing_required_variables.append(key)
                print(key + ' not set: ' + value['message'])
        if len(missing_required_variables) > 0:
            raise EnvironmentError('Required environment variables missing: ' + ','.join(missing_required_variables))

    def loaddata_clean(self):
        from django.contrib.contenttypes.models import ContentType
        ContentType.objects.all().delete()
