orders_local.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from rest_framework import serializers
  2. from webservice_running.models import OrdersLocal
  3. from tickets.models import *
  4. from metaservicesynced.models import Orders
  5. from SharixAdmin.models import SharixUser
  6. from tickets.serializer import TaskSerializer
  7. # from metaservicesynced.serializer import OrdersSerializer
  8. from rest_framework.utils import model_meta
  9. import pprint
  10. from django.db import connection
  11. class OrderNotDefined(Exception):
  12. """
  13. If order_synced field is not defined throw exception
  14. """
  15. def __init__(self, message="order_synced is not defined"):
  16. self.message = message
  17. super().__init__(self.message)
  18. class OrdersLocalSerializer(serializers.ModelSerializer):
  19. service = serializers.IntegerField(source="order_synced.service.pk", allow_null=True)
  20. service_type = serializers.IntegerField(source="order_synced.service_type.pk", allow_null=True)
  21. state = serializers.CharField(source="order_synced.state", allow_null=True)
  22. id_metaservice = serializers.IntegerField(source="order_synced.id_metaservice", allow_null=True)
  23. provider = serializers.IntegerField(source="order_synced.provider.pk", allow_null=True)
  24. provider_screen_name = serializers.CharField(source="order_synced.provider.user_id.full_name", allow_null=True)
  25. receiver = serializers.IntegerField(source="order_synced.receiver.pk", allow_null=True)
  26. receiver_screen_name = serializers.CharField(source="order_synced.receiver.full_name", allow_null=True)
  27. client_id = serializers.CharField(source="order_synced.client_id.pk", allow_null=True)
  28. client_screen_name = serializers.CharField(source="order_synced.client.user.full_name", allow_null=True)
  29. #time_created = serializers.DateTimeField(source="order_synced.time_created", allow_null=True)
  30. time_placed = serializers.DateTimeField(source="order_synced.time_placed", allow_null=True)
  31. time_start = serializers.DateTimeField(source="order_synced.time_start", allow_null=True)
  32. time_start_real = serializers.DateTimeField(source="order_synced.time_start_real", allow_null=True)
  33. time_start_predicted = serializers.DateTimeField(source="order_synced.time_start_predicted", allow_null=True)
  34. time_finish_predicted = serializers.DateTimeField(source="order_synced.time_finish_predicted", allow_null=True)
  35. time_finish_real = serializers.DateTimeField(source="order_synced.time_finish_real", allow_null=True)
  36. predicted_price = serializers.FloatField(source="order_synced.predicted_price", allow_null=True)
  37. real_price = serializers.FloatField(source="order_synced.real_price", allow_null=True)
  38. is_global = serializers.BooleanField(source="order_synced.is_global", allow_null=True)
  39. is_visible = serializers.BooleanField(source="order_synced.is_visible", allow_null=True)
  40. ticket = serializers.IntegerField(source="order_synced.ticket.pk", allow_null=True)
  41. status = serializers.IntegerField(source="order_synced.ticket.stauts", allow_null=True)
  42. priority = serializers.IntegerField(source="order_synced.ticket.priority", allow_null=True)
  43. title = serializers.CharField(source="order_synced.ticket.title", allow_null=True)
  44. note = serializers.CharField(source="order_synced.ticket.note", allow_null=True)
  45. due_date = serializers.DateField(source="order_synced.ticket.due_date", allow_null=True)
  46. status_changed_date = serializers.DateTimeField(source="order_synced.ticket.status_changed_date", allow_null=True)
  47. # order_synced = OrdersSerializer(read_only=True)
  48. class Meta:
  49. model = OrdersLocal
  50. fields = "__all__"
  51. # fields = ["order", "order_synced"]
  52. # depth = 2
  53. def update(self, instance, validated_data):
  54. """
  55. Normally order_synced is not null, but in testing it is null=True, so it can produce exceptions
  56. """
  57. order_instance = instance.order_synced
  58. if not order_instance:
  59. raise OrderNotDefined()
  60. ticket_instance = instance.order_synced.ticket
  61. for attr, value in validated_data.items():
  62. if attr == "order_synced":
  63. for order_synced_attr, order_synced_value in value.items():
  64. if order_synced_attr == "ticket":
  65. for ticket_attr, ticket_value in order_synced_value.items():
  66. setattr(ticket_instance, ticket_attr, ticket_value)
  67. continue
  68. else:
  69. setattr(order_instance, order_synced_attr, order_synced_value)
  70. else:
  71. setattr(instance, attr, value)
  72. instance.save()
  73. ticket_instance.save()
  74. order_instance.save()
  75. return instance