sportsman.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from rest_framework import serializers
  2. from webservice_running.models import Sportsman
  3. from metaservicesynced.models import Client
  4. from SharixAdmin.models import SharixUser
  5. from webservice_running.exceptions import ClientNotDefined
  6. from rest_framework.response import Response
  7. class SportsmanSerializer(serializers.ModelSerializer):
  8. class Meta:
  9. model = Sportsman
  10. exclude = ["blocked_sportsman",
  11. "browse_sportsman",
  12. "saved_sportsman",
  13. "sport_preferrence",
  14. "achievement",
  15. "saved_event",
  16. "participant",
  17. "browse_event",]
  18. # User data
  19. phone_number = serializers.CharField(source="client.user.phone_number")
  20. first_name = serializers.CharField(source="client.user.first_name")
  21. last_name = serializers.CharField(source="client.user.last_name")
  22. email = serializers.CharField(source="client.user.email")
  23. # Client data
  24. requirements = serializers.CharField(source="client.user.requirements")
  25. status = serializers.CharField(source="client.user.status")
  26. ticket_status = serializers.IntegerField(source="client.ticket_status.pk")
  27. id_metaservice = serializers.IntegerField(source="client.id_metaservice")
  28. is_global = serializers.BooleanField(source="client.is_global")
  29. is_visible = serializers.BooleanField(source="client.is_visible")
  30. # def create(self, validated_data):
  31. # print("="*30)
  32. # print(validated_data['client'].status)
  33. # # # Temporary solution
  34. # # client_fields = ("requirements", "status", "ticket_status", "id_metaservice", "is_global", "is_visible")
  35. # # for attr, value in validated_data.items():
  36. # # # If "client" in req, then User model is accessed
  37. # # if attr == "client":
  38. # # for u_attr, u_value in value.get('user').items():
  39. # # setattr(user_instance, u_attr, u_value)
  40. # # else:
  41. # # setattr(instance, attr, value)
  42. # # for i in validated_data.items():
  43. # # pass
  44. # # user_instance = SharixUser._default_manager.create(**validated_data)
  45. # # client_instance = Client._default_manager.create(**validated_data)
  46. # # instance = Sportsman._default_manager.create(**validated_data)
  47. # # return instance
  48. def update(self, instance, validated_data):
  49. client_instance = instance.client
  50. if not client_instance:
  51. raise ClientNotDefined()
  52. user_instance = instance.client.user
  53. for attr, value in validated_data.items():
  54. # If "client" in req, then User model is accessed
  55. if attr == "client":
  56. for u_attr, u_value in value.get('user').items():
  57. setattr(user_instance, u_attr, u_value)
  58. else:
  59. setattr(instance, attr, value)
  60. instance.save()
  61. user_instance.save()
  62. return instance
  63. '''
  64. {
  65. 'client': {
  66. 'user': {
  67. 'phone_number': '99999999999',
  68. 'email': 'email'
  69. }
  70. },
  71. 'bio': 'bio test'}
  72. '''