Advertisement
Mochinov

Untitled

Mar 7th, 2023
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.95 KB | None | 0 0
  1. from typing import Any
  2.  
  3. from rest_framework import serializers
  4. from django.core.exceptions import ImproperlyConfigured
  5.  
  6.  
  7. class MasterDataCreateUpdateMixinSerializer:
  8.  
  9.     model_element_class = None
  10.     model_dashboard_class = None
  11.  
  12.     create = None
  13.     update = None
  14.  
  15.     def get_model_element_class(self) -> Any:
  16.         """
  17.        Return a name of model_element_class to be used for the request. Must return
  18.            a rase. May not be called if render_to_response() is overridden.
  19.        """
  20.         if (
  21.             self.model_element_class is None
  22.         ):
  23.             raise ImproperlyConfigured(
  24.                 "MasterDataCreateUpdateMixinSerializer requires either a definition of "
  25.                 "'model_element_class' or an implementation of 'get_model_element_class()'"
  26.             )
  27.  
  28.         return self.model_element_class
  29.  
  30.     def get_model_dashboard_class(self) -> Any:
  31.         """
  32.        Return a name of model_dashboard_class to be used for the request. Must return
  33.            a rase. May not be called if render_to_response() is overridden.
  34.        """
  35.         if (
  36.             self.model_dashboard_class is None
  37.         ):
  38.             raise ImproperlyConfigured(
  39.                 "MasterDataCreateUpdateMixinSerializer requires either a definition of "
  40.                 "'model_dashboard_class' or an implementation of 'get_model_dashboard_class()'"
  41.             )
  42.  
  43.         return self.model_dashboard_class
  44.  
  45.  
  46.     def get_create(self) -> Any:
  47.         """
  48.        Return a name of create to be used for the request. Must return
  49.            a rase. May not be called if render_to_response() is overridden.
  50.        """
  51.         if (
  52.             self.create is None
  53.         ):
  54.             raise ImproperlyConfigured(
  55.                 "MasterDataCreateUpdateMixinSerializer requires either a definition of "
  56.                 "'create' or an implementation of 'get_create()'"
  57.             )
  58.  
  59.         return self.create
  60.  
  61.  
  62.     def get_update(self) -> Any:
  63.         """
  64.        Return a name of update to be used for the request. Must return
  65.            a rase. May not be called if render_to_response() is overridden.
  66.        """
  67.         if (
  68.             self.update is None
  69.         ):
  70.             raise ImproperlyConfigured(
  71.                 "MasterDataCreateUpdateMixinSerializer requires either a definition of "
  72.                 "'update' or an implementation of 'get_update()'"
  73.             )
  74.  
  75.         return self.update
  76.  
  77.  
  78. class GenericCreateUpdateSerializer(
  79.         MasterDataCreateUpdateMixinSerializer,
  80.         serializers.Serializer,
  81.     ):
  82.     """
  83.    The backing class inherits from MasterDataCreateUpdateMixinSerializer
  84.        and from the Django Serializer class.
  85.        # Example:
  86.            class MyClass(GenericCreateUpdateSerializer):
  87.                create = MySerializer(yor_attr, ...)
  88.                update = MySerializer(yor_attr, ...)
  89.                model_dashboard_class = Model
  90.                
  91.                def create_one(self, data) -> None:
  92.                    # you logic
  93.                
  94.                def update_one(self, data) -> None:
  95.                    # you logic
  96.    """
  97.  
  98.     def __init__(self, *args, **kwargs) -> None:
  99.         super().__init__(*args, **kwargs)
  100.  
  101.         self.create = self.get_create()
  102.         self.update = self.get_update()
  103.  
  104.         self.model_element_class = self.get_model_element_class()
  105.         self.model_dashboard_class = self.get_model_dashboard_class()
  106.  
  107.         self.fields['create'] = serializers.ListField(
  108.             child=self.create,
  109.             required=False,
  110.             allow_empty=False,
  111.         )
  112.         self.fields['update'] = serializers.ListField(
  113.             child=self.create,
  114.             required=False,
  115.             allow_empty=False,
  116.         )
  117.  
  118.     def save(self, **kwargs):
  119.  
  120.         validated_create_data = [
  121.             {**attrs, **kwargs} for attrs in self.validated_data.get('create', [])
  122.         ]
  123.  
  124.         validated_update_data = [
  125.             {**attrs, **kwargs} for attrs in self.validated_data.get('update', [])
  126.         ]
  127.  
  128.         self.create_status(validated_create_data)
  129.         self.update_status(validated_update_data)
  130.  
  131.     def create_status(self, validated_data: list):
  132.  
  133.         try:
  134.             for item in validated_data:
  135.                 self.create_one(item)
  136.  
  137.         except ImproperlyConfigured:
  138.             raise ImproperlyConfigured(
  139.                 "GenericCreateUpdateSerializer requires either a definition of "
  140.                 "'create_one' or an implementation of create_one()'"
  141.             )
  142.  
  143.     def update_status(self, validated_data: list):
  144.  
  145.         try:
  146.             for item in validated_data:
  147.                 self.update_one(item)
  148.  
  149.         except ImproperlyConfigured:
  150.             raise ImproperlyConfigured(
  151.                 "GenericCreateUpdateSerializer requires either a definition of "
  152.                 "'update_one' or an implementation of update_one()'"
  153.             )
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement