def update(self, instance, validated_data): """Update domain and create/update transport.""" transport_def = validated_data.pop("transport", None) resources = validated_data.pop("domainobjectlimit_set", None) super().update(instance, validated_data) if transport_def and instance.type == "relaydomain": transport = getattr(instance, "transport", None) created = False if transport: for key, value in transport_def.items(): setattr(instance, key, value) transport._settings = transport_def["_settings"] else: transport = transport_models.Transport(**transport_def) created = True transport.pattern = instance.name transport.save() if created: instance.transport = transport instance.save() if resources: for resource in resources: instance.domainobjectlimit_set.filter( name=resource["name"]).update( max_value=resource["max_value"]) return instance
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 import_relaydomain(user, row, formopts): """Specific code for relay domains import""" if len(row) != 7: raise BadRequest(_("Invalid line")) domain = admin_models.Domain(name=row[1].strip(), type="relaydomain", quota=0, enabled=(row[5].strip().lower() in ["true", "1", "yes", "y"])) domain.save(creator=user) settings = { "relay_target_host": row[2].strip(), "relay_target_port": row[3].strip(), "relay_verify_recipients": (row[6].strip().lower() in ["true", "1", "yes", "y"]) } transport = tr_models.Transport(pattern=domain.name, service="relay", _settings=settings) tr_backends.manager.get_backend("relay").serialize(transport) transport.save()
def create(self, validated_data): """Create administrator and other stuff if needed.""" domain_admin = validated_data.pop("domain_admin", None) transport_def = validated_data.pop("transport", None) domain = super().create(validated_data) if transport_def: transport = transport_models.Transport(**transport_def) transport.pattern = domain.name transport.save() domain.transport = transport domain.save() if not domain_admin: return domain # 1. Create a domain administrator username = "******" % (domain_admin["username"], domain.name) try: da = core_models.User.objects.get(username=username) except core_models.User.DoesNotExist: pass else: raise lib_exceptions.Conflict( _("User '%s' already exists") % username) user = self.context["request"].user core_signals.can_create_object.send(self.__class__, context=user, klass=models.Mailbox) da = core_models.User(username=username, email=username, is_active=True) password = domain_admin.get("password") if password is None: password = param_tools.get_global_parameter("default_password", app="core") da.set_password(password) da.save() da.role = "DomainAdmins" da.post_create(user) # 2. Create mailbox if needed if domain_admin["with_mailbox"]: dom_admin_username = domain_admin["username"] mb = models.Mailbox(address=dom_admin_username, domain=domain, user=da, use_domain_quota=True) mb.set_quota(override_rules=user.has_perm("admin.change_domain")) mb.save(creator=user) # 3. Create aliases if needed condition = (domain.type == "domain" and domain_admin["with_aliases"] and dom_admin_username != "postmaster") if condition: core_signals.can_create_object.send(self.__class__, context=user, klass=models.Alias) address = u"postmaster@{}".format(domain.name) alias = models.Alias.objects.create(address=address, domain=domain, enabled=True) alias.set_recipients([mb.full_address]) alias.post_create(user) domain.add_admin(da) return domain