def migrate_mailbox_aliases(domain, options): print "\tMigrating mailbox aliases" old_aliases = pf_models.Alias.objects.using( options._from).filter(domain=domain.name) for old_al in old_aliases: if old_al.address == old_al.goto: continue new_al = md_models.Alias() local_part, tmp = split_mailbox(old_al.address) if local_part is None or not len(local_part): if tmp is None or not len(tmp): print """Warning: skipping alias %s (cannot retrieve local part). You will need to recreate it manually. """ % old_al.address continue new_al.address = "*" else: new_al.address = local_part new_al.domain = domain new_al.enabled = old_al.active extmboxes = [] intmboxes = [] for goto in old_al.goto.split(","): try: mb = md_models.Mailbox.objects.using( options.to).get(user__username=goto) except md_models.Mailbox.DoesNotExist: extmboxes += [goto] else: intmboxes += [mb] new_al.dates = migrate_dates(old_al) new_al.save(intmboxes, extmboxes, using=options.to)
def create(self, validated_data): """Create appropriate objects.""" creator = self.context["request"].user recipients = validated_data.pop("recipients", None) alias = admin_models.Alias(domain=self.domain, **validated_data) alias.save(creator=creator) alias.set_recipients(recipients) return alias
def create(self, validated_data): """Create appropriate objects.""" creator = self.context["request"].user try: core_signals.can_create_object.send(sender=self.__class__, context=creator, klass=admin_models.Alias) core_signals.can_create_object.send(sender=self.__class__, context=self.domain, object_type="mailbox_aliases") except lib_exceptions.ModoboaException as inst: raise serializers.ValidationError(force_text(inst)) recipients = validated_data.pop("recipients", None) alias = admin_models.Alias(domain=self.domain, **validated_data) alias.save(creator=creator) alias.set_recipients(recipients) return alias
def _migrate_mailbox_aliases(self, domain, options, creator): """Migrate mailbox aliases of a single domain.""" print "\tMigrating mailbox aliases" old_aliases = pf_models.Alias.objects \ .using(options["_from"]).filter(domain=domain.name) for old_al in old_aliases: if old_al.address == old_al.goto: continue new_al = admin_models.Alias() local_part, domname = split_mailbox(old_al.address) if not local_part: if not domname: print( "Warning: skipping alias %s (cannot retrieve local " "part). You will need to recreate it manually." % old_al.address) continue new_al.address = "@{}".format(domname) else: new_al.address = old_al.address new_al.domain = domain new_al.enabled = old_al.active self._migrate_dates(new_al, old_al) new_al.save(creator=creator, using=options["_to"]) to_create = [] for goto in old_al.goto.split(","): alr = admin_models.AliasRecipient(address=goto, alias=new_al) try: mb = admin_models.Mailbox.objects \ .using(options["_to"]).get(user__username=goto) except admin_models.Mailbox.DoesNotExist: pass else: alr.r_mailbox = mb to_create.append(alr) admin_models.AliasRecipient.objects.using( options["_to"]).bulk_create(to_create)