# Django module and packages from django.views.decorators.csrf import csrf_exempt from rest_framework.decorators import action from rest_framework import viewsets # Project module and packages from common.restapiviewwrapper import ( Response, status, AllowAnyAPIView ) from common.response import \ ResponseEntity, ExceptionResponseEntity, ValidationErrorResponseEntity from common.common_methods import convert_to_dict from location.bll.location_bll import LocationManager from common.logger import CustomLogger LOGGER = CustomLogger.get_logger(__name__) class Constant: """ defining constant""" IS_ERROR = True FAILED_STATUS = "FAILED" DEFUALT_MSG = "Something Went Wrong" POST = "POST" GET = "GET" class LocationView(viewsets.ViewSet, AllowAnyAPIView): """ Location related operation """ def __init__(self, **kwargs): super(LocationView, self).__init__(**kwargs) self.success_resp = ResponseEntity()
# Django modules from rest_framework import viewsets from django.http import JsonResponse from rest_framework.decorators import action from rest_framework import status from common.logger import CustomLogger from products.serializers import ProductSerializer from products.bll.product_bll import ProductManager from products.bll.product_integrators import ProductIntegrator import pandas as pd logger = CustomLogger.get_logger(__name__) logger.info("initiating api-views") class Constant: IS_ERROR = True FAILED_STATUS = "FAILED" DEFAULT_MSG = "Something Went Wrong" POST = "POST" GET = "GET" class ProductView(viewsets.ViewSet): def __init__(self, **kwargs): super(ProductView, self).__init__(**kwargs) @action( detail=True,
#!/usr/bin/env python # -*- coding: utf-8 -*- """ This is a description of this module: 参考链接: https://www.cnblogs.com/lddragon1/p/12113437.html """ from traceback import format_exc from common.response import success, failure from common.logger import CustomLogger logger = CustomLogger("request") # 使用 request logger 实例记录日志 class CreateViewMixin: """ 通用创建 Mixin 对象 子类需要指定 queryset, serializer_class 两个类成员属性 """ def create(self, request, *args, **kwargs): """ 创建就是传递一堆数据表单验证 """ request_data = request.data many = isinstance(request_data, list) try: serializer = self.get_serializer(data=request_data, many=many) if not serializer.is_valid(): return failure(serializer.errors) # 批量创建有2种方式: 1. 当前方式, 2. 类似于批量修改的方式,使用辅助类 ListSerializer ModelClass = serializer.Meta.model # 获取当前Model类对象