orders_local.py 4.7 KB

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