Best Python code snippet using localstack_python
views_json.py
Source:views_json.py
1import logging2from typing import Type3from django.db import models4from django.http import (5 HttpRequest,6 JsonResponse,7 HttpResponse,8)9from .managers import SortableManager10from .models import Photographer, Pic11logger = logging.getLogger('pics')12GET_METHOD_RESPONSE = JsonResponse(13 {'result': 'This view does not handle get requests, sorry'},14 status=40015)16EXCEPTION_500_RESPONSE = JsonResponse(17 {'result': 'Something went horribly wrong'},18 status=50019)20EXCEPTION_415_RESPONSE = JsonResponse({'success': False}, status=415)21SUCCESS_RESPONSE = JsonResponse({'success': True}, status=200)22# path: 'phs/savepics/<str:pk>/'23def save_new_pics(request: HttpRequest, pk: str) -> JsonResponse:24 response = GET_METHOD_RESPONSE25 if request.method == 'POST':26 try:27 ph = Photographer.objects.get(pk=pk)28 files = files_from_request(request)29 pics_created = ph.pics_from_files(files)30 response = JsonResponse(31 data={32 'pics_created': pics_created,33 'phpk': pk,34 },35 status=20036 )37 except Exception as e:38 logger.error(e)39 response = EXCEPTION_415_RESPONSE40 return response41# path: 'phs/delpic/'42def delete_pic(request: HttpRequest) -> JsonResponse:43 response = GET_METHOD_RESPONSE44 try:45 Pic.objects.get(pk=request.POST.get('obj_pk')).delete()46 response = SUCCESS_RESPONSE47 except Exception as e:48 print(e)49 response = EXCEPTION_415_RESPONSE50 return response51# path: 'phs/markmain/<str:pk>/'52def mark_pic_as_main(request: HttpRequest, pk: str) -> JsonResponse:53 response = GET_METHOD_RESPONSE54 try:55 ph = Photographer.objects.get(pk=pk)56 pic = Pic.objects.get(pk=request.POST.get('obj_pk'))57 ph.set_new_main_pic(pic)58 response = SUCCESS_RESPONSE59 except Exception as e:60 print(e)61 response = EXCEPTION_415_RESPONSE62 return response63# path: 'phs/sort/'64def sort_ph(request: HttpRequest) -> JsonResponse:65 obj_model = Photographer66 manager = Photographer.objects67 return sort_object(obj_model, manager, request)68# path: 'phs/sortpics/<str:pk>/'69def sort_pic(request: HttpRequest, pk: str) -> JsonResponse:70 obj_model = Pic71 ph = Photographer.objects.get(pk=pk)72 manager = ph.pics73 return sort_object(obj_model, manager, request)74def sort_object(obj_model: Type[models.Model],75 manager: Type[SortableManager],76 request: HttpRequest) -> JsonResponse:77 response = GET_METHOD_RESPONSE78 if request.method == 'POST':79 try:80 manager.insort(81 obj=obj_model.objects.get(pk=request.POST.get('obj_pk')),82 new_idx=int(request.POST.get('new_idx'))83 )84 response = SUCCESS_RESPONSE85 except Exception as e:86 response = EXCEPTION_500_RESPONSE87 return response88def files_from_request(request: HttpRequest) -> list:89 return [90 request.FILES.get(f'pic[{i}]') for i in range(0, len(request.FILES))...
tshoot.py
Source:tshoot.py
...14pprint(client.get_integration_response(restApiId=manual_api_id, resourceId=manual_rsrc, httpMethod='OPTIONS', statusCode='200'))15print('DEVOPS OPTIONS INTEGRATION RESPONSE')16pprint(client.get_integration_response(restApiId=devops_api_id, resourceId=devops_rsrc, httpMethod='OPTIONS', statusCode='200'))17print('MANUAL OPTIONS METHOD RESPONSE')18pprint(client.get_method_response(restApiId=manual_api_id, resourceId=manual_rsrc, httpMethod='OPTIONS', statusCode='200'))19print('DEVOPS OPTIONS METHOD RESPONSE')20pprint(client.get_method_response(restApiId=devops_api_id, resourceId=devops_rsrc, httpMethod='OPTIONS', statusCode='200'))21print('MANUAL POST INTEGRATION')22pprint(client.get_integration(restApiId=manual_api_id, resourceId=manual_rsrc, httpMethod='POST'))23print('DEVOPS POST INTEGRATION')...
poll.py
Source:poll.py
...3 def __init__(self):4 self.token = '5f1c2baf5f1c2baf5f1c2baf995f6b69b455f1c5f1c2baf3f708d23efda03adc95d1b31'5 self.__version = '5.92'6 self.__domain = 'bells996'7 def get_method_response(self, method):8 response = requests.get(f'https://api.vk.com/method/{method}',9 params={10 'access_token': self.token,11 'v': self.__version,12 'domain': self.__domain13 })14 return response.json()15 def get_poll_results(self, post_id=None):16 response = self.get_method_response('wall.get')['response']17 poll_results = response['items'][0]18 if post_id is not None:19 for item in response['items']:20 if item['id'] == post_id:21 poll_results = item22 break23 else:24 for item in response['items']:25 if 'attachments' in item:26 if item['attachments'][0]['type'] == 'poll':27 poll_results = item28 break29 votes_amount = poll_results['attachments'][0]['poll']['votes']30 answers = sorted(poll_results['attachments'][0]['poll']['answers'], key=lambda answer: answer['rate'])...
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!!