Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Any
- from rest_framework import serializers
- from django.core.exceptions import ImproperlyConfigured
- class MasterDataCreateUpdateMixinSerializer:
- model_element_class = None
- model_dashboard_class = None
- create = None
- update = None
- def get_model_element_class(self) -> Any:
- """
- Return a name of model_element_class to be used for the request. Must return
- a rase. May not be called if render_to_response() is overridden.
- """
- if (
- self.model_element_class is None
- ):
- raise ImproperlyConfigured(
- "MasterDataCreateUpdateMixinSerializer requires either a definition of "
- "'model_element_class' or an implementation of 'get_model_element_class()'"
- )
- return self.model_element_class
- def get_model_dashboard_class(self) -> Any:
- """
- Return a name of model_dashboard_class to be used for the request. Must return
- a rase. May not be called if render_to_response() is overridden.
- """
- if (
- self.model_dashboard_class is None
- ):
- raise ImproperlyConfigured(
- "MasterDataCreateUpdateMixinSerializer requires either a definition of "
- "'model_dashboard_class' or an implementation of 'get_model_dashboard_class()'"
- )
- return self.model_dashboard_class
- def get_create(self) -> Any:
- """
- Return a name of create to be used for the request. Must return
- a rase. May not be called if render_to_response() is overridden.
- """
- if (
- self.create is None
- ):
- raise ImproperlyConfigured(
- "MasterDataCreateUpdateMixinSerializer requires either a definition of "
- "'create' or an implementation of 'get_create()'"
- )
- return self.create
- def get_update(self) -> Any:
- """
- Return a name of update to be used for the request. Must return
- a rase. May not be called if render_to_response() is overridden.
- """
- if (
- self.update is None
- ):
- raise ImproperlyConfigured(
- "MasterDataCreateUpdateMixinSerializer requires either a definition of "
- "'update' or an implementation of 'get_update()'"
- )
- return self.update
- class GenericCreateUpdateSerializer(
- MasterDataCreateUpdateMixinSerializer,
- serializers.Serializer,
- ):
- """
- The backing class inherits from MasterDataCreateUpdateMixinSerializer
- and from the Django Serializer class.
- # Example:
- class MyClass(GenericCreateUpdateSerializer):
- create = MySerializer(yor_attr, ...)
- update = MySerializer(yor_attr, ...)
- model_dashboard_class = Model
- def create_one(self, data) -> None:
- # you logic
- def update_one(self, data) -> None:
- # you logic
- """
- def __init__(self, *args, **kwargs) -> None:
- super().__init__(*args, **kwargs)
- self.create = self.get_create()
- self.update = self.get_update()
- self.model_element_class = self.get_model_element_class()
- self.model_dashboard_class = self.get_model_dashboard_class()
- self.fields['create'] = serializers.ListField(
- child=self.create,
- required=False,
- allow_empty=False,
- )
- self.fields['update'] = serializers.ListField(
- child=self.create,
- required=False,
- allow_empty=False,
- )
- def save(self, **kwargs):
- validated_create_data = [
- {**attrs, **kwargs} for attrs in self.validated_data.get('create', [])
- ]
- validated_update_data = [
- {**attrs, **kwargs} for attrs in self.validated_data.get('update', [])
- ]
- self.create_status(validated_create_data)
- self.update_status(validated_update_data)
- def create_status(self, validated_data: list):
- try:
- for item in validated_data:
- self.create_one(item)
- except ImproperlyConfigured:
- raise ImproperlyConfigured(
- "GenericCreateUpdateSerializer requires either a definition of "
- "'create_one' or an implementation of create_one()'"
- )
- def update_status(self, validated_data: list):
- try:
- for item in validated_data:
- self.update_one(item)
- except ImproperlyConfigured:
- raise ImproperlyConfigured(
- "GenericCreateUpdateSerializer requires either a definition of "
- "'update_one' or an implementation of update_one()'"
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement