Divided separate get_sync_query_set method for easy reusage

This commit is contained in:
M1ha 2018-12-19 15:11:17 +05:00
parent 76b01c13de
commit 6a1ffbadd3

View File

@ -4,10 +4,9 @@ This file defines base abstract models to inherit from
import datetime import datetime
from collections import defaultdict from collections import defaultdict
from itertools import chain from itertools import chain
from typing import List, Tuple, Iterable from typing import List, Tuple, Iterable, Set, Any
from django.db.models import Model as DjangoModel from django.db.models import Model as DjangoModel, QuerySet as DjangoQuerySet
from django.utils.timezone import now
from infi.clickhouse_orm.database import Database from infi.clickhouse_orm.database import Database
from infi.clickhouse_orm.engines import CollapsingMergeTree from infi.clickhouse_orm.engines import CollapsingMergeTree
from infi.clickhouse_orm.models import Model as InfiModel, ModelBase as InfiModelBase from infi.clickhouse_orm.models import Model as InfiModel, ModelBase as InfiModelBase
@ -135,6 +134,16 @@ class ClickHouseModel(with_metaclass(ClickHouseModelMeta, InfiModel)):
return True return True
@classmethod
def get_sync_query_set(cls, using, pk_set): # type: (str, Set[Any]) -> DjangoQuerySet
"""
Forms django queryset to fetch for sync
:param using: Database to fetch from
:param pk_set: A set of primary keys to fetch
:return: QuerySet
"""
return cls.django_model.objects.filter(pk__in=pk_set).using(using)
@classmethod @classmethod
def get_sync_objects(cls, operations): # type: (List[Tuple[str, str]]) -> List[DjangoModel] def get_sync_objects(cls, operations): # type: (List[Tuple[str, str]]) -> List[DjangoModel]
""" """
@ -150,10 +159,7 @@ class ClickHouseModel(with_metaclass(ClickHouseModelMeta, InfiModel)):
using, pk = pk_str.split('.') using, pk = pk_str.split('.')
pk_by_db[using].add(pk) pk_by_db[using].add(pk)
objs = chain(*( objs = chain(*(cls.get_sync_query_set(using, pk_set) for using, pk_set in pk_by_db.items()))
cls.django_model.objects.filter(pk__in=pk_set).using(using)
for using, pk_set in pk_by_db.items()
))
return list(objs) return list(objs)
@classmethod @classmethod