6
0

event.py 4.1 KB

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