1
0

orders_local.py 5.5 KB

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