Server IP : 85.214.239.14 / Your IP : 13.58.105.80 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/lib/python3.5/site-packages/modoboa/transport/ |
Upload File : |
"""Transport backend.""" from django import forms from django.core import validators from django.utils.translation import ugettext_lazy as _ from modoboa.lib import validators as lib_validators class TransportBackend(object): """Base backend class.""" name = None settings = () def _validate_host_address(self, value): """Validate an host address syntax.""" validator_list = [ lib_validators.validate_hostname, validators.validate_ipv46_address ] for validator in validator_list: try: validator(value) except forms.ValidationError: pass else: return True return False def clean_fields(self, values): """Check that values are correct.""" errors = [] for setting in self.settings: fname = "{}_{}".format(self.name, setting["name"]) value = values.get(fname) if not value: if not setting.get("required", True): continue errors.append((fname, _("This field is required"))) continue ftype = setting.get("type", "string") if ftype == "string": validator = setting.get("validator") vfunc = "_validate_{}".format(validator) if validator and hasattr(self, vfunc): if getattr(self, vfunc)(value): continue else: continue elif ftype == "int" and isinstance(value, int): continue elif ftype == "boolean" and isinstance(value, bool): continue errors.append((fname, _("Invalid value"))) return errors def serialize(self, transport): """Transform received values if needed.""" pass class TransportBackendManager(object): """Transport backends manager.""" def __init__(self): """Constructor.""" self.backends = {} def get_backend(self, name, **kwargs): """Return backend instance.""" backend_class = self.backends.get(name) if backend_class is None: return None return backend_class(**kwargs) def get_backend_list(self): """Return known backend list.""" return sorted( ( (key, key) for key, backend in self.backends.items() ), key=lambda i: i[1] ) def get_backend_settings(self, name): """Return backend settings.""" backend_class = self.backends.get(name) if backend_class is None: return None return backend_class.settings def get_all_backend_settings(self): """Return all backend settings.""" return { name: backend_class.settings for name, backend_class in list(self.backends.items()) } def register_backend(self, backend_class): """Register a new backend.""" if not backend_class.name: raise RuntimeError("missing backend name") self.backends[backend_class.name] = backend_class manager = TransportBackendManager()