pombreda / djapian

Automatically exported from code.google.com/p/djapian
Other
0 stars 0 forks source link

creating and registering indexers using a class decorator #93

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
# any chance of something like this being added to djapian?

import djapian

class IndexMaker(object):
    def __init__(self, include=['title', 'name']):
        self.include = include
        self.models = []
        self.space = djapian.space

    def _populate(self, cls, fields, tags):
        for word in self.include:
            if hasattr(cls, word):
                fields.append(word)
                tags.append((word, word))                
        return fields, tags

    def index(self, *fields, **tags):
        fs, ts = list(fields), tags.items()
        def _decorator(cls):
            fields, tags = self._populate(cls, fs, ts)
            Indexer = type(cls.__name__+'Indexer', 
                                     (djapian.Indexer,), 
                                     dict(fields=fields, tags=tags)
                            )
            self.space.add_index(cls, Indexer, attach_as='indexer')
            self.models.append(cls)
            return cls
        return _decorator

    def config(self):
        indexers = [getattr(m, 'indexer') for m in self.models]
        djapian.CompositeIndexer(*indexers)

def example():
    from django.db import models

    search = IndexMaker(include=['title', 'name', 'description'])

    @search.index('data', content='data')
    class Page(models.Model):
        title = models.CharField( max_length = 64 )
        data = models.TextField()

    @search.index()
    class Page(models.Model):
        title = models.CharField( max_length = 64 )
        description = models.TextField()

    search.config()

Original issue reported on code.google.com by shakeeb....@gmail.com on 4 Nov 2009 at 9:35

Attachments:

GoogleCodeExporter commented 9 years ago
Greate idea! But class decorators is supported by very few spread python 
versions. In
future I think such helper must be in Djapian.

Original comment by daevaorn on 4 Nov 2009 at 9:43

GoogleCodeExporter commented 9 years ago
Of course. In any case, I'm refining the above proof-of-concept and I'll post 
back
the results here once it's fleshed out further and use-tested. Thanks.

Original comment by shakeeb....@gmail.com on 5 Nov 2009 at 6:05

GoogleCodeExporter commented 9 years ago
Great!

Original comment by daevaorn on 7 Nov 2009 at 5:33

GoogleCodeExporter commented 9 years ago

Original comment by daevaorn on 13 Dec 2009 at 9:07

GoogleCodeExporter commented 9 years ago
# FYI, I've been using it extensively with a few minor changes
# I think this has more legs as python 2.6.x and above support class 
decorators::

import djapian

class IndexMaker(object):
    """ usage:

        search = IndexMaker(include=['title', 'name', 'description'])

        @search.index('data', content='data')
        class Page(Model):
            title = models.CharField( max_length = 64 )
            data = models.TextField()

        @search.index()
        class Page(Model):
            title = models.CharField( max_length = 64 )
            description = models.TextField()

        search.config()

    """
    def __init__(self, include=['title', 'name']):
        self.include = include
        self.models = []
        self.space = djapian.space
        self.engine = None
        self.MODEL_MAP = {}
        self.MODEL_CHOICES = []

    def _populate(self, cls, fields, tags):
        for word in self.include:
            if hasattr(cls, word):
                fields.append(word)
                tags.append((word, word))                
        return fields, tags

    def index(self, *fields, **tags):
        fs, ts = list(fields), tags.items()
        def _decorator(cls):
            fields, tags = self._populate(cls, fs, ts)
            Indexer = type(cls.__name__+'Indexer', 
                                     (djapian.Indexer,), 
                                     dict(fields=fields, tags=tags)
                            )
            self.space.add_index(cls, Indexer, attach_as='indexer')
            self.models.append(cls)
            return cls
        return _decorator

    def config(self):
        indexers = [getattr(m, 'indexer') for m in self.models]
        self.engine = djapian.CompositeIndexer(*indexers)
        self.MODEL_MAP = mmap = dict([(cls.__name__.lower(), cls) for cls in
self.models])
        self.MODEL_CHOICES = [('', 'all')] + zip(mmap.keys(), mmap.keys())
        return self

def example_models_py():
    from django.db import models

    search = IndexMaker(include=['title', 'name', 'description'])

    @search.index('data', content='data')
    class Page(models.Model):
        title = models.CharField( max_length = 64 )
        data = models.TextField()

    @search.index()
    class Page(models.Model):
        title = models.CharField( max_length = 64 )
        description = models.TextField()

    search.config()

Original comment by shakeeb....@gmail.com on 24 Mar 2010 at 9:14

Attachments: