From 94231aa99eec659c6ef20d0f575e7509c81a0031 Mon Sep 17 00:00:00 2001 From: Ian Pickering Date: Sat, 8 Mar 2014 22:20:56 -0600 Subject: [PATCH 1/4] added search ability by location and type --- server/main/models.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/server/main/models.py b/server/main/models.py index de5f30f..04cdc5a 100644 --- a/server/main/models.py +++ b/server/main/models.py @@ -6,11 +6,26 @@ class ItemManager(models.Manager): def search(self, search_query): terms = [term.strip() for term in search_query.split()] - - q_objects = [] + + queries = {} for term in terms: - q_objects.append(Q(name__icontains=term)) + things = term.split(':'); + if len(things) == 1: + queries['name'] = queries['name'] + ' ' + things[0] + else: + queries[things[0]] = things[1] + + q_objects = [] + + for kind, value in queries.iteritems(): + if kind == 'type': + q_objects.append(Q(item_type__name__icontains=value)) + if kind == 'location': + q_objects.append(Q(location__icontains=value)) + else: + q_objects.append(Q(name__icontains=value)) + qs = self.get_query_set() From 9d5de6e4639a169dddb598eceae49eb9f6f11f8d Mon Sep 17 00:00:00 2001 From: Ian Pickering Date: Sun, 9 Mar 2014 00:01:36 -0600 Subject: [PATCH 2/4] added full attribute-searching functionality (use first word for multi-word attributes) --- server/main/models.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/server/main/models.py b/server/main/models.py index 04cdc5a..1e3fbb6 100644 --- a/server/main/models.py +++ b/server/main/models.py @@ -12,27 +12,35 @@ def search(self, search_query): for term in terms: things = term.split(':'); if len(things) == 1: + if not 'name' in queries: + queries['name'] = things[0] + else: queries['name'] = queries['name'] + ' ' + things[0] else: - queries[things[0]] = things[1] + queries[things[0]] = things[1] q_objects = [] + qs = self.get_query_set() + + print queries + for kind, value in queries.iteritems(): if kind == 'type': - q_objects.append(Q(item_type__name__icontains=value)) - if kind == 'location': - q_objects.append(Q(location__icontains=value)) + qs = qs.filter(item_type__name__icontains=value) + elif kind == 'location': + qs = qs.filter(location__icontains=value) + elif any(kind in s.split()[0].lower() for s in Attribute.objects.values_list('name', flat=True)): + qs = qs.filter(attributevalue__attribute__name__icontains=kind, attributevalue__value__icontains=value) else: - q_objects.append(Q(name__icontains=value)) + qs = qs.filter(name__icontains=value) + print len(qs.values_list('name')) - qs = self.get_query_set() - if len(terms) == 0: return qs.filter() - return qs.filter(reduce(operator.or_, q_objects)) + return qs class Item(models.Model): item_type = models.ForeignKey('ItemType') From 0cadeb7db19f0b1ec27a2a15af95d106eda60100 Mon Sep 17 00:00:00 2001 From: Ian Pickering Date: Sun, 9 Mar 2014 00:25:54 -0600 Subject: [PATCH 3/4] add id search function --- server/main/models.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/server/main/models.py b/server/main/models.py index 1e3fbb6..4bc3ae3 100644 --- a/server/main/models.py +++ b/server/main/models.py @@ -19,23 +19,19 @@ def search(self, search_query): else: queries[things[0]] = things[1] - q_objects = [] - qs = self.get_query_set() - print queries - for kind, value in queries.iteritems(): if kind == 'type': qs = qs.filter(item_type__name__icontains=value) elif kind == 'location': qs = qs.filter(location__icontains=value) + elif kind == 'id' and value.isdigit(): + qs = qs.filter(pk__exact=value) elif any(kind in s.split()[0].lower() for s in Attribute.objects.values_list('name', flat=True)): qs = qs.filter(attributevalue__attribute__name__icontains=kind, attributevalue__value__icontains=value) else: qs = qs.filter(name__icontains=value) - print len(qs.values_list('name')) - if len(terms) == 0: return qs.filter() From b5a5b1b18035bfff1b36153ff9c5aff001e8f464 Mon Sep 17 00:00:00 2001 From: Ian Pickering Date: Sun, 9 Mar 2014 00:31:14 -0600 Subject: [PATCH 4/4] add owner search function --- server/main/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/main/models.py b/server/main/models.py index 4bc3ae3..ea756fd 100644 --- a/server/main/models.py +++ b/server/main/models.py @@ -28,6 +28,8 @@ def search(self, search_query): qs = qs.filter(location__icontains=value) elif kind == 'id' and value.isdigit(): qs = qs.filter(pk__exact=value) + elif kind == 'owner': + qs = qs.filter(owner_id__username__icontains=value) elif any(kind in s.split()[0].lower() for s in Attribute.objects.values_list('name', flat=True)): qs = qs.filter(attributevalue__attribute__name__icontains=kind, attributevalue__value__icontains=value) else: