6
0

event.py~ 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from django.forms.models import model_to_dict
  2. from rest_framework import serializers
  3. from webservice_running.models import Event
  4. from webservice_running.exceptions import OrderNotDefined
  5. class EventSerializer(serializers.ModelSerializer):
  6. service = serializers.IntegerField(source="order_synced.service.pk", allow_null=True, required=False)
  7. service_type = serializers.IntegerField(source="order_synced.service_type.pk", allow_null=True, required=False)
  8. state = serializers.CharField(source="order_synced.state", allow_null=True, required=False)
  9. id_metaservice = serializers.IntegerField(source="order_synced.id_metaservice", allow_null=True, required=False)
  10. provider = serializers.IntegerField(source="order_synced.provider.pk", allow_null=True, required=False)
  11. provider_screen_name = serializers.CharField(source="order_synced.provider.user_id.full_name", allow_null=True, required=False)
  12. receiver = serializers.IntegerField(source="order_synced.receiver.pk", allow_null=True, required=False)
  13. receiver_screen_name = serializers.CharField(source="order_synced.receiver.full_name", allow_null=True, required=False)
  14. client_id = serializers.CharField(source="order_synced.client_id.pk", allow_null=True, required=False)
  15. client_screen_name = serializers.CharField(source="order_synced.client.user.full_name", allow_null=True, required=False)
  16. #time_created = serializers.DateTimeField(source="order_synced.time_created", allow_null=True)
  17. time_placed = serializers.DateTimeField(source="order_synced.time_placed", allow_null=True, required=False)
  18. time_start = serializers.DateTimeField(source="order_synced.time_start", allow_null=True, required=False)
  19. time_start_real = serializers.DateTimeField(source="order_synced.time_start_real", allow_null=True, required=False)
  20. time_start_predicted = serializers.DateTimeField(source="order_synced.time_start_predicted", allow_null=True, required=False)
  21. time_finish_predicted = serializers.DateTimeField(source="order_synced.time_finish_predicted", allow_null=True, required=False)
  22. time_finish_real = serializers.DateTimeField(source="order_synced.time_finish_real", allow_null=True, required=False)
  23. predicted_price = serializers.FloatField(source="order_synced.predicted_price", allow_null=True, required=False)
  24. real_price = serializers.FloatField(source="order_synced.real_price", allow_null=True, required=False)
  25. is_global = serializers.BooleanField(source="order_synced.is_global", allow_null=True, required=False)
  26. is_visible = serializers.BooleanField(source="order_synced.is_visible", allow_null=True, required=False)
  27. ticket = serializers.IntegerField(source="order_synced.ticket.pk", allow_null=True, required=False)
  28. status = serializers.IntegerField(source="order_synced.ticket.stauts", allow_null=True, required=False)
  29. priority = serializers.IntegerField(source="order_synced.ticket.priority", allow_null=True, required=False)
  30. title = serializers.CharField(source="order_synced.ticket.title", allow_null=True, required=False)
  31. note = serializers.CharField(source="order_synced.ticket.note", allow_null=True, required=False)
  32. due_date = serializers.DateField(source="order_synced.ticket.due_date", allow_null=True, required=False)
  33. status_changed_date = serializers.DateTimeField(source="order_synced.ticket.status_changed_date", allow_null=True, required=False)
  34. # order_synced = OrdersSerializer(read_only=True)
  35. class Meta:
  36. model = Event
  37. fields = "__all__"
  38. # fields = ["order", "order_synced"]
  39. # depth = 2
  40. def update(self, instance, validated_data):
  41. """
  42. Normally order_synced is not null, but in testing it is null=True, so it can produce exceptions
  43. """
  44. order_instance = instance.order_synced
  45. if not order_instance:
  46. raise OrderNotDefined()
  47. ticket_instance = instance.order_synced.ticket
  48. # ОШИБКА ЕСЛИ В validated_data УКАЗАТЬ order_synced
  49. for attr, value in validated_data.items():
  50. if attr == "order_synced":
  51. print("="*100)
  52. #print(model_to_dict(value))
  53. print("="*100)
  54. for order_synced_attr, order_synced_value in value.items():
  55. if order_synced_attr == "ticket":
  56. for ticket_attr, ticket_value in order_synced_value.items():
  57. setattr(ticket_instance, ticket_attr, ticket_value)
  58. continue
  59. else:
  60. setattr(order_instance, order_synced_attr, order_synced_value)
  61. else:
  62. setattr(instance, attr, value)
  63. instance.save()
  64. ticket_instance.save()
  65. order_instance.save()
  66. return instance