def test_limit_with_start_simple_list(self): q = Query() q.append_collection(self.test_1_col.name) q.order_by('little_number') q.limit(count=1, start=1) docs = q.execute() self.assertEqual(len(docs), 1) doc1 = docs[0] self.assertDocumentsEqual(doc1, self.col1_doc3)
def test_sorting_asc_document_list(self): q = Query() q.append_collection(self.test_1_col.name) q.order_by('little_number') docs = q.execute() self.assertEqual(len(docs), 3) doc1 = docs[0] doc2 = docs[1] doc3 = docs[2] self.assertDocumentsEqual(doc1, self.col1_doc2) self.assertDocumentsEqual(doc2, self.col1_doc3) self.assertDocumentsEqual(doc3, self.col1_doc1)
class CollectionQueryset(LazyQueryset): """ """ def __init__(self, manager): """ """ super(CollectionQueryset, self).__init__(manager=manager) self._query = Query() # Relations self._is_working_on_relations = False self._relations_start_model = None self._relations_end_model = None self._relations_relation_collection = None self._related_model_class = None def get_field_relations(self, relation_collection, related_model_class, start_model=None, end_model=None): """ """ self._is_working_on_relations = True self._has_cache = False self._relations_start_model = start_model self._relations_end_model = end_model self._relations_relation_collection = relation_collection self._related_model_class = related_model_class return self def get(self, **kwargs): """ """ return self._manager.get(**kwargs) def all(self): """ """ self._has_cache = False self._query.set_collection(self._collection.name) self._query.clear() return self def filter(self, bit_operator=Query.NO_BIT_OPERATOR, **kwargs): """ """ self._has_cache = False kwargs = self._normalize_kwargs(**kwargs) self._query.filter(bit_operator=bit_operator, **kwargs) return self def exclude(self, **kwargs): """ """ self._has_cache = False kwargs = self._normalize_kwargs(**kwargs) self._query.exclude(**kwargs) return self def limit(self, count, start=-1): """ """ self._has_cache = False self._query.limit(count, start) return self def order_by(self, field, order): """ """ self._has_cache = False self._query.order_by(field=field, order=order) return self def _normalize_kwargs(self, **kwargs): """ """ # You can use this way models for key, value in kwargs.items(): if isinstance(value, CollectionModel): kwargs[key] = value.id return kwargs def _clone(self): """ """ cloned_queryset = CollectionQueryset(manager=self._manager) # Query cloned_queryset._query = copy.deepcopy(self._query) # Relation cloned_queryset._is_working_on_relations = self._is_working_on_relations cloned_queryset._relations_start_model = self._relations_start_model cloned_queryset._relations_end_model = self._relations_end_model cloned_queryset._relations_relation_collection = self._is_working_on_relations cloned_queryset._related_model_class = self._related_model_class return cloned_queryset def _generate_cache(self): """ """ super(CollectionQueryset, self)._generate_cache() if self._is_working_on_relations: start_model = self._relations_start_model end_model = self._relations_end_model relation_collection = self._relations_relation_collection if start_model: found_relations = Traveser.follow( start_vertex=start_model.document.id, edge_collection=relation_collection, direction='outbound' ) else: found_relations = Traveser.follow( start_vertex=end_model.document.id, edge_collection=relation_collection, direction='inbound' ) result = found_relations result_class = self._related_model_class else: result = self._query.execute() result_class = self._manager._model_class for doc in result: model = self._manager._create_model_from_doc(doc=doc, model_class=result_class) self._cache.append(model)
class CollectionQueryset(object): """ """ def __init__(self, manager): """ """ self._manager = manager self._collection = manager._model_class.collection_instance self._query = Query() # Relations self._is_working_on_relations = False self._relations_start_model = None self._relations_end_model = None self._relations_relation_collection = None self._related_model_class = None # Cache self._has_cache = False self._cache = [] def get_field_relations(self, relation_collection, related_model_class, start_model=None, end_model=None): """ """ self._is_working_on_relations = True self._has_cache = False self._relations_start_model = start_model self._relations_end_model = end_model self._relations_relation_collection = relation_collection self._related_model_class = related_model_class return self def all(self): """ """ self._has_cache = False self._query.set_collection(self._collection.name) self._query.clear() return self def filter(self, bit_operator=Query.NO_BIT_OPERATOR, **kwargs): """ """ self._has_cache = False self._query.filter(bit_operator=bit_operator, **kwargs) return self def exclude(self, **kwargs): """ """ self._has_cache = False self._query.exclude(**kwargs) return self def limit(self, count, start=-1): """ """ self._has_cache = False self._query.limit(count, start) return self def order_by(self, field, order): """ """ self._has_cache = False self._query.order_by(field=field, order=order) return self def _clone(self): """ """ cloned_queryset = CollectionQueryset(manager=self._manager) # Query cloned_queryset._query = copy.deepcopy(self._query) # Relation cloned_queryset._is_working_on_relations = self._is_working_on_relations cloned_queryset._relations_start_model = self._relations_start_model cloned_queryset._relations_end_model = self._relations_end_model cloned_queryset._relations_relation_collection = self._is_working_on_relations cloned_queryset._related_model_class = self._related_model_class return cloned_queryset def _generate_cache(self): """ """ self._cache = [] # TODO: Check how to best clear this list self._has_cache = True if self._is_working_on_relations: start_model = self._relations_start_model end_model = self._relations_end_model relation_collection = self._relations_relation_collection if start_model: found_relations = Traveser.follow( start_vertex=start_model.document.id, edge_collection=relation_collection, direction='outbound' ) else: found_relations = Traveser.follow( start_vertex=end_model.document.id, edge_collection=relation_collection, direction='inbound' ) result = found_relations result_class = self._related_model_class else: result = self._query.execute() result_class = self._manager._model_class for doc in result: model = self._manager._create_model_from_doc(doc=doc, model_class=result_class) self._cache.append(model) def __getitem__(self, item): """ Is used for the index access """ if not self._has_cache: self._generate_cache() return self._cache[item] def __len__(self): """ """ if not self._has_cache: self._generate_cache() return len(self._cache)
class CollectionQueryset(LazyQueryset): """ """ def __init__(self, manager): """ """ super(CollectionQueryset, self).__init__(manager=manager) self._query = Query() # Relations self._is_working_on_relations = False self._relations_start_model = None self._relations_end_model = None self._relations_relation_collection = None self._related_model_class = None def get_field_relations(self, relation_collection, related_model_class, start_model=None, end_model=None): """ """ self._is_working_on_relations = True self._has_cache = False self._relations_start_model = start_model self._relations_end_model = end_model self._relations_relation_collection = relation_collection self._related_model_class = related_model_class return self def get(self, **kwargs): """ """ return self._manager.get(**kwargs) def all(self): """ """ self._has_cache = False self._query.set_collection(self._collection.name) self._query.clear() return self def filter(self, bit_operator=Query.NO_BIT_OPERATOR, **kwargs): """ """ self._has_cache = False kwargs = self._normalize_kwargs(**kwargs) self._query.filter(bit_operator=bit_operator, **kwargs) return self def exclude(self, **kwargs): """ """ self._has_cache = False kwargs = self._normalize_kwargs(**kwargs) self._query.exclude(**kwargs) return self def limit(self, count, start=-1): """ """ self._has_cache = False self._query.limit(count, start) return self def order_by(self, field, order): """ """ self._has_cache = False self._query.order_by(field=field, order=order) return self def _normalize_kwargs(self, **kwargs): """ """ # You can use this way models for key, value in list(kwargs.items()): if isinstance(value, CollectionModel): kwargs[key] = value.id return kwargs def _clone(self): """ """ cloned_queryset = CollectionQueryset(manager=self._manager) # Query cloned_queryset._query = copy.deepcopy(self._query) # Relation cloned_queryset._is_working_on_relations = self._is_working_on_relations cloned_queryset._relations_start_model = self._relations_start_model cloned_queryset._relations_end_model = self._relations_end_model cloned_queryset._relations_relation_collection = self._is_working_on_relations cloned_queryset._related_model_class = self._related_model_class return cloned_queryset def _generate_cache(self): """ """ super(CollectionQueryset, self)._generate_cache() if self._is_working_on_relations: start_model = self._relations_start_model end_model = self._relations_end_model relation_collection = self._relations_relation_collection if start_model: found_relations = Traveser.follow( start_vertex=start_model.document.id, edge_collection=relation_collection, direction='outbound') else: found_relations = Traveser.follow( start_vertex=end_model.document.id, edge_collection=relation_collection, direction='inbound') result = found_relations result_class = self._related_model_class else: result = self._query.execute() result_class = self._manager._model_class for doc in result: model = self._manager._create_model_from_doc( doc=doc, model_class=result_class) self._cache.append(model)