def search_index_view(request): context = {"indexes": SortedDict()} """ todo DONE reindex selected indexes in background . show when indexer is working . lock form (if working) . show last time indexer started (if working) . ajaxify . vue.js? """ from digipal.views.faceted_search import faceted_search from digipal.utils import get_all_files_under from datetime import datetime from digipal.views.faceted_search.search_indexer import SearchIndexer indexer = SearchIndexer() content_types = faceted_search.get_types(True) # process request action = request.POST.get("action", "") reindexes = [] if action == "reindex": for ct in content_types: if request.POST.get("select-%s" % ct.key): reindexes.append(ct.key) if reindexes: dputils.call_management_command("dpsearch", "index_facets", **{"if": ",".join(reindexes)}) context["indexing"] = indexer.get_state_initial(reindexes) if not "indexing" in context: context["indexing"] = indexer.read_state() context["running"] = context["indexing"] and context["indexing"]["progress"] < 1.0 now = datetime.now() if context["indexing"] and ( not context["running"] and ((now - context["indexing"]["updated"]).total_seconds() > (60 * 10)) ): context["indexing"] = None # read the index stats for ct in content_types: info = {"date": 0, "size": 0} context["indexes"][ct.key] = { "object": ct, "info": info, "indexing": context["indexing"]["indexes"].get(ct.key, None) if context["indexing"] else None, } for afile in get_all_files_under(ct.get_whoosh_index_path(), file_types="f"): info["size"] += os.path.getsize(afile) info["date"] = max(info["date"], os.path.getmtime(afile)) info["date"] = datetime.fromtimestamp(info["date"]) info["size"] = int(info["size"]) context["title"] = "Search Indexer" template = "search/search_index.html" if request.is_ajax(): template = "search/search_index_fragment.html" ret = render_to_response(template, context, context_instance=RequestContext(request)) return ret