Backstage Management - No.3 Commodity Management

1,kwargs

  • Objective: To obtain the parameters carried in routing by kwargs

  • Operational procedures:

    • 1. Root routing

      • url(r'^test/(?P<name>\w+)/(?P<age>\d+)/$',test.TestView.as_view()),
        
    • 2. Class View (TestView.py)

      • class TestView(APIView):
            def get(self,request,name,age):
        
                print(self.kwargs)
        
                return JsonResponse(self.kwargs)
        

2. Sku spu specification information acquisition

  • Objective: after selecting spu, we can query the specification information display of spu.

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • url(r'^goods/(?P<spu_id>\d+)/specs/$',sku_views.SPUSpecificationView.as_view())
        
    • 2. Class View (good/sku_views.py)

      • #4,spu specification information
        class SPUSpecificationView(ListAPIView):
            serializer_class = sku_serializers.SPUSpecificationSerializer
            # queryset = SPUSpecification.objects.all()
        
            def get_queryset(self):
        
                #1, get spu_id
                spu_id = self.kwargs.get("spu_id")
        
                #2. Return specification information
                return SPUSpecification.objects.filter(spu_id=spu_id).all()
        
    • 3. Serializers (good/sku_serializers.py)

      • #Option serializer
        class SPUSpecificationOption(serializers.ModelSerializer):
            class Meta:
                model = SpecificationOption
                fields = ("id","value")
        
        
        #4,spu specification serializer
        class SPUSpecificationSerializer(serializers.ModelSerializer):
        
            #1. Option field
            options = SPUSpecificationOption(read_only=True,many=True)
        
            class Meta:
                model = SPUSpecification
                fields = "__all__"
        
        

3. Sku Data Preservation Specification Serializer

  • Purpose: SKU serializer can be programmed to match the parameters carried by the front end.

  • Operational procedures:

    • 1. Serializers (good/sku_serializers.py)

      • #Specification Options Serializer
        class SKUSpecificationSerializer(serializers.Serializer):
            spec_id = serializers.IntegerField()
            option_id = serializers.IntegerField()
        
        #1. SKU serializer
        class SKUSerializer(serializers.ModelSerializer):
        
            #1,spu,spu_id
            spu = serializers.StringRelatedField(read_only=True)
            spu_id = serializers.IntegerField()
        
            #2,category,category_id
            category = serializers.StringRelatedField(read_only=True)
            category_id = serializers.IntegerField()
        
            #3. Matching specification options
            specs = SKUSpecificationSerializer(read_only=True,many=True)
        
            class Meta:
                model = SKU
                fields = "__all__"
        

4. Sku Object, Specification Storage

  • Purpose: When sku is added to the library, the associated specifications can be incorporated into the library.

  • Operational procedures:

    • 1. Serializers (good/sku_serializers.py)

      • #1. SKU serializer
        class SKUSerializer(serializers.ModelSerializer):
        
            ...
        
            #4. Rewrite the create method to save specification information
            def create(self, validated_data):
        
                #1. Create and store sku
                sku = SKU.objects.create(**validated_data)
        
                #2, sku, specification storage
                specs = self.context["request"].data["specs"]
                for spec_dict in specs:
                    SKUSpecification.objects.create(sku_id=sku.id,spec_id=spec_dict["spec_id"],option_id=spec_dict["option_id"])
        
                #3. Return the response
                return sku
        
        

4. Sku table saves data and sets transactions

  • AIM: To ensure that both of them succeed or fail simultaneously while preserving SKU and SKU specifications.

  • Operational procedures:

    • 1. Serializers (good/sku_serializers.py)

      •     # 4. Rewrite the create method to save specification information
            @transaction.atomic
            def create(self, validated_data):
        
                sid = transaction.savepoint() #TODO Sets Save Points
        
                try:
                    
                    #1. Create and store sku
                    sku = SKU.objects.create(**validated_data)
                    
                    #2, sku, specification storage
                    specs = self.context["request"].data["specs"]
                    for spec_dict in specs:
                        SKUSpecification.objects.create(sku_id=sku.id,spec_id=spec_dict["spec_id"],option_id=spec_dict["option_id"])
                except Exception as e:
                    transaction.savepoint_rollback(sid) #TODO rollback
                    raise serializers.ValidationError("sku Adding exceptions")
                else:
                    #3. Return the response
                    transaction.savepoint_commit(sid) #TODO submission
                    return sku
        

5. Sku table update data

  • OBJECTIVE: To rewrite the update method and change the specification information

  • Operational procedures:

    • 1. Serializers (good/sku_serializers.py)

      •     #5. Rewrite the update method and change the specification information
            @transaction.atomic
            def update(self, instance, validated_data):
        
                sid = transaction.savepoint()  # TODO Sets Save Points
        
                try:
                    #1. Change sku basic information (name, price.)
                    SKU.objects.filter(id=instance.id).update(**validated_data)
        
                    #2. Change specification information
                    #Delete previous specifications
                    [spec.delete() for spec in instance.specs.all()]
        
                    #Create new specifications
                    specs = self.context["request"].data["specs"]
                    for spec_dict in specs:
                        SKUSpecification.objects.create(sku_id=instance.id, spec_id=spec_dict["spec_id"],
                                                        option_id=spec_dict["option_id"])
                except Exception as e:
                    transaction.savepoint_rollback(sid)  # TODO rollback
                    raise serializers.ValidationError("sku Modifying exceptions")
                else:
                    #3. Return the response
                    transaction.savepoint_commit(sid)  # TODO submission
                    return SKU.objects.get(id=instance.id)
        

6. SPU table data acquisition

  • Objective: To display the information of spu table on the page.

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • #4. SPU management
        router = DefaultRouter()
        router.register(r'goods',spu_views.SPUViewSet,base_name="goods")
        urlpatterns += router.urls
        
        
    • 2. Class view (good/spu_views.py)

      • #1. SPU management
        class SPUViewSet(ModelViewSet):
            pagination_class = MyPageNumberPagination
            serializer_class = spu_serializers.SPUSerializer
            queryset = SPU.objects.all()
        
    • 3. Serializers (spu_serializers.py)

      • #1,spu serializer
        class SPUSerializer(serializers.ModelSerializer):
        
            #1. Rewrite brand
            brand = serializers.StringRelatedField(read_only=True)
            brand_id = serializers.IntegerField()
        
            #2. Rewriting Classification
            category1 = serializers.StringRelatedField(read_only=True)
            category1_id = serializers.IntegerField()
        
            category2 = serializers.StringRelatedField(read_only=True)
            category2_id = serializers.IntegerField()
        
            category3 = serializers.StringRelatedField(read_only=True)
            category3_id = serializers.IntegerField()
        
            class Meta:
                model = SPU
                fields = "__all__"
        

7. SPU table to get brand information

  • Purpose: Brand information can be obtained when spu is added.

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • url(r'^goods/brands/simple/$',spu_views.SPUBrandSimpleView.as_view())
        
    • 2. Class view (good/spu_views.py)

      • #2,spu,brand acquisition
        class SPUBrandSimpleView(ListAPIView):
            serializer_class = spu_serializers.SPUBrandSimpleSerializer
            queryset = Brand.objects.all()
        
    • 3. Serializers (good/spu_serializers.py)

      • #2,spu,brand serializer
        class SPUBrandSimpleSerializer(serializers.ModelSerializer):
            class Meta:
                model = Brand
                fields = ("id","name")
        
        

8. SPU table to obtain first-level classification information

  • Purpose: First-level categorization can be displayed on spu-added pages.

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • url(r'^goods/channel/categories/$',spu_views.SPUCategoryView.as_view()),
        
    • 2. Class view (good/spu_views.py)

      • #3,spu,category classification acquisition
        class SPUCategoryView(ListAPIView):
            serializer_class = spu_serializers.SPUCategorySerializer
            queryset = GoodsCategory.objects.filter(parent__isnull=True)
        
    • 3. Serializers (good/spu_serializers.py)

      • #3,spu,category serializer
        class SPUCategorySerializer(serializers.ModelSerializer):
            class Meta:
                model = GoodsCategory
                fields = ("id","name")
        

9. SPU table to obtain secondary and tertiary classification

  • Purpose: After selecting the parent, query the corresponding subset data

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • url(r'^goods/channel/categories/(?P<category_id>\d+)/$',spu_views.SPUSubsCategoryView.as_view()),
        
    • 2. Class view (good/spu_views.py)

      • #4,spu, secondary classification, tertiary classification
        class SPUSubsCategoryView(ListAPIView):
            serializer_class = spu_serializers.SPUCategorySerializer
        
            def get_queryset(self):
                category_id = self.kwargs.get("category_id")
                queryset = GoodsCategory.objects.filter(parent_id=category_id).all()
                return queryset
        

10, debug fdfs upload picture

  • Objective: to debug fdfs upload pictures through terminal

    • 1. Add the client file to utils

    • 2. Terminal Debugging

      • In [1]: from fdfs_client.client import Fdfs_client                                                                                                                 
        
        In [2]: #1. Create objects                                                                                                                                                
        
        In [3]: client = Fdfs_client('/Users/heJing/Desktop/classes2/day19/meiduo14/meiduo_mall/meiduo_mall/utils/fdfs/client.conf')                                       
        
        In [4]: #2. Upload pictures                                                                                                                                                
        
        In [5]: client.upload_by_filename("/Users/heJing/Desktop/Lessons 2/day19/3_data/Test picture/haha.jpg")                                                                
        getting connection
        <fdfs_client.connection.Connection object at 0x10f8459e8>
        <fdfs_client.fdfs_protol.Tracker_header object at 0x10f845710>
        Out[5]: 
        {'Group name': 'group1',
         'Remote file_id': 'group1/M00/00/03/rBAMhl11zCyAfqwPAACP4XconFQ208.jpg',
         'Status': 'Upload successed.',
         'Local file name': '/Users/heJing/Desktop/Lessons 2/day19/3_data/Test picture/haha.jpg',
         'Uploaded size': '35.00KB',
         'Storage IP': '172.16.12.134'}
        
        

11,spu table uploads pictures

  • AIM: To upload spu images via fdfs

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • url(r'^goods/images/$',spu_views.SPUImageUploadView.as_view()),
        
    • 2. Class view (good/spu_views.py)

      • #5,spu,image upload
        class SPUImageUploadView(APIView):
        
            def post(self,request):
                #1. Get the parameters
                image = request.FILES.get("image")
        
                #2. Calibration parameters
                if not image:
                    return Response(status=400)
        
                #3. Data warehousing (fdfs)
                #3,1 Upload Pictures
                client = Fdfs_client(settings.BASE_CONFIG)
                result = client.upload_by_buffer(image.read())
        
                #3,2 Judging whether the picture was uploaded successfully
                if result.get("Status") != "Upload successed.":
                    return Response(status=400)
        
                image_url = result.get("Remote file_id")
        
                #4. Return the response
                return Response({
                    "img_url":"%s%s"%(settings.BASE_URL,image_url)
                })
        

12. SPU table data preservation

  • Note: Since the management of the SPU completed by the view set is used, saving the SPU has been implemented.

13. SPU table data update

  • Note: Updating the SPU has been implemented because it uses the management of the SPU completed by the view set.

14,spu table data deletion

  • Note: Since the management of SPUs completed by view sets is used, deletion of SPUs has been implemented.

15, spu specification management

  • Purpose: Views can be written to obtain spu specifications

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • #5. SPU spec management
        router = DefaultRouter()
        router.register(r'goods/specs',spu_specs_views.SPUSpecsViewSet,base_name="specs")
        urlpatterns += router.urls
        
    • 2. Class view (good/spu_specs_views.py)

      • #1. SPU specs management
        class SPUSpecsViewSet(ModelViewSet):
            pagination_class = MyPageNumberPagination
            serializer_class = spu_specs_serializers.SPUSpecsSerializer
            queryset = SPUSpecification.objects.all()
        
    • 3. Serializer (good/spu_specs_serializsers.py)

      • #1,spu specs serializer
        class SPUSpecsSerializer(serializers.ModelSerializer):
        
            #1. Rewrite spu
            spu = serializers.StringRelatedField(read_only=True)
            spu_id = serializers.IntegerField()
        
            class Meta:
                model = SPUSpecification
                fields = "__all__"
        

16. Added or deleted, deleted or amended specification sheet management

  • Note: Since specification management uses view sets, all functions are implemented

17,spu specification, option information acquisition

  • Purpose: Class view can be written to obtain information about specification options.

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • #6. Spec option management
        router = DefaultRouter()
        router.register(r'specs/options',specs_options_views.SpecsOptionViewSet,base_name="options")
        urlpatterns += router.urls
        
    • 2. Class view (good/specs_options_views.py)

      • #1. Spec option management
        class SpecsOptionViewSet(ModelViewSet):
            pagination_class = MyPageNumberPagination
            serializer_class = specs_options_serializers.SpecsOptionSerializer
            queryset = SpecificationOption.objects.all()
        
    • 3. Serializer (good/specs_options_serializsers.py)

      • #1,spec option serializer
        class SpecsOptionSerializer(serializers.ModelSerializer):
        
            #1. Rewrite spec
            spec = serializers.StringRelatedField(read_only=True)
            spec_id = serializers.IntegerField()
        
            class Meta:
                model = SpecificationOption
                fields = "__all__"
        

18. SPU specification information acquisition

  • Purpose: To obtain the corresponding specification information when adding options

  • Operational procedures:

    • 1. Subrouting (meiduo_admin/urls.py)

      • #6,spec option routing
            url(r'^goods/specs/simple/$',specs_options_views.SpecSimpleView.as_view())
        
    • 2. Class view (good/specs_options_views.py)

      • #2. Spec information acquisition
        class SpecSimpleView(ListAPIView):
            serializer_class = specs_options_serializers.SpecSimpleSerializer
            # queryset = SPUSpecification.objects.all()
        
            #1. Rewrite the get_queryset method for easy viewing when displaying data on the front end
            def get_queryset(self):
                #1. Get the data source
                queryset = SPUSpecification.objects.all()
        
                #2. Add spu.name to the data source name
                for spec in queryset:
                    spec.name = "%s - %s"%(spec.spu.name,spec.name)
        
                #3. Return to the data source
                return queryset
        
    • 3. Serializer (good/specs_options_serializsers.py)

      • #2. Spec Specification Information
        class SpecSimpleSerializer(serializers.ModelSerializer):
            class Meta:
                model = SPUSpecification
                fields = ("id","name")
        

19. SPU specification options add, modify, delete

  • Note: Since the specification options are implemented using view sets, add, delete and change them all.

Added by MattMan on Tue, 10 Sep 2019 11:41:05 +0300