<commit_before>import datetime
import os
from haystack.indexes import *
from haystack import site
from projects.models import File, ImportedFile
from projects import constants

class FileIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    author = CharField(model_attr='project__user')
    project = CharField(model_attr='project__name')
    title = CharField(model_attr='heading')

    def get_queryset(self):
        return File.objects.filter(project__status=constants.LIVE_STATUS)

class ImportedFileIndex(SearchIndex):
    text = CharField(document=True)
    author = CharField(model_attr='project__user')
    project = CharField(model_attr='project__name')
    title = CharField(model_attr='name')

    def prepare_text(self, obj):
        full_path = obj.project.full_html_path
        to_read = os.path.join(full_path, obj.path.lstrip('/'))
        content = open(to_read, 'r').read()
        return content

site.register(File, FileIndex)
site.register(ImportedFile, ImportedFileIndex)<commit_msg>Fix unicode fail in search indexing.<commit_after>import datetime
import os
import codecs

from haystack.indexes import *
from haystack import site
from projects.models import File, ImportedFile
from projects import constants

class FileIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    author = CharField(model_attr='project__user')
    project = CharField(model_attr='project__name')
    title = CharField(model_attr='heading')

    def get_queryset(self):
        return File.objects.filter(project__status=constants.LIVE_STATUS)

class ImportedFileIndex(SearchIndex):
    text = CharField(document=True)
    author = CharField(model_attr='project__user')
    project = CharField(model_attr='project__name')
    title = CharField(model_attr='name')

    def prepare_text(self, obj):
        full_path = obj.project.full_html_path
        to_read = os.path.join(full_path, obj.path.lstrip('/'))
        content = codecs.open(to_read, encoding="utf-8", mode='r').read()
        return content

site.register(File, FileIndex)
site.register(ImportedFile, ImportedFileIndex)
{'fraction_non_alphanumeric': 0.1021555763823805, 'fraction_numerical': 0.00046860356138706655, 'mean_word_length': 4.947075208913649, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7190372', 'n_tokens_mistral': 631, 'n_tokens_neox': 625, 'n_words': 131}