divio / aldryn-search

Haystack 2.0 search index for django CMS
Other
48 stars 78 forks source link

add changed date to index #56

Closed SachaMPS closed 8 years ago

SachaMPS commented 8 years ago

I hope this is not a stupid pull request :)

Bit as it seems the published date remains the same after the very first publish. Published changes are reflected in the changed_date. In our case this value is far more interesting and i thought we might not be the only ones.

Thanks for your great work.

czpython commented 8 years ago

Thanks! I can assure you it's not a stupid pull request :)

My only concern is if it's really necessary mostly because this would require all projects that use this with solr and upgrade to the new version to update their schema.xml file which makes the upgrade not so straightforward. So I'm not sure if it's worth the hassle for all users when upgrading.

Usually additions like this I do at the project level with the base index functionality.

SachaMPS commented 8 years ago

@czpython Could you give me a hint in the correct direction how to do that at project level... I am still a learner in the django world and would highly appreciate it :)

My second guess is to override the Defatuls in conf.py and use something like: ALDRYN_SEARCH_INDEX_BASE_CLASS = "..." in my settings?!

czpython commented 8 years ago

@SachaMPS your guess is correct :)

I usually create an app called search on my project. Then inside this app I create a base index in a file called base.py. Inside I define my base index like so:

from aldryn_search.base import AldrynIndexBase

class BaseIndex(AldrynIndexBase):
    # following your example
    changed_date = indexes.DateTimeField(null=True)

then I can tell aldryn search to use my class as the new index base class like so (on the settings.py):

ALDRYN_SEARCH_INDEX_BASE_CLASS = 'search.base.BaseIndex'

Now that all your indexes have this field, you can add the prepare_changed_date method.

For indexes that come from third party apps like newsblog or cms page (coming from this app), you can easily extend them to have the prepare_changed_date method.

Here's an example of how you can extend the cms page index from this app in your project. On your search app from above, create a file called search_indexes.py. Inside the file add the following:

from aldryn_search.search_indexes import TitleIndex as BaseTitleIndex

class TitleIndex(BaseTitleIndex):
    def prepare_changed_date(self, obj):
         return obj.page.changed_date

Last, in yout settings.py add the following:

ALDRYN_SEARCH_CMS_PAGE = False # disabled to add custom field values

The above setting tells aldryn search to not register it's index for the cms, thus allowing you to register your own which inherits from the one in aldryn search.

SachaMPS commented 8 years ago

@czpython Thank you very much. I already stumbled in my head around the issue that aldryn search registers its index. That it simply can be deactivated as shown didnt pop into my eyes... Got it!!! Thanks!!!

czpython commented 8 years ago

@SachaMPS my pleasure. I'll close the pr for the reasons above. Thank you very much for your contribution.