2023-09-11 06:38:07 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2024-06-26 11:26:03 +00:00
|
|
|
from common.const import Language
|
2023-09-11 06:38:07 +00:00
|
|
|
from common.serializers.fields import EncryptedField
|
2023-09-19 08:30:48 +00:00
|
|
|
from ...models import Preference
|
2023-09-11 06:38:07 +00:00
|
|
|
|
|
|
|
|
2024-06-26 11:26:03 +00:00
|
|
|
class FileEncryptSerializer(serializers.Serializer):
|
2024-05-11 03:17:02 +00:00
|
|
|
has_secret_key = serializers.SerializerMethodField(
|
|
|
|
help_text=_(
|
2024-05-27 03:13:13 +00:00
|
|
|
'*! The password for file encryption, '
|
2024-05-11 03:17:02 +00:00
|
|
|
'used for decryption when the system sends emails containing file attachments. '
|
|
|
|
'<br>'
|
|
|
|
'Such as: account backup files, account password change results files'
|
2024-06-26 11:26:03 +00:00
|
|
|
),
|
2024-05-11 03:17:02 +00:00
|
|
|
)
|
2023-09-11 06:38:07 +00:00
|
|
|
secret_key = EncryptedField(
|
|
|
|
required=False, max_length=1024,
|
2023-09-11 07:26:50 +00:00
|
|
|
write_only=True, allow_blank=True,
|
2024-05-11 03:17:02 +00:00
|
|
|
label=_('New password'),
|
2023-09-11 06:38:07 +00:00
|
|
|
)
|
|
|
|
secret_key_again = EncryptedField(
|
|
|
|
required=False, max_length=1024,
|
2023-09-11 07:26:50 +00:00
|
|
|
write_only=True, allow_blank=True,
|
2024-05-11 03:17:02 +00:00
|
|
|
label=_('Confirm password')
|
2023-09-11 06:38:07 +00:00
|
|
|
)
|
|
|
|
|
2023-09-19 08:30:48 +00:00
|
|
|
def get_has_secret_key(self, obj):
|
|
|
|
user = self.context['request'].user
|
|
|
|
query = {'user': user, 'name': 'secret_key', 'category': 'lina'}
|
|
|
|
return Preference.objects.filter(**query).exists()
|
|
|
|
|
2023-09-11 06:38:07 +00:00
|
|
|
def validate(self, attrs):
|
|
|
|
secret_key = attrs.pop('secret_key', None)
|
|
|
|
secret_key_again = attrs.pop('secret_key_again', None)
|
|
|
|
|
|
|
|
if (secret_key or secret_key_again) and secret_key != secret_key_again:
|
|
|
|
msg = _('The newly set password is inconsistent')
|
|
|
|
raise serializers.ValidationError({'secret_key_again': msg})
|
|
|
|
elif secret_key and secret_key_again:
|
|
|
|
attrs['secret_key'] = secret_key
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
2024-06-26 11:26:03 +00:00
|
|
|
class BasicSerializer(serializers.Serializer):
|
2024-06-28 08:36:34 +00:00
|
|
|
lang = serializers.ChoiceField(required=False, choices=Language.choices, label=_('Language'), default=Language.en)
|
2024-06-26 11:26:03 +00:00
|
|
|
|
|
|
|
|
2023-09-11 06:38:07 +00:00
|
|
|
class LinaSerializer(serializers.Serializer):
|
2024-06-26 11:26:03 +00:00
|
|
|
# basic = BasicSerializer(required=False, label=_('Basic'))
|
|
|
|
file = FileEncryptSerializer(required=False, label=_('File Encryption'))
|