Last active
August 29, 2015 14:07
-
-
Save calebsmith/77f373bf7e3b4db84436 to your computer and use it in GitHub Desktop.
Reindex JP2 Homepage content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from django.db.models import get_model | |
from django.contrib.sites.models import Site | |
def reindex_homepage(site_id=2010, num=50, async=False): | |
layout_ids = find_homepage_layout_ids(site_id) | |
return reindex(layout_ids, num, async) | |
def find_homepage_layout_ids(site_id): | |
site = Site.objects.get(id=site_id) | |
return [hp.section.layout_id for hp in site.homepage_set.all()] | |
def reindex_layout(layout_id, async=False): | |
Layout = get_model('grids', 'Layout') | |
layout = Layout.objects.get(id=layout_id) | |
slot_objects = [slot.content_object for slot in layout.slots.all() if slot.content_object] | |
objects = filter(lambda o: o.model_name == 'fastautolist', slot_objects) | |
method = 'reindex_async' if async else 'reindex' | |
fl_results = [getattr(obj, method)(using='solr4_aws') for obj in objects] | |
pinned_results = [getattr(pi.content_object, method)(using='solr4_aws') for pi in obj.pinneditem_set.all() for obj in objects] | |
return fl_results, pinned_results | |
def reindex_stories(num=50, async=False): | |
Story = get_model('news', 'medleystory') | |
stories = Story.objects.filter(status=1, pub_date__lte=datetime.now()).order_by('-pub_date')[:num] | |
method = 'reindex_async' if async else 'reindex' | |
return [getattr(story, method)(using='solr4_aws') for story in stories] | |
def reindex(layout_ids, num=50, async=False): | |
return ( | |
[reindex_layout(layout_id, async) for layout_id in layout_ids], | |
reindex_stories(num, async)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment