1
0

orders.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from rest_framework import serializers
  2. from dbsynce.models import Orders, Client
  3. from dbsynce.ejabber import *
  4. from tickets.models import Task, TaskList
  5. from core.config import DEBUG, WEBSERVICE_NAME, EJ_HOST, EJ_SERVICE
  6. import traceback
  7. import requests
  8. class OrdersSerializer(serializers.ModelSerializer):
  9. class Meta:
  10. model = Orders
  11. fields = '__all__'
  12. def create(self, validated_data):
  13. ModelClass = self.Meta.model
  14. if "ticket" not in validated_data:
  15. # Create ticket with minimal required data
  16. task_data = {
  17. "task_list": TaskList.objects.get(pk=9), # MS-SUP входящие обычные заявки ST_REQUEST
  18. "status": None,
  19. "created_date": "2000-01-01", # mandatory
  20. "status_changed_date": None,
  21. "created_by": None,
  22. "priority": 0, # default: 0
  23. "type": None,
  24. "title": "New ticket title", # mandatory
  25. "note": None,
  26. "due_date": None,
  27. "assigned_to": None,
  28. }
  29. ticket_instance = Task._default_manager.create(**task_data)
  30. validated_data["ticket"] = ticket_instance
  31. try:
  32. instance = ModelClass._default_manager.create(**validated_data)
  33. except TypeError:
  34. tb = traceback.format_exc()
  35. msg = (
  36. 'Got a `TypeError` when calling `%s.%s.create()`. '
  37. 'This may be because you have a writable field on the '
  38. 'serializer class that is not a valid argument to '
  39. '`%s.%s.create()`. You may need to make the field '
  40. 'read-only, or override the %s.create() method to handle '
  41. 'this correctly.\nOriginal exception was:\n %s' %
  42. (
  43. ModelClass.__name__,
  44. ModelClass._default_manager.name,
  45. ModelClass.__name__,
  46. ModelClass._default_manager.name,
  47. self.__class__.__name__,
  48. tb
  49. )
  50. )
  51. raise TypeError(msg)
  52. else:
  53. if DEBUG: print("[CREATING EJABBER ROOMS]")
  54. # Get neccessary data
  55. order_num = instance.pk
  56. support = pick_support()
  57. client = instance.client_id.user.phone_number
  58. provider = instance.provider.user_id.phone_number
  59. for room_name, room_members in get_rooms(WEBSERVICE_NAME, order_num, support, client, provider).items():
  60. # Create ejabber rooms
  61. data = {
  62. "name": room_name,
  63. "service": EJ_SERVICE,
  64. "host": EJ_HOST,
  65. }
  66. res = ej_execute("create_room", data)
  67. if DEBUG: print(f"[RESPONSE CREATED]: {res.json()}")
  68. if DEBUG: print(f"[ROOM]: {room_name}")
  69. # Set rooms to members_only
  70. data = {
  71. "name": room_name,
  72. "service": EJ_SERVICE,
  73. "option": "members_only",
  74. "value": "true"
  75. }
  76. res = ej_execute("change_room_option", data)
  77. if DEBUG: print(f"[SET ROOM TO MEMBERS ONLY]: {res.json()}")
  78. """
  79. Add members to the room
  80. member_account is a dict that contains members credentials
  81. Key - the same thing that is in room_members
  82. Value - tuple (jid, ejabber affiliation).
  83. Affiliation can be one of: owner, admin, member, outcast, none.
  84. owner > admin
  85. """
  86. members_accounts = {
  87. "owner": (WEBSERVICE_NAME + "_backend@" + EJ_HOST, "owner"),
  88. "client": (client, "member"),
  89. "support": (support, "member"),
  90. "provider": (provider, "member"),
  91. }
  92. data = {
  93. "name": room_name,
  94. "service": EJ_SERVICE,
  95. "jid": WEBSERVICE_NAME + "_backend@" + EJ_HOST,
  96. "affiliation": "owner"
  97. }
  98. # Add all neccessary member to the room
  99. for member in room_members:
  100. data["jid"] = members_accounts[member][0]
  101. data["affiliation"] = members_accounts[member][1]
  102. res = ej_execute("set_room_affiliation", data)
  103. if DEBUG: print(f"[ADDED USER]: {res.json()}")
  104. return instance