Advertisement
Mochinov

Untitled

Mar 7th, 2023
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1.  
  2. class MasterDataDelistingCreateUpdateSerializer(GenericCreateUpdateSerializer):
  3.  
  4.     create = MasterDataDelistingCreateSerializer()
  5.     update = MasterDataDelistingUpdateSerializer()
  6.  
  7.     model_dashboard_class = MasterDataDelisting
  8.  
  9.  
  10.     def create_one(self, data: dict) -> None:
  11.  
  12.         master_data_element_id = data.get('master_data_element_id')
  13.         master_data_element = MasterDataElement.objects.filter(id=master_data_element_id)
  14.  
  15.         if master_data_element.exists():
  16.             raise
  17.  
  18.         master_data_element = master_data_element.first()
  19.         master_data_delisting = self.model_dashboard_class.objects.create(
  20.             date=data.get('date'),
  21.             master_data_element=master_data_element,
  22.             hierarchy_id=master_data_element.level.hierarchy_id,
  23.             is_archived=False,
  24.         )
  25.         master_data_element.switch_status(
  26.             manual_status_id=MasterDataElementStatus.Options.DELISTING.value,
  27.             calculated_status_id=MasterDataElementStatus.Options.DELISTING.value,
  28.         )
  29.  
  30.         return master_data_delisting
  31.  
  32.     def update_one(self, data: dict) -> None:
  33.  
  34.         instance = self.model_dashboard_class.objects.filter(
  35.             id=data.get('id'),
  36.         ).first()
  37.         date = data.get('date')
  38.         is_archived = data.get('is_archived')
  39.         status_id = data.get('status_id')
  40.  
  41.         if date:
  42.             instance.date = date
  43.             instance.save()
  44.  
  45.         if is_archived is not None:
  46.             instance.is_archived = not instance.is_archived
  47.             instance.save()
  48.  
  49.         master_data_element = instance.master_data_element
  50.  
  51.         if status_id:
  52.             master_data_element.switch_status(
  53.                 calculated_status_id=status_id,
  54.                 manual_status_id=status_id,
  55.             )
  56.  
  57.         return instance
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement