Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""This file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
"""
import os
 
from io_storages.redis.models import RedisExportStorage, RedisImportStorage
from io_storages.serializers import ExportStorageSerializer, ImportStorageSerializer
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
 
 
class RedisImportStorageSerializer(ImportStorageSerializer):
    type = serializers.ReadOnlyField(default=os.path.basename(os.path.dirname(__file__)))
 
    class Meta:
        model = RedisImportStorage
        fields = '__all__'
 
    def to_representation(self, instance):
        result = super().to_representation(instance)
        result.pop('password')
        return result
 
    def validate(self, data):
        data = super(RedisImportStorageSerializer, self).validate(data)
 
        storage = RedisImportStorage(**data)
        try:
            storage.validate_connection()
        except:  # noqa: E722
            raise ValidationError("Can't connect to Redis server.")
        return data
 
 
class RedisExportStorageSerializer(ExportStorageSerializer):
    type = serializers.ReadOnlyField(default=os.path.basename(os.path.dirname(__file__)))
 
    def to_representation(self, instance):
        result = super().to_representation(instance)
        result.pop('password')
        return result
 
    class Meta:
        model = RedisExportStorage
        fields = '__all__'