summaryrefslogtreecommitdiff
path: root/chimere/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-09-30 11:28:56 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-09-30 11:28:56 +0200
commitc073239b4ff54fdf9334e53137219c4da611118c (patch)
tree34e5cd326e2f5380ddd4ecfb10d712fde1cd28ef /chimere/models.py
parent4d7c7c9410d86160cfac73c0e1ace599b9e34a73 (diff)
downloadChimère-c073239b4ff54fdf9334e53137219c4da611118c.tar.bz2
Chimère-c073239b4ff54fdf9334e53137219c4da611118c.zip
Manage search pagination
Diffstat (limited to 'chimere/models.py')
-rw-r--r--chimere/models.py36
1 files changed, 26 insertions, 10 deletions
diff --git a/chimere/models.py b/chimere/models.py
index 46a778c..451596b 100644
--- a/chimere/models.py
+++ b/chimere/models.py
@@ -609,6 +609,7 @@ class GeographicItem(models.Model):
search_vector = SearchVectorField(_("Search vector"), blank=True, null=True,
help_text=_("Auto filled at save"))
+ geom_attr = ''
default_values = {}
json_color_keys = []
@@ -861,7 +862,8 @@ class GeographicItem(models.Model):
raise NotImplementedError()
@classmethod
- def getGeoJSONs(cls, queryset, limit_to_categories=None, slice=None):
+ def getGeoJSONs(cls, queryset, limit_to_categories=None, slice=None,
+ check_next=False):
default_values = copy.copy(cls.default_values)
q_values = ['json', 'name', 'pk', 'categories__pk'] + \
cls.json_color_keys
@@ -873,9 +875,8 @@ class GeographicItem(models.Model):
start, end = 0, None
if slice:
start, end = slice
+ has_next = False
for item in q.all():
- if end and len(vals) > end:
- break
if item['pk'] in added:
continue
if limit_to_categories and \
@@ -884,7 +885,11 @@ class GeographicItem(models.Model):
added.append(item['pk'])
if start:
start -= 1
+ end -= 1
continue
+ if end and len(vals) > end:
+ has_next = True
+ break
properties = {"pk": item['pk'], "name": item['name'],
'key': "{}-{}".format(cls.geom_attr, item['pk'])}
icon_and_colors, current_categories = \
@@ -905,18 +910,24 @@ class GeographicItem(models.Model):
"geometry": geom,
"properties": properties
})
- return vals
+ if not check_next:
+ return vals
+ return vals, has_next
@classmethod
- def search(cls, query, area=None, get_json=False, slice=None):
+ def search(cls, query, area=None, get_json=False, slice=None,
+ check_next=False):
"""
Search items using search_vector
:param query: terms to search
:param area: area limitation (default None)
:param get_json: output as json if True else return a queryset
- :param slice: startting and ending bounds
- :return: items matching the search query and the area
+ :param slice: starting and ending bounds
+ :param check_next: check if next items are available (only relevant
+ if a slice is provided)
+ :return: items matching the search query - if check next to true
+ also return if next items are available
"""
subcats = SubCategory.getAvailable(instance=True, area=area)
q = cls.objects.filter(categories__in=subcats,
@@ -924,11 +935,16 @@ class GeographicItem(models.Model):
query,
config=settings.CHIMERE_SEARCH_LANGUAGE))
if get_json:
- return cls.getGeoJSONs(q, slice=slice)
+ return cls.getGeoJSONs(q, slice=slice, check_next=check_next)
if slice:
- return q.all()[slice[0]:slice[1]]
+ if not check_next:
+ return q.all()[slice[0]:slice[1]]
+ has_next = q.count() > slice[1]
+ return q.all()[slice[0]:slice[1]], has_next
else:
- return q.all()
+ if not check_next:
+ return q.all()
+ return q.all(), False
def update_search_vector(self, save=True):
"""