Advertisement
maskon

Untitled

May 17th, 2022 (edited)
593
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###################   my_server/r_server/serializer.py
  2. from rest_framework import serializers
  3. from .models import *
  4.  
  5.  
  6. class NetAdapterSerializer(serializers.ModelSerializer):
  7.     class Meta:
  8.         model = NetAdapter
  9.         exclude = ['client']
  10.  
  11.  
  12. class DisksSerializer(serializers.ModelSerializer):
  13.     class Meta:
  14.         model = Disks
  15.         exclude = ['client']
  16.  
  17.  
  18. class ClientSerializer(serializers.ModelSerializer):
  19.     disk = DisksSerializer(many=True)
  20.     adapter = NetAdapterSerializer(many=True)
  21.  
  22.     def create(self, validated_data):
  23.         disks_data = validated_data.pop('disk')
  24.         net_adapter_data = validated_data.pop('adapter')
  25.         client = Client.objects.create(**validated_data)
  26.  
  27.         for disk_data in disks_data:
  28.             Disks.objects.create(client=client, **disk_data)
  29.  
  30.         for adapter_data in net_adapter_data:
  31.             NetAdapter.objects.create(client=client, **adapter_data)
  32.  
  33.         return client
  34.  
  35.     def update(self, instance, validated_data): # <---- It's my problem... :(
  36.         disks_data = validated_data.pop('disk')
  37.         net_adapter_data = validated_data.pop('adapter')
  38.         client = Client.objects.update(**validated_data)
  39.  
  40.         for disk_data in disks_data:
  41.             Disks.objects.update(client=instance, **disk_data)
  42.  
  43.         for adapter_data in net_adapter_data:
  44.             NetAdapter.objects.update(client=instance, **adapter_data)
  45.  
  46.         return client
  47.  
  48.     class Meta:
  49.         model = Client
  50.         fields = '__all__'
  51.        
  52.        
  53. ###################   my_server/r_server/urls.py
  54. from django.urls import path
  55. from .views import ClientDetailView, ClientViewSet, ClientAddView
  56.  
  57. urlpatterns = [
  58.     path('clients/', ClientViewSet.as_view()),
  59.     path('clients/<int:pk>', ClientDetailView.as_view()),
  60.     path('clients/add', ClientAddView.as_view()),
  61. ]
  62.  
  63.  
  64. ###################   rebrian_server/r_server/views.py
  65. from rest_framework import generics
  66. from .serializer import ClientSerializer
  67. from .models import Client
  68.  
  69.  
  70. class ClientViewSet(generics.ListAPIView):
  71.  
  72.     queryset = Client.objects.all()
  73.     serializer_class = ClientSerializer
  74.  
  75.  
  76. class ClientAddView(generics.CreateAPIView):
  77.  
  78.     queryset = Client.objects.all()
  79.     serializer_class = ClientSerializer
  80.  
  81.  
  82. class ClientDetailView(generics.RetrieveUpdateDestroyAPIView):
  83.  
  84.     queryset = Client.objects.all()
  85.     serializer_class = ClientSerializer
  86.  
  87.  
  88.  
  89. ################################## my_server/r_server/models.py
  90. from django.db import models
  91.  
  92.  
  93. class Client(models.Model):
  94.     name = models.CharField('name', max_length=255)
  95.     description = models.TextField('Description', max_length=255, default='no_description')
  96.     memory_total = models.CharField('Memory total', max_length=20)
  97.     cpu_cores = models.IntegerField()
  98.     cpu_physical_cores = models.IntegerField()
  99.     cpu_frequency_current = models.IntegerField()
  100.     cpu_frequency_max = models.IntegerField()
  101.     date_add = models.DateTimeField(auto_now_add=True)
  102.     date_update = models.DateTimeField(auto_now=True)
  103.     client_is_active = models.BooleanField(default=True, verbose_name='Active')
  104.  
  105.     class Meta:
  106.         managed = True
  107.         verbose_name = 'Client'
  108.  
  109.     def __str__(self):
  110.         return '%s: %s' % (self.name, self.description)
  111.  
  112.  
  113. class Disks(models.Model):
  114.     client = models.ForeignKey('Client', related_name='disk', verbose_name='Disk', on_delete=models.CASCADE)
  115.     device = models.CharField('Device', max_length=10)
  116.     total = models.IntegerField('Total volume')
  117.     used = models.IntegerField('Used space')
  118.     free = models.IntegerField('Free space')
  119.     fstype = models.CharField('FS type', max_length=15)
  120.     mountpoint = models.CharField('Mount point', max_length=5)
  121.  
  122.     class Meta:
  123.         managed = True
  124.         verbose_name = 'Disk'
  125.  
  126.     def __str__(self):
  127.         return '%s: %s' % (self.device, self.total)
  128.  
  129.  
  130. class NetAdapter(models.Model):
  131.     client = models.ForeignKey('Client', related_name='adapter', verbose_name='NetAdapter',
  132.                                on_delete=models.CASCADE)
  133.     net_adapter_device = models.CharField('NetAdapter Name', max_length=200)
  134.     net_adapter_status = models.BooleanField(default=False)
  135.     net_adapter_mtu = models.IntegerField()
  136.     net_adapter_speed = models.IntegerField()
  137.     net_adapter_duplex = models.CharField(default='Unknown', max_length=200)
  138.     IPv4 = models.GenericIPAddressField('IPv4', protocol='IPv4', default='0.0.0.0')
  139.     MAC = models.CharField(default='00-00-00-00-00-00', max_length=17)
  140.  
  141.     class Meta:
  142.         managed = True
  143.         verbose_name = 'NetAdapter'
  144.  
  145.     def __str__(self):
  146.         return '%s: %s' % (self.net_adapter_device, self.IPv4)
  147.  
  148.  
  149.  
Advertisement
RAW Paste Data Copied
Advertisement