Example #1
0
    def _migrate_fields(self, **options):
        force = options.get('force')
        no_input = options.get('no_input')

        total_migrated = 0

        for lf in legacy.Field.objects.iterator():
            try:
                f = DataField.objects.get_by_natural_key(
                    lf.app_name, lf.model_name, lf.field_name)
            except DataField.DoesNotExist:
                f = DataField(app_name=lf.app_name,
                              model_name=lf.model_name,
                              field_name=lf.field_name)

            qualified_name = \
                u'({0}) {1}.{2}'.format(f.app_name, f.model_name, f.field_name)

            if f.pk and not force:
                print u'{0} already exists. Skipping...'.format(qualified_name)
                continue

            # Check if this is an orphan
            if not f.field:
                print u'{0} is orphaned. Skipping...'.format(qualified_name)
                continue

            # Map various fields
            f.name = lf.name
            f.description = lf.description
            f.keywords = lf.keywords
            f.translator = lf.translator
            f.group_id = lf.group_id

            print u'Migrated "{0}"'.format(qualified_name)

            f.__dict__.update(utils.get_heuristic_flags(f.field))

            # Disagreement with enumerable status
            if not no_input and f.enumerable != lf.enable_choices:
                if lf.enable_choices:
                    override = raw_input(
                        u'"{0}" is marked as enumerable, but '
                        'does not qualify to be enumerable. '
                        'Override? [y/N] '.format(qualified_name))
                else:
                    override = raw_input(
                        u'"{0}" is not marked as enumerable, '
                        'but qualifies to be enumerable. '
                        'Override? [y/N] '.format(qualified_name))

                if override.lower() == 'y':
                    f.enumerable = lf.enable_choices

            f.save()
            f.sites = lf.sites.all()

            total_migrated += 1

        print u'Fields migrated:\t{0}'.format(total_migrated)
Example #2
0
    def _migrate_fields(self, **options):
        force = options.get('force')
        no_input = options.get('no_input')

        total_migrated = 0

        for lf in legacy.Field.objects.iterator():
            try:
                f = DataField.objects.get_by_natural_key(lf.app_name,
                                                         lf.model_name,
                                                         lf.field_name)
            except DataField.DoesNotExist:
                f = DataField(app_name=lf.app_name, model_name=lf.model_name,
                              field_name=lf.field_name)

            qualified_name = \
                u'({0}) {1}.{2}'.format(f.app_name, f.model_name, f.field_name)

            if f.pk and not force:
                print u'{0} already exists. Skipping...'.format(qualified_name)
                continue

            # Check if this is an orphan
            if not f.field:
                print u'{0} is orphaned. Skipping...'.format(qualified_name)
                continue

            # Map various fields
            f.name = lf.name
            f.description = lf.description
            f.keywords = lf.keywords
            f.translator = lf.translator
            f.group_id = lf.group_id

            print u'Migrated "{0}"'.format(qualified_name)

            f.__dict__.update(utils.get_heuristic_flags(f.field))

            # Disagreement with enumerable status
            if not no_input and f.enumerable != lf.enable_choices:
                if lf.enable_choices:
                    override = raw_input(u'"{0}" is marked as enumerable, but '
                                         'does not qualify to be enumerable. '
                                         'Override? [y/N] '
                                         .format(qualified_name))
                else:
                    override = raw_input(u'"{0}" is not marked as enumerable, '
                                         'but qualifies to be enumerable. '
                                         'Override? [y/N] '
                                         .format(qualified_name))

                if override.lower() == 'y':
                    f.enumerable = lf.enable_choices

            f.save()
            f.sites = lf.sites.all()

            total_migrated += 1

        print u'Fields migrated:\t{0}'.format(total_migrated)
Example #3
0
    def handle_field(self, field, model_name, app_name, **options):
        include_keys = options.get('include_keys')
        force = options.get('force')
        include_non_editable = options.get('include_non_editable')
        prepend_model_name = options.get('prepend_model_name')
        create_concepts = options.get('concepts')
        auto_publish = options.get('publish')
        create_categories = options.get('categories')

        # M2Ms do not make any sense here..
        if isinstance(field, ManyToManyField):
            return

        if dep_supported('objectset'):
            from objectset.models import ObjectSet
            objectset = issubclass(field.model, ObjectSet)
        else:
            objectset = False

        lexicon = issubclass(field.model, Lexicon)

        # Lexicons and ObjectSets are represented via their primary key, so
        # these may pass
        if not objectset and not lexicon:
            # Check for primary key, and foreign key fields
            if isinstance(field, self.key_field_types) and not include_keys:
                print(u'({0}) {1}.{2} is a primary or foreign key. Skipping...'
                      .format(app_name, model_name, field.name))
                return

            # Ignore non-editable fields since in most cases they are for
            # managment purposes
            if not field.editable and not include_non_editable:
                print(u'({0}) {1}.{2} is not editable. Skipping...'
                      .format(app_name, model_name, field.name))
                return

        # All but the field name is case-insensitive, do initial lookup
        # to see if it already exists, skip if it does
        lookup = {
            'app_name__iexact': app_name,
            'model_name__iexact': model_name,
            'field_name': field.name,
        }

        # Note, `name` is set below
        kwargs = {
            'description': field.help_text or None,
            'app_name': app_name,
            'model_name': model_name.lower(),
            'field_name': field.name,
        }

        if lexicon:
            kwargs.update({
                'label_field_name': 'label',
                'order_field_name': 'order',
                'code_field_name': 'code',
            })
        elif objectset and hasattr(field.model, 'label_field'):
            kwargs.update({
                'label_field_name': field.model.label_field
            })

        try:
            f = DataField.objects.get(**lookup)
        except DataField.DoesNotExist:
            f = DataField(published=options.get('publish'), **kwargs)

        if f.pk:
            created = False
            if not force:
                print(u'({0}) {1}.{2} already exists. Skipping...'
                      .format(app_name, model_name, field.name))
                return
            # Only overwrite if the source value is not falsy
            f.__dict__.update([(k, v) for k, v in kwargs.items()])
        else:
            created = True

        if not f.name:
            # Use the default unicode representation of the datafield
            if prepend_model_name:
                f.name = unicode(f)
            else:
                f.name = field.verbose_name.title()

        # Update fields with flags
        f.__dict__.update(utils.get_heuristic_flags(field))

        # Create category based on the model name and associate
        # it to the field.
        if create_categories:
            category, _ = DataCategory.objects\
                .get_or_create(name=f.model._meta.verbose_name.title(),
                               published=auto_publish)
            f.category = category
        else:
            category = None

        f.save()

        # Create a concept if one does not already exist for this field
        if create_concepts and not DataConcept.objects\
                .filter(fields=f).exists():

            kwargs = {
                'published': auto_publish,
                'category': category,
            }

            DataConcept.objects.create_from_field(f, **kwargs)

        return created
Example #4
0
    def handle_field(self, field, model_name, app_name, **options):
        include_keys = options.get('include_keys')
        force = options.get('force')
        include_non_editable = options.get('include_non_editable')
        prepend_model_name = options.get('prepend_model_name')
        create_concepts = options.get('concepts')
        auto_publish = options.get('publish')
        create_categories = options.get('categories')

        # M2Ms do not make any sense here..
        if isinstance(field, ManyToManyField):
            return

        # Check for primary key, and foreign key fields
        if isinstance(field, self.key_field_types) and not include_keys:
            print(u'({0}) {1}.{2} is a primary or foreign key. Skipping...'.
                  format(app_name, model_name, field.name))
            return

        # Ignore non-editable fields since in most cases they are for
        # managment purposes
        if not field.editable and not include_non_editable:
            print(u'({0}) {1}.{2} is not editable. Skipping...'.format(
                app_name, model_name, field.name))
            return

        # All but the field name is case-insensitive, do initial lookup
        # to see if it already exists, skip if it does
        lookup = {
            'app_name__iexact': app_name,
            'model_name__iexact': model_name,
            'field_name': field.name,
        }

        # Note, `name` is set below
        kwargs = {
            'description': field.help_text or None,
            'app_name': app_name,
            'model_name': model_name.lower(),
            'field_name': field.name,
        }

        try:
            f = DataField.objects.get(**lookup)
        except DataField.DoesNotExist:
            f = DataField(published=options.get('publish'), **kwargs)

        if f.pk:
            created = False
            if not force:
                print(u'({0}) {1}.{2} already exists. Skipping...'.format(
                    app_name, model_name, field.name))
                return
            # Only overwrite if the source value is not falsy
            f.__dict__.update([(k, v) for k, v in kwargs.items()])
        else:
            created = True

        if not f.name:
            # Use the default unicode representation of the datafield
            if prepend_model_name:
                f.name = unicode(f)
            else:
                f.name = field.verbose_name.title()

        # Update fields with flags
        f.__dict__.update(utils.get_heuristic_flags(field))

        # Create category based on the model name and associate
        # it to the field.
        if create_categories:
            category, _ = DataCategory.objects\
                .get_or_create(name=f.model._meta.verbose_name.title(),
                               published=auto_publish)
            f.category = category
        else:
            category = None

        f.save()

        # Create a concept if one does not already exist for this field
        if create_concepts and not DataConcept.objects\
                .filter(fields=f).exists():

            kwargs = {
                'published': auto_publish,
                'category': category,
            }

            DataConcept.objects.create_from_field(f, **kwargs)

        return created
Example #5
0
    def handle_field(self, field, model_name, app_name, **options):
        include_keys = options.get('include_keys')
        force = options.get('force')
        include_non_editable = options.get('include_non_editable')

        # M2Ms do not make any sense here..
        if isinstance(field, ManyToManyField):
            return

        # Lexicons and ObjectSets are represented via their primary key, so
        # these may pass
        if not issubclass(field.model, (Lexicon, ObjectSet)):
            # Check for primary key, and foreign key fields
            if isinstance(field, self.key_field_types) and not include_keys:
                return

            # Ignore non-editable fields since in most cases they are for
            # managment purposes
            if not field.editable and not include_non_editable:
                return

        # All but the field name is case-insensitive, do initial lookup
        # to see if it already exists, skip if it does
        lookup = {
            'app_name__iexact': app_name,
            'model_name__iexact': model_name,
            'field_name': field.name,
        }

        # Note, `name` is set below
        kwargs = {
            'description': field.help_text or None,
            'app_name': app_name,
            'model_name': model_name.lower(),
            'field_name': field.name,
        }

        try:
            datafield = DataField.objects.get(**lookup)
        except DataField.DoesNotExist:
            datafield = DataField(published=False, **kwargs)

        if datafield.pk:
            created = False
            if not force:
                print '({0}) {1}.{2} already exists. Skipping...'.format(app_name,
                    model_name, field.name)
                return
            # Only overwrite if the source value is not falsy
            datafield.__dict__.update([(k, v) for k, v in kwargs.items()])
        else:
            created = True

        if not datafield.name:
            # Use the default unicode representation of the datafield
            datafield.name = unicode(datafield)

        # Update fields with flags
        datafield.__dict__.update(utils.get_heuristic_flags(datafield))
        datafield.save()
        return created