<commit_before>from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from infrastructure.models import Project

budget_phase = fields.ObjectField(properties={
   "code": fields.TextField(),
   "name": fields.TextField()
})

financial_year = fields.ObjectField(properties={
   "budget_year": fields.TextField(),
})


@registry.register_document
class ProjectDocument(Document):

    #geography = fields.ObjectField(properties={
    #    "name": fields.TextField(),
    #    "province_name": fields.TextField(),
    #})

    #expenditure = fields.NestedField(properties={
    #    "budget_phase": budget_phase,
    #    "financial_year": financial_year,
    #    "amount": fields.DoubleField(),
    #})


    project_type = fields.KeywordField()
    function = fields.KeywordField()
    province = fields.KeywordField()
    municipality = fields.KeywordField()

    # TODO year currently hardcoded - need to figure out the best way to handle this
    total_forecast_budget = fields.DoubleField()
    #id = fields.IntegerField(attr='id')
    def prepare_total_forecast_budget(self, instance):

        qs = instance.expenditure.filter(financial_year__budget_year="2019/2020")
        if qs.count() > 0:
            return qs.first().amount
        return 0

    def prepare_province(self, instance):
        return instance.geography.province_name

    def prepare_municipality(self, instance):
        return instance.geography.name

    def prepare_project_type(self, instance):
        return instance.geography.name

    def get_queryset(self):
        # TODO remove slice - used for dev only
        return (super(ProjectDocument, self)
            .get_queryset()
            .select_related("geography")
            .prefetch_related("expenditure__budget_year", "expenditure__financial_Year")
        )

    class Index:
        # Name of the Elasticsearch index
        name = "projects"
        # See Elasticsearch Indices API reference for available settings
        settings = {"number_of_shards": 1,
                    "number_of_replicas": 0}

    class Django:
        model = Project 

        fields = [
            #"id",
            "project_description",
            "project_number",
            "mtsf_service_outcome",
            "iudf",
            "own_strategic_objectives",
            "asset_class",
            "asset_subclass",
            "ward_location",
            "longitude",
            "latitude",
        ]<commit_msg>Fixed project_type index<commit_after>from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from infrastructure.models import Project

budget_phase = fields.ObjectField(properties={
   "code": fields.TextField(),
   "name": fields.TextField()
})

financial_year = fields.ObjectField(properties={
   "budget_year": fields.TextField(),
})


@registry.register_document
class ProjectDocument(Document):

    #geography = fields.ObjectField(properties={
    #    "name": fields.TextField(),
    #    "province_name": fields.TextField(),
    #})

    #expenditure = fields.NestedField(properties={
    #    "budget_phase": budget_phase,
    #    "financial_year": financial_year,
    #    "amount": fields.DoubleField(),
    #})


    project_description = fields.KeywordField()
    project_type = fields.KeywordField()
    function = fields.KeywordField()
    province = fields.KeywordField()
    municipality = fields.KeywordField()

    # TODO year currently hardcoded - need to figure out the best way to handle this
    total_forecast_budget = fields.DoubleField()
    #id = fields.IntegerField(attr='id')
    def prepare_total_forecast_budget(self, instance):

        qs = instance.expenditure.filter(financial_year__budget_year="2019/2020")
        if qs.count() > 0:
            return qs.first().amount
        return 0

    def prepare_province(self, instance):
        return instance.geography.province_name

    def prepare_municipality(self, instance):
        return instance.geography.name

    def prepare_project_type(self, instance):
        return instance.project_type

    def get_queryset(self):
        # TODO remove slice - used for dev only
        return (super(ProjectDocument, self)
            .get_queryset()
            .select_related("geography")
            .prefetch_related("expenditure__budget_year", "expenditure__financial_Year")
        )

    class Index:
        # Name of the Elasticsearch index
        name = "projects"
        # See Elasticsearch Indices API reference for available settings
        settings = {"number_of_shards": 1,
                    "number_of_replicas": 0}

    class Django:
        model = Project 

        fields = [
            #"id",
            #"project_description",
            "project_number",
            "mtsf_service_outcome",
            "iudf",
            "own_strategic_objectives",
            "asset_class",
            "asset_subclass",
            "ward_location",
            "longitude",
            "latitude",
        ]
{'fraction_non_alphanumeric': 0.0984251968503937, 'fraction_numerical': 0.004724409448818898, 'mean_word_length': 2.719619326500732, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 5, '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': '1524901', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1403, 'n_words': 298}