Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

63 linhas
1.8 KiB

  1. from api import models
  2. from rest_framework import serializers
  3. from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
  4. class PasswordSerializer(serializers.ModelSerializer):
  5. class Meta:
  6. model = models.Password
  7. fields = (
  8. "id",
  9. "login",
  10. "site",
  11. "lowercase",
  12. "uppercase",
  13. "symbols",
  14. "digits",
  15. "counter",
  16. "length",
  17. "version",
  18. "created",
  19. "modified",
  20. )
  21. read_only_fields = ("created", "modified")
  22. def create(self, validated_data):
  23. user = self.context["request"].user
  24. return models.Password.objects.create(user=user, **validated_data)
  25. def to_internal_value(self, data):
  26. if "number" in data and "digits" not in data:
  27. data["digits"] = data["number"]
  28. if "numbers" in data and "digits" not in data:
  29. data["digits"] = data["numbers"]
  30. if "symbol" in data and "symbols" not in data:
  31. data["symbols"] = data["symbol"]
  32. data = super().to_internal_value(data)
  33. return data
  34. class EncryptedPasswordProfileSerializer(serializers.ModelSerializer):
  35. class Meta:
  36. model = models.EncryptedPasswordProfile
  37. fields = (
  38. "id",
  39. "password_profile",
  40. "created",
  41. "modified",
  42. )
  43. read_only_fields = ("created", "modified")
  44. def create(self, validated_data):
  45. user = self.context["request"].user
  46. return models.EncryptedPasswordProfile.objects.create(
  47. user=user, **validated_data
  48. )
  49. class BackwardCompatibleTokenObtainPairSerializer(TokenObtainPairSerializer):
  50. def validate(self, attrs):
  51. data = super().validate(attrs)
  52. data.update({"token": data["access"]})
  53. return data