Best Python code snippet using localstack_python
paginations.py
Source:paginations.py
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,...
custom.py
Source:custom.py
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 = []...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!