diff options
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 59 |
1 files changed, 45 insertions, 14 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 12dbf9069..610d92551 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1030,7 +1030,13 @@ class FullSearch(models.Model): :param save: True if you want to save the object immediately :return: True if modified """ - if not self.BASE_SEARCH_VECTORS and not self.M2M_SEARCH_VECTORS: + if not self.pk: + logger.warning("Cannot update search vector before save or " + "after deletion.") + return + if not self.BASE_SEARCH_VECTORS and not self.M2M_SEARCH_VECTORS \ + and not self.INT_SEARCH_VECTORS \ + and not self.PARENT_SEARCH_VECTORS: logger.warning("No search_vectors defined for {}".format( self.__class__)) return @@ -1070,16 +1076,17 @@ class FullSearch(models.Model): if hasattr(parent, 'all'): # m2m for p in parent.all(): search_vectors.append(p.search_vector) - else: + elif parent: search_vectors.append(parent.search_vector) - # query "simple" fields - q = base_q.annotate( - search=SearchVector( - *self.BASE_SEARCH_VECTORS, - config=settings.ISHTAR_SEARCH_LANGUAGE - )).values('search') - search_vectors.append(q.all()[0]['search']) + if self.BASE_SEARCH_VECTORS: + # query "simple" fields + q = base_q.annotate( + search=SearchVector( + *self.BASE_SEARCH_VECTORS, + config=settings.ISHTAR_SEARCH_LANGUAGE + )).values('search') + search_vectors.append(q.all()[0]['search']) self.search_vector = merge_tsvectors(search_vectors) changed = old_search != self.search_vector if save and changed: @@ -1793,7 +1800,7 @@ class DashboardFormItem(object): return q.order_by('pk').distinct('pk') @classmethod - def get_total_number(cls, fltr={}): + def get_total_number(cls, fltr=None): q = cls.objects if fltr: q = q.filter(**fltr) @@ -1802,7 +1809,9 @@ class DashboardFormItem(object): class Dashboard(object): def __init__(self, model, slice='year', date_source=None, show_detail=None, - fltr={}): + fltr=None): + if not fltr: + fltr = {} # don't provide date_source if it is not relevant self.model = model self.total_number = model.get_total_number(fltr) @@ -2246,6 +2255,7 @@ class Organization(Address, Merge, OwnPerms, ValueGetter): 'name': 'name__icontains', 'organization_type': 'organization_type__pk__in', } + BASE_SEARCH_VECTORS = ['name', 'town'] # fields name = models.CharField(_(u"Name"), max_length=500) @@ -2303,6 +2313,8 @@ class PersonType(GeneralType): verbose_name = _(u"Person type") verbose_name_plural = _(u"Person types") ordering = ('label',) + + post_save.connect(post_save_cache, sender=PersonType) post_delete.connect(post_save_cache, sender=PersonType) @@ -2315,6 +2327,8 @@ class TitleType(GeneralType): verbose_name = _(u"Title type") verbose_name_plural = _(u"Title types") ordering = ('label',) + + post_save.connect(post_save_cache, sender=TitleType) post_delete.connect(post_save_cache, sender=TitleType) @@ -2332,6 +2346,8 @@ class Person(Address, Merge, OwnPerms, ValueGetter): 'attached_to__name', 'town') SHOW_URL = 'show-person' MODIFY_URL = 'person_modify' + BASE_SEARCH_VECTORS = ['name', 'surname', 'raw_name', 'town', + 'attached_to__name', 'email'] # search parameters REVERSED_BOOL_FIELDS = ['ishtaruser__isnull'] @@ -2339,6 +2355,7 @@ class Person(Address, Merge, OwnPerms, ValueGetter): 'name': ['name__icontains', 'raw_name__icontains'], 'surname': ['surname__icontains', 'raw_name__icontains'], 'attached_to': 'attached_to__pk', + 'attached_to__name': 'attached_to__name', 'person_types': 'person_types__pk__in', 'ishtaruser__isnull': 'ishtaruser__isnull' } @@ -2545,10 +2562,13 @@ class Person(Address, Merge, OwnPerms, ValueGetter): =user.ishtaruser) -class IshtarUser(models.Model): +class IshtarUser(FullSearch): TABLE_COLS = ('username', 'person__name', 'person__surname', 'person__email', 'person__person_types_list', 'person__attached_to') + BASE_SEARCH_VECTORS = [ + 'user_ptr__username', 'person__name', 'person__surname', + 'person__email', 'person__town', 'person__attached_to__name'] # search parameters EXTRA_REQUEST_KEYS = { @@ -2559,6 +2579,7 @@ class IshtarUser(models.Model): 'email': ['person__email'], 'attached_to': 'person__attached_to__pk', 'person_types': 'person__person_types__pk__in', + 'person__person_types_list': 'person__person_types__name' } # fields @@ -2667,10 +2688,11 @@ post_save.connect(post_save_cache, sender=AuthorType) post_delete.connect(post_save_cache, sender=AuthorType) -class Author(models.Model): +class Author(FullSearch): person = models.ForeignKey(Person, verbose_name=_(u"Person"), related_name='author') author_type = models.ForeignKey(AuthorType, verbose_name=_(u"Author type")) + PARENT_SEARCH_VECTORS = ['person'] class Meta: verbose_name = _(u"Author") @@ -2704,6 +2726,8 @@ class SourceType(GeneralType): verbose_name = _(u"Source type") verbose_name_plural = _(u"Source types") ordering = ['label'] + + post_save.connect(post_save_cache, sender=SourceType) post_delete.connect(post_save_cache, sender=SourceType) @@ -2712,6 +2736,8 @@ class SupportType(GeneralType): class Meta: verbose_name = _(u"Support type") verbose_name_plural = _(u"Support types") + + post_save.connect(post_save_cache, sender=SupportType) post_delete.connect(post_save_cache, sender=SupportType) @@ -2727,7 +2753,7 @@ post_save.connect(post_save_cache, sender=Format) post_delete.connect(post_save_cache, sender=Format) -class Source(OwnPerms, ImageModel, models.Model): +class Source(OwnPerms, ImageModel, FullSearch): title = models.CharField(_(u"Title"), max_length=300) external_id = models.TextField(_(u"External ID"), max_length=300, null=True, blank=True) @@ -2760,8 +2786,13 @@ class Source(OwnPerms, ImageModel, models.Model): additional_information = models.TextField(_(u"Additional information"), blank=True, null=True) duplicate = models.BooleanField(_(u"Has a duplicate"), default=False) + TABLE_COLS = ['title', 'source_type', 'authors', 'associated_url'] COL_LINK = ['associated_url'] + BASE_SEARCH_VECTORS = ['title', 'source_type__label', 'external_id', + 'reference', 'description', 'comment', + 'additional_information'] + PARENT_SEARCH_VECTORS = ['authors'] class Meta: abstract = True |