Ejemplo n.º 1
0
    def search(self, content, queryset=None, max_results=None, partial=False,
               using=None):
        if dep_supported('haystack'):
            return self._haystack_search(content=content,
                                         queryset=queryset,
                                         max_results=max_results,
                                         partial=partial,
                                         using=using)

        return self._basic_search(content, queryset)
Ejemplo n.º 2
0
    def search(self,
               content,
               queryset=None,
               max_results=None,
               partial=False,
               using=None):
        if dep_supported('haystack'):
            return self._haystack_search(content=content,
                                         queryset=queryset,
                                         max_results=max_results,
                                         partial=partial,
                                         using=using)

        return self._basic_search(content, queryset)
Ejemplo n.º 3
0
    def field(self):
        "Returns the field object this datafield represents."
        model = self.model

        if model:
            if issubclass(model, Lexicon):
                return model._meta.pk

            if dep_supported('objectset'):
                from objectset.models import ObjectSet

                if issubclass(model, ObjectSet):
                    return model._meta.pk

        return self.real_field
Ejemplo n.º 4
0
    def field(self):
        "Returns the field object this datafield represents."
        model = self.model

        if model:
            if issubclass(model, Lexicon):
                return model._meta.pk

            if dep_supported('objectset'):
                from objectset.models import ObjectSet

                if issubclass(model, ObjectSet):
                    return model._meta.pk

        return self.real_field
Ejemplo n.º 5
0
def is_objectset(f):
    """Returns true if the model is a subclass of ObjectSet and this
    is the pk field. All other fields on the class are treated as
    normal datafields.
    """
    warn('ObjectSet detection in the DataField API is deprecated and '
         'will be removed in 2.4. Set the alternate fields explicitly '
         'on the field instance', DeprecationWarning)

    if dep_supported('objectset'):
        from objectset.models import ObjectSet

        return f.model and issubclass(f.model, ObjectSet) \
            and f.field == f.model._meta.pk

    return False
Ejemplo n.º 6
0
def is_objectset(f):
    """Returns true if the model is a subclass of ObjectSet and this
    is the pk field. All other fields on the class are treated as
    normal datafields.
    """
    warn(
        'ObjectSet detection in the DataField API is deprecated and '
        'will be removed in 2.4. Set the alternate fields explicitly '
        'on the field instance', DeprecationWarning)

    if dep_supported('objectset'):
        from objectset.models import ObjectSet

        return f.model and issubclass(f.model, ObjectSet) \
            and f.field == f.model._meta.pk

    return False
Ejemplo n.º 7
0
    def handle(self, *args, **options):
        "Handles app_label or app_label.model_label formats."

        if not args:
            # TODO: this should technically retrieve the subcommand name from
            # the avocado management command class
            self.print_help(sys.argv[0], 'init')
            return

        if options.get('quiet'):
            self.stdout = sys.stdout
            sys.stdout = open(os.devnull, 'w')

        if options.get('force'):
            resp = raw_input('Forcing a init will update metadata for '
                             'existing fields. Are you sure you want to do '
                             'this?\n This will overwrite any previous '
                             'changes made. Type "yes" to continue: ')
            if resp.lower() != 'yes':
                print('Initialization operation cancelled')
                return

        for label in args:
            pending_fields = []
            pending_models = []

            toks = label.split('.')
            app_name = model_name = field_name = None

            # Specific field
            if len(toks) == 3:
                app_name, model_name, field_name = toks
            # All fields for a model
            elif len(toks) == 2:
                app_name, model_name = toks
            # All fields for each model in the app
            else:
                app_name = toks[0]

            if model_name:
                model = get_model(app_name, model_name)

                if model is None:
                    print(u'Cannot find model "{0}", skipping...'
                          .format(label))
                    continue

                # Specific field
                if field_name:
                    try:
                        field = model._meta.get_field_by_name(field_name)[0]
                    except FieldDoesNotExist:
                        print(u'Cannot find field "{0}", skipping...'
                              .format(label))
                        continue
                    pending_fields = [(field, model_name, app_name)]

                # No specific field, queue up the model
                else:
                    pending_models.append(model)
            else:
                app = get_app(app_name)
                if app is None:
                    print(u'Cannot find app "{0}", skipping...'.format(label))
                    continue
                pending_models.extend(get_models(app))

            for model in pending_models:
                lexicon = issubclass(model, Lexicon)

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

                model_name = model._meta.object_name.lower()

                if lexicon or objectset:
                    pk = model._meta.pk
                    pk.verbose_name = model._meta.verbose_name
                    pending_fields.append((pk, model_name, app_name))
                else:
                    for field in model._meta.fields:
                        pending_fields.append((field, model_name, app_name))

            added = 0
            updated = 0

            for field_args in pending_fields:
                status = self.handle_field(*field_args, **options)
                if status is True:
                    added += 1
                elif status is False:
                    updated += 1

            if added == 1:
                print(u'1 field added for {0}'.format(label))
            elif added > 1:
                print(u'{0} fields added for {1}'.format(added, label))

            if updated == 1:
                print(u'1 field updated for {0}'.format(label))
            elif updated > 1:
                print(u'{0} fields updated for {1}'.format(updated, label))

        if options.get('quiet'):
            sys.stdout = self.stdout
Ejemplo n.º 8
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
Ejemplo n.º 9
0
import warnings
from avocado.conf import dep_supported, raise_dep_error

if not dep_supported('objectset'):
    raise_dep_error('objectset')

from objectset.models import ObjectSet, SetObject  # noqa

warnings.warn('The built-in ObjectSet and SetObject classes have been '
              'removed in favor of the classes defined in the '
              'django-objectset library. Imports should be changed to '
              'import directly from objectset.models instead of from '
              'avocado.sets.models')
Ejemplo n.º 10
0
    def handle(self, *args, **options):
        "Handles app_label or app_label.model_label formats."

        if not args:
            # TODO: this should technically retrieve the subcommand name from
            # the avocado management command class
            self.print_help(sys.argv[0], 'init')
            return

        if options.get('quiet'):
            self.stdout = sys.stdout
            sys.stdout = open(os.devnull, 'w')

        if options.get('force'):
            resp = raw_input('Forcing a init will update metadata for '
                             'existing fields. Are you sure you want to do '
                             'this?\n This will overwrite any previous '
                             'changes made. Type "yes" to continue: ')
            if resp.lower() != 'yes':
                print('Initialization operation cancelled')
                return

        for label in args:
            pending_fields = []
            pending_models = []

            toks = label.split('.')
            app_name = model_name = field_name = None

            # Specific field
            if len(toks) == 3:
                app_name, model_name, field_name = toks
            # All fields for a model
            elif len(toks) == 2:
                app_name, model_name = toks
            # All fields for each model in the app
            else:
                app_name = toks[0]

            if model_name:
                model = get_model(app_name, model_name)

                if model is None:
                    print(
                        u'Cannot find model "{0}", skipping...'.format(label))
                    continue

                # Specific field
                if field_name:
                    try:
                        field = model._meta.get_field_by_name(field_name)[0]
                    except FieldDoesNotExist:
                        print(u'Cannot find field "{0}", skipping...'.format(
                            label))
                        continue
                    pending_fields = [(field, model_name, app_name)]

                # No specific field, queue up the model
                else:
                    pending_models.append(model)
            else:
                app = get_app(app_name)
                if app is None:
                    print(u'Cannot find app "{0}", skipping...'.format(label))
                    continue
                pending_models.extend(get_models(app))

            for model in pending_models:
                lexicon = issubclass(model, Lexicon)

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

                model_name = model._meta.object_name.lower()

                if lexicon or objectset:
                    pk = model._meta.pk
                    pk.verbose_name = model._meta.verbose_name
                    pending_fields.append((pk, model_name, app_name))
                else:
                    for field in model._meta.fields:
                        pending_fields.append((field, model_name, app_name))

            added = 0
            updated = 0

            for field_args in pending_fields:
                status = self.handle_field(*field_args, **options)
                if status is True:
                    added += 1
                elif status is False:
                    updated += 1

            if added == 1:
                print(u'1 field added for {0}'.format(label))
            elif added > 1:
                print(u'{0} fields added for {1}'.format(added, label))

            if updated == 1:
                print(u'1 field updated for {0}'.format(label))
            elif updated > 1:
                print(u'{0} fields updated for {1}'.format(updated, label))

        if options.get('quiet'):
            sys.stdout = self.stdout
Ejemplo n.º 11
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