Best Python code snippet using localstack_python
views.py
Source:views.py
1from rest_framework.decorators import api_view2from django.views.decorators.csrf import csrf_exempt3from drf_yasg.utils import swagger_auto_schema4from master_file import open_api_serializers, keys, open_api_keys, messages, response_key5from user_form import utils as user_form_utils6from rest_framework.parsers import MultiPartParser7from rest_framework.decorators import parser_classes8from user_form import serializer as user_form_serializer9@swagger_auto_schema(10 method="post", request_body=open_api_serializers.UserFormDataPOSTSerializer, manual_parameters=[open_api_keys.HEADER_TOKEN])11@csrf_exempt12@api_view(["POST"])13@parser_classes([MultiPartParser])14def create_form(request):15 response_json = {}16 user = request.user17 request_data = request.data.copy()18 request_data[keys.USER] = user.id19 serializer = user_form_serializer.UserFormDataSerializer(data = request_data)20 if serializer.is_valid():21 serializer.save()22 response_json[keys.FORM_DETAIL] = serializer.data23 return response_key.SIMPLE_SUCCESS_RESPONSE(response=response_json)24 else:25 return response_key.SIMPLE_ERROR_RESPONSE(msg=serializer.errors)26@swagger_auto_schema(27 method="get", manual_parameters=[open_api_keys.HEADER_TOKEN])28@api_view(["GET"])29def get_form_list(request):30 user = request.user31 response_json = {}32 form_list = user_form_utils.get_user_form_list(user)33 form_list = user_form_serializer.UserFormDataSerializer(form_list, many=True).data34 response_json[keys.FORM_LIST] = form_list35 return response_key.SIMPLE_SUCCESS_RESPONSE(response=response_json)36@swagger_auto_schema(37 method="get", manual_parameters=[open_api_keys.FORM_ID])38@api_view(["GET"])39def get_form_field(request):40 response_json = {}41 form_id = request.GET.get(keys.FORM_ID, None)42 if not form_id:43 return response_key.SIMPLE_ERROR_RESPONSE(msg = messages.INALID_ID)44 form_field = user_form_utils.get_form_field_detail(request)45 response_json[keys.FORM_FIELD_DETAIL] = form_field46 return response_key.SIMPLE_SUCCESS_RESPONSE(response=response_json)47@swagger_auto_schema(48 method="get", manual_parameters=[open_api_keys.HEADER_TOKEN,open_api_keys.FORM_ID])49@api_view(["GET"])50def form_data_list(request):51 response_json = {}52 if not request.GET.get(keys.FORM_ID, None):53 return response_key.SIMPLE_ERROR_RESPONSE(msg = messages.FORM_ID)54 form_data = user_form_utils.get_data_list(request)55 response_json[keys.FORM_DATA_LIST] = form_data56 return response_key.SIMPLE_SUCCESS_RESPONSE(response=response_json)57@swagger_auto_schema(58 method="get", manual_parameters=[open_api_keys.FORM_ID, open_api_keys.ID])59@swagger_auto_schema(60 method="post", manual_parameters=[open_api_keys.FORM_ID])61@swagger_auto_schema(62 method="patch", manual_parameters=[open_api_keys.FORM_ID, open_api_keys.ID])63@swagger_auto_schema(64 method="delete", manual_parameters=[open_api_keys.FORM_ID, open_api_keys.ID])65@api_view(["GET","POST","PATCH","DELETE"])66def form_data(request):67 response_json = {}68 if not request.GET.get(keys.FORM_ID, None):69 return response_key.SIMPLE_ERROR_RESPONSE(msg = messages.FORM_ID)70 if request.method in [keys.GET, keys.DELETE, keys.PATCH]:71 if not request.GET.get(keys.ID, None):72 return response_key.SIMPLE_ERROR_RESPONSE(msg = messages.INALID_ID)73 if request.method == keys.GET:74 form_data = user_form_utils.get_data(request)75 elif request.method == keys.PATCH:76 form_data = user_form_utils.create_or_update_data(request)77 else:78 form_data = user_form_utils.delete_data(request)79 response_json[keys.FORM_DATA] = form_data80 else:81 form_data = user_form_utils.create_or_update_data(request)...
pre_config.py
Source:pre_config.py
...12 return self.show(pre_config_id)13 def show(self, pre_config_id):14 pre_config, json_msg = self.get_pre_config(pre_config_id)15 if pre_config is None:16 return simple_error_response(json_msg, requests.codes.not_found)17 return pre_config.to_json(), requests.codes.ok18 def post(self):19 data = request.get_json(force=True)20 try:21 pre_config = PreConfig(22 name=data.get("name", None),23 characteristics=data["characteristics"],24 subcharacteristics=data["subcharacteristics"],25 measures=data["measures"],26 )27 pre_config.save()28 except me.errors.NotUniqueError:29 return simple_error_response(30 DUPLICATED_PRE_CONFIG_NAME_MSG, requests.codes.unprocessable_entity31 )32 return pre_config.to_json(), requests.codes.created33 def patch(self, pre_config_id):34 pre_config, error_msg = self.get_pre_config(pre_config_id)35 if pre_config is None:36 return simple_error_response(error_msg, requests.codes.not_found)37 data = request.get_json(force=True)38 try:39 pre_config.update(**data)40 pre_config.reload()41 return pre_config.to_json(), requests.codes.ok42 except me.errors.NotUniqueError:43 return simple_error_response(44 DUPLICATED_PRE_CONFIG_NAME_MSG, requests.codes.unprocessable_entity45 )46 except me.errors.ValidationError as error:47 return simple_error_response(48 str(error), requests.codes.unprocessable_entity49 )50 def get_pre_config(self, pre_config_id):51 try:52 pre_config = PreConfig.objects.with_id(pre_config_id)53 if pre_config is None:54 return None, f"There is no pre configurations with ID {pre_config_id}"55 return pre_config, None56 except me.errors.ValidationError:...
items.py
Source:items.py
...29 )3031 item.save()32 except me.errors.NotUniqueError:33 return simple_error_response(34 DUPLICATED_ITEM_MSG, requests.codes.unprocessable_entity35 )3637 return item.to_json(), requests.codes.created3839 def patch(self, name):40 item, json_msg = Item.get_by_name(name)4142 item_data = request.get_json(force=True)4344 item.update(**item_data)4546 item.reload()4748 return self.__create_response(item, json_msg, requests.codes.ok)4950 def delete(self, name):51 item, json_msg = Item.get_by_name(name)5253 if item is None:54 return self.__create_response(item, json_msg, requests.codes.not_found)5556 Item.delete(item)5758 def __create_response(self, item, json_msg, response_code):59 if item is None:60 return simple_error_response(json_msg, response_code)61
...
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!!