How to use paginated_list method in localstack

Best Python code snippet using localstack_python

paginations.py

Source:paginations.py Github

copy

Full Screen

1from dateutil import parser2from django.conf import settings3from rest_framework.pagination import BasePagination4from rest_framework.response import Response5class EndlessPagination(BasePagination):6 page_size = 20 if not settings.TESTING else 107 def __init__(self):8 super(EndlessPagination, self).__init__()9 self.has_next_page = False10 def to_html(self):11 pass12 def paginate_ordered_list(self, reverse_ordered_list, request):13 """14 request 里 确定了要取哪一页15 """16 # 朋友圈下拉,刷出来新的数据17 if 'created_at__gt' in request.query_params:18 created_at__gt = parser.isoparse(request.query_params['created_at__gt'])19 objects = []20 for obj in reverse_ordered_list:21 if obj.created_at > created_at__gt:22 objects.append(obj)23 else:24 break25 self.has_next_page = False26 return objects27 index = 028 # 朋友圈上拉,刷旧的数据29 if 'created_at__lt' in request.query_params:30 created_at__lt = parser.isoparse(request.query_params['created_at__lt'])31 for index, obj in enumerate(reverse_ordered_list):32 if obj.created_at < created_at__lt:33 break34 else:35 # 没找到任何满足条件的 objects, 返回空数组36 # 注意这个 else 对应的是 for,参见 python 的 for else 语法37 reverse_ordered_list = []38 self.has_next_page = len(reverse_ordered_list) > index + self.page_size39 return reverse_ordered_list[index: index + self.page_size]40 def paginate_queryset(self, queryset, request, view=None):41 if 'created_at__gt' in request.query_params:42 # min_id 用于下拉刷新的时候加载最新的内容进来43 # 为了简便起见,下拉刷新不做翻页机制,直接加载所有更新的数据44 # 因为如果数据很久没有更新的话,不会采用下拉刷新的方式进行更新,而是重新加载最新的数据45 created_at__gt = request.query_params['created_at__gt']46 queryset = queryset.filter(created_at__gt=created_at__gt)47 self.has_next_page = False48 return queryset.order_by('-id')49 if 'created_at__lt' in request.query_params:50 # max_id 用于向上滚屏(往下翻页)的时候加载下一页的数据51 # 寻找 id < max_id 的 objects 里按照 id 倒序的前 page_size + 1 个 objects52 # 比如目前的id列表是 [10, 9, 8, 7 .. 1] 如果 max_id=10, page_size = 253 # 则应该返回 [9, 8, 7],多返回一个 object 的原因是为了判断是否还有下一页54 # 从而减少一次空加载。55 created_at__lt = request.query_params['created_at__lt']56 queryset = queryset.filter(created_at__lt=created_at__lt)57 queryset = queryset.order_by('-id')[:self.page_size + 1]58 self.has_next_page = len(queryset) > self.page_size59 return queryset[:self.page_size]60 def paginate_cached_list(self, cached_list, request):61 """62 输入所有 cached obj list, 输出别的63 """64 paginated_list = self.paginate_ordered_list(cached_list, request)65 # 如果是上翻页,paginated_list 里是所有的最新的数据,直接返回66 if 'created_at__gt' in request.query_params:67 return paginated_list68 # 如果还有下一页,说明 cached_list 里的数据还没有取完,也直接返回69 if self.has_next_page:70 return paginated_list71 # 如果 cached_list 的长度不足最大限制,说明 cached_list 里已经是所有数据了72 # cache 缓存里的数据有可能超过我们写的 redis 限制,所以这里限制下:73 # 若超过限制,直接去数据库里取内容74 if len(cached_list) < settings.REDIS_LIST_LENGTH_LIMIT:75 return paginated_list76 # 如果进入这里,说明可能存在在数据库里没有 load 在 cache 里的数据,需要直接去数据库查询77 return None78 def get_paginated_response(self, data):79 return Response({80 'has_next_page': self.has_next_page,81 'results': data,...

Full Screen

Full Screen

custom.py

Source:custom.py Github

copy

Full Screen

1from django import forms2import logging3from django.core.exceptions import ValidationError4from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage5from django.forms import widgets6from django.utils.encoding import force_text7from apps.custom_base.widgets.material import InputTextMaterial, TextAreaMaterial, SelectMaterial, URLMaterial, \8 EmailMaterial, NumberMaterial, BooleanMaterial, MaterialSelectMultiple9logger = logging.getLogger('error')10class AbstractIdeiaForm(object):11 def process(self):12 # :try:13 return self.__process__() if self.is_valid() else False14 # except NotImplementedError:15 # raise NotImplementedError16 # except Exception, e:17 # if settings.DEBUG:18 # raise e19 # else:20 # logger.error(e)21 # self.add_error(None, "General error!")22 # return False23 def __process__(self):24 raise NotImplementedError25 def is_valid(self):26 pass27 def add_error(self, *args, **kwargs):28 pass29class IdeiaForm(forms.Form, AbstractIdeiaForm):30 def __init__(self, *args, **kwargs):31 super(IdeiaForm, self).__init__(*args, **kwargs)32class IdeiaModelForm(forms.ModelForm, AbstractIdeiaForm):33 def __init__(self, *args, **kwargs):34 super(IdeiaModelForm, self).__init__(*args, **kwargs)35MATERIAL_WIDGETS = {36 widgets.TextInput: InputTextMaterial,37 widgets.Textarea: TextAreaMaterial,38 widgets.Select: SelectMaterial,39 widgets.URLInput: URLMaterial,40 widgets.EmailInput: EmailMaterial,41 widgets.NumberInput: NumberMaterial,42 widgets.CheckboxInput: BooleanMaterial,43 widgets.SelectMultiple: MaterialSelectMultiple,44}45class MaterialModelForm(forms.ModelForm):46 def __update_fields__(self, attrs=None):47 for key in self.fields:48 field = self.fields[key]49 widget = field.widget50 if (not self._meta.widgets or key not in self._meta.widgets) and type(widget) in MATERIAL_WIDGETS:51 field.widget = MATERIAL_WIDGETS[type(widget)](attrs=attrs)52 field.widget.label = field.label53 if hasattr(field, "choices"):54 field.widget.choices = field.choices55 def __init__(self, *args, **kwargs):56 super(MaterialModelForm, self).__init__(*args, **kwargs)57 attrs = kwargs.get('attrs')58 self.__update_fields__(attrs=attrs)59 def add_error(self, field, error):60 super(MaterialModelForm, self).add_error(field=field, error=error)61 self.__update_error__(field=field)62 def __update_error__(self, field):63 if field:64 widget = self.fields[field].widget65 if (not self._meta.widgets or field and field not in self._meta.widgets) \66 and type(widget) in MATERIAL_WIDGETS.values() and field in self.errors:67 self.fields[field].widget.errors = self.errors[field]68 def is_valid(self):69 valid = super(MaterialModelForm, self).is_valid()70 for key in self.fields:71 self.__update_error__(field=key)72 return valid73class CustomPaginator():74 @staticmethod75 def paginate(queryset=None, itens_per_page=10, page=1):76 paginated_list = Paginator(queryset, itens_per_page)77 try:78 paginated_list = paginated_list.page(page)79 except PageNotAnInteger:80 paginated_list = paginated_list.page(1)81 except EmptyPage:82 paginated_list = []...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful