From fcb58f0f4ce353c2aa8bef3f68b7d70572ae63fa Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Mon, 22 Jun 2020 14:20:26 +0300 Subject: [PATCH] List APIs: fix the stoken calculation for collections. I'm not sure why it just wouldn't work with aggregate. I also couldn't get it to work with annotate then aggregate or any other alternative. --- django_etebase/views.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/django_etebase/views.py b/django_etebase/views.py index 2944fdb..955c49f 100644 --- a/django_etebase/views.py +++ b/django_etebase/views.py @@ -106,9 +106,15 @@ class BaseViewSet(viewsets.ModelViewSet): return queryset, stoken_rev def get_queryset_stoken(self, queryset): - aggr_fields = {x: Max(x) for x in self.stoken_id_fields} - aggr = queryset.aggregate(**aggr_fields) - maxid = max(map(lambda x: x or -1, aggr.values())) + aggr_field_names = ['max_{}'.format(i) for i, x in enumerate(self.stoken_id_fields)] + aggr_fields = {name: Max(field) for name, field in zip(aggr_field_names, self.stoken_id_fields)} + aggr = queryset.annotate(**aggr_fields).values(*aggr_field_names) + # FIXME: we are doing it in python instead of SQL because I just couldn't get aggregate to work over the + # annotated values. This should probably be fixed as it could be quite slow + maxid = -1 + for row in aggr: + rowmaxid = max(map(lambda x: x or -1, row.values())) + maxid = max(maxid, rowmaxid) new_stoken = (maxid >= 0) and Stoken.objects.get(id=maxid).uid return queryset, new_stoken