test_mongo_paginate.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # coding=utf-8
  2. import os
  3. from base import init_env
  4. os.environ["DJANGO_SETTINGS_MODULE"] = "configs.dev_zjl"
  5. init_env(False)
  6. from apps.web.device.models import Device
  7. from apps.web.common.proxy import QuerySetProxy
  8. class SubQuerySet(object):
  9. def __init__(self, numbers, flag):
  10. """
  11. list [1, 2, 3, 4, ......]
  12. """
  13. self._data = numbers
  14. self._count = len(numbers)
  15. self._flag = flag
  16. def count(self):
  17. print self._count
  18. return self._count
  19. @property
  20. def amountOfData(self):
  21. return self._data
  22. @amountOfData.setter
  23. def amountOfData(self, value):
  24. from copy import copy
  25. new_value = copy(value)
  26. self._data = new_value
  27. def skip(self, n):
  28. """
  29. 跳过的意思就是有多少条数据不需要获得
  30. """
  31. assert n >= 0, "skip numbers must >=0 "
  32. self.amountOfData = self.amountOfData[n:]
  33. return self
  34. def limit(self, n):
  35. assert n >= 0, "skip numbers must >=0 "
  36. return self.__class__(self.amountOfData[:n], self._flag)
  37. def __iter__(self):
  38. for i in self.amountOfData:
  39. yield "{}-{}".format(self._flag, i)
  40. def do_1():
  41. TestQuerySetOne = SubQuerySet(range(1, 97), "one")
  42. TestQuerySetTwo = SubQuerySet(range(97, 1001), "two")
  43. # 输出 第 3 页 
  44. TestPageIndex = 10
  45. TestPageSize = 10
  46. for i in QuerySetProxy(TestQuerySetOne, TestQuerySetTwo).paginate(pageIndex=TestPageIndex, pageSize=TestPageSize):
  47. print i
  48. do_1()