diff options
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 02c19e011..3d8057c9f 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -45,6 +45,7 @@ from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.files.uploadedfile import SimpleUploadedFile from django.core.urlresolvers import reverse, NoReverseMatch from django.core.validators import validate_slug +from django.db import connection from django.db.models import Q, Max, Count from django.db.models.signals import post_save, post_delete, m2m_changed from django.db.utils import DatabaseError @@ -1004,6 +1005,8 @@ class JsonDataField(models.Model): value_type = models.CharField(_(u"Type"), default="T", max_length=10, choices=JSON_VALUE_TYPES) order = models.IntegerField(_(u"Order"), default=10) + search_index = models.BooleanField(_(u"Use in search indexes"), + default=False) section = models.ForeignKey(JsonDataSection, blank=True, null=True) custom_forms = models.ManyToManyField( "CustomForm", blank=True, through="CustomFormJsonField") @@ -1158,6 +1161,27 @@ class FullSearch(models.Model): q.all()[0]['search'].decode('utf-8') ) ) + + if hasattr(self, 'data') and self.data: + content_type = ContentType.objects.get_for_model(self) + datas = [] + for json_field in JsonDataField.objects.filter( + content_type=content_type, + search_index=True).all(): + data = copy.deepcopy(self.data) + no_data = False + for key in json_field.key.split('__'): + if key not in data: + no_data = True + break + data = data[key] + if no_data: + continue + with connection.cursor() as cursor: + cursor.execute("SELECT to_tsvector(%s)", + [data]) + row = cursor.fetchone() + search_vectors.append(row[0]) self.search_vector = merge_tsvectors(search_vectors) changed = old_search != self.search_vector if save and changed: |