You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serializers.py 1.4 KiB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. "numbers",
  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. class EncryptedPasswordProfileSerializer(serializers.ModelSerializer):
  26. class Meta:
  27. model = models.EncryptedPasswordProfile
  28. fields = (
  29. "id",
  30. "password_profile",
  31. "created",
  32. "modified",
  33. )
  34. read_only_fields = ("created", "modified")
  35. def create(self, validated_data):
  36. user = self.context["request"].user
  37. return models.EncryptedPasswordProfile.objects.create(
  38. user=user, **validated_data
  39. )
  40. class BackwardCompatibleTokenObtainPairSerializer(TokenObtainPairSerializer):
  41. def validate(self, attrs):
  42. data = super().validate(attrs)
  43. data.update({"token": data["access"]})
  44. return data