def handle(self, *args, **options): args = list(args) self.commit = options.get("commit", False) ixp_from = pdbm.InternetExchange.objects.get(id=options.get("ids")) ixp_to = pdbm.InternetExchange.objects.get(id=options.get("target")) self.log(f"Merging {ixp_from.name} into {ixp_to.name}") ixlans_from = pdbm.IXLan.objects.filter(ix=ixp_from).exclude( status="deleted") for ixlan in ixlans_from: ixlan.ix = ixp_to self.log(f"Moving IXLAN {ixlan.id} to {ixp_to.name}") if self.commit: ixlan.save() self.log("Soft Deleting %s" % ixp_from.name) if self.commit: ixp_from.delete() mail_users_entity_merge( ixp_from.org.admin_usergroup.user_set.all(), ixp_to.org.admin_usergroup.user_set.all(), ixp_from, ixp_to, )
def handle(self, *args, **options): args = list(args) self.commit = options.get("commit", False) ixp_from = pdbm.InternetExchange.objects.get(id=options.get("ids")) ixp_to = pdbm.InternetExchange.objects.get(id=options.get("target")) self.log("Merging %s into %s" % (ixp_from.name, ixp_to.name)) ixlans_from = pdbm.IXLan.objects.filter(ix=ixp_from).exclude( status="deleted") for ixlan in ixlans_from: ixlan.ix = ixp_to self.log("Moving IXLAN %s to %s" % (ixlan.id, ixp_to.name)) if self.commit: ixlan.save() self.log("Soft Deleting %s" % ixp_from.name) if self.commit: ixp_from.delete() mail_users_entity_merge( ixp_from.org.admin_usergroup.user_set.all(), ixp_to.org.admin_usergroup.user_set.all(), ixp_from, ixp_to)
def merge_organizations(targets, target, request): """ Merge organizations specified in targets into organization specified in target Arguments: targets <QuerySet|list> iterable of Organization instances target <Organization> merge organizations with this organization """ if request.user: reversion.set_user(request.user) # preare stats ix_moved = 0 fac_moved = 0 net_moved = 0 user_moved = 0 org_merged = 0 for org in targets: if org == target: raise ValueError(_("Target org cannot be in selected organizations list")) for org in targets: merge = OrganizationMerge.objects.create(from_org=org, to_org=target) source_admins = [] # move entities for ix in org.ix_set.all(): ix.org = target ix.save() merge.log_entity(ix) ix_moved += 1 for net in org.net_set.all(): net.org = target net.save() merge.log_entity(net) net_moved += 1 for fac in org.fac_set.all(): fac.org = target fac.save() merge.log_entity(fac) fac_moved += 1 # move users for user in org.usergroup.user_set.all(): target.usergroup.user_set.add(user) org.usergroup.user_set.remove(user) merge.log_entity(user, note="usergroup") user_moved += 1 for user in org.admin_usergroup.user_set.all(): target.usergroup.user_set.add(user) org.admin_usergroup.user_set.remove(user) merge.log_entity(user, note="admin_usergroup") user_moved += 1 source_admins.append(user) # mark deleted org.delete() org_merged += 1 mail_users_entity_merge( source_admins, target.admin_usergroup.user_set.all(), org, target ) return { "ix": ix_moved, "fac": fac_moved, "net": net_moved, "user": user_moved, "org": org_merged, }
def handle(self, *args, **options): self.commit = options.get("commit", False) self.moved = [] self.target = options.get("target", 0) if not self.target: msg = "Target ID required (--target)" self.log(msg) raise CommandError(msg) self.match = options.get("match", None) self.ids = options.get("ids", "") facs = [] moved = self.moved if self.match: if self.ids: msg = "ids and match are mutually exclusive" self.log(msg) raise CommandError(msg) self.log("Merging all facilities matching '%s'" % self.match) for fac in pdbm.Facility.objects.exclude(status="deleted"): if re.match(self.match, fac.name, re.IGNORECASE): facs.append(fac) elif self.ids: self.ids = self.ids.split(",") self.log("Merging facilities {} -> {}".format( ", ".join(self.ids), self.target)) for fac in pdbm.Facility.objects.filter(id__in=self.ids): facs.append(fac) else: msg = "IDs or match is required" self.log(msg) raise CommandError(msg) self.target = pdbm.Facility.objects.get(id=self.target) if self.target.status == "deleted": self.target.status = "ok" if self.commit: self.target.save() for fac in facs: if fac.id == self.target.id: continue self.log("Merging %s (%d) .." % (fac, fac.id)) for netfac in pdbm.NetworkFacility.objects.filter( facility=fac).exclude(status="deleted"): netfac_other = pdbm.NetworkFacility.objects.filter( facility=self.target, network_id=netfac.network_id) # we check if the target fac already has a netfac to the same network (that is currently undeleted), if it does we skip it if netfac_other.exclude(status="deleted").exists(): self.log( " - netfac %s : connection already exists at target, skipping." % netfac) continue # if it exists but is currently delete, we simply undelete it elif netfac_other.exists(): netfac_other = netfac_other.first() netfac_other.local_asn = netfac.local_asn netfac_other.avail_sonet = netfac.avail_sonet netfac_other.avail_ethernet = netfac.avail_ethernet netfac_other.avail_atm = netfac.avail_atm netfac_other.status = "ok" self.log(" - netfac %s (undeleting and updating)" % netfac_other) moved.append(netfac_other) if self.commit: netfac_other.save() # if it doesnt exist, we update the facility to the target facility and save else: self.log(" - netfac %s" % netfac) netfac.facility = self.target moved.append(netfac) if self.commit: netfac.save() for ixfac in pdbm.InternetExchangeFacility.objects.filter( facility=fac).exclude(status="deleted"): ixfac_other = pdbm.InternetExchangeFacility.objects.filter( facility=self.target, ix=ixfac.ix) # we check if the target fac already has an ixfac to the same exchange (that is currently undeleted), if it does, we skip it if ixfac_other.exclude(status="deleted").exists(): self.log( " - ixfac %s : connection already exists at target, skipping." % ixfac) continue # if it exists but is currently deleted, we undelete and copy elif ixfac_other.exists(): ixfac_other = ixfac_other.first() ixfac_other.status = "ok" moved.append(ixfac_other) self.log(" - ixfac %s (undeleting and updating)" % ixfac_other) if self.commit: ixfac_other.save() # if it doesnt exist, we update the facility to the target facility and save else: self.log(" - ixfac %s" % ixfac) ixfac.facility = self.target moved.append(ixfac) if self.commit: ixfac.save() soft_delete(fac, self) if self.commit: mail_users_entity_merge( fac.org.admin_usergroup.user_set.all(), self.target.org.admin_usergroup.user_set.all(), fac, self.target, )
def handle(self, *args, **options): self.commit = options.get("commit", False) self.moved = [] self.target = options.get("target", 0) if not self.target: msg = "Target ID required (--target)" self.log(msg) raise CommandError(msg) self.match = options.get("match", None) self.ids = options.get("ids", "") facs = [] moved = self.moved if self.match: if self.ids: msg = 'ids and match are mutually exclusive' self.log(msg) raise CommandError(msg) self.log("Merging all facilities matching '%s'" % self.match) for fac in pdbm.Facility.objects.exclude(status="deleted"): if re.match(self.match, fac.name, re.IGNORECASE): facs.append(fac) elif self.ids: self.ids = self.ids.split(",") self.log("Merging facilities %s -> %s" % (", ".join(self.ids), self.target)) for fac in pdbm.Facility.objects.filter(id__in=self.ids): facs.append(fac) else: msg = 'IDs or match is required' self.log(msg) raise CommandError(msg) self.target = pdbm.Facility.objects.get(id=self.target) if self.target.status == "deleted": self.target.status = "ok" if self.commit: self.target.save() for fac in facs: if fac.id == self.target.id: continue self.log("Merging %s (%d) .." % (fac, fac.id)) for netfac in pdbm.NetworkFacility.objects.filter( facility=fac).exclude(status="deleted"): netfac_other = pdbm.NetworkFacility.objects.filter( facility=self.target, network_id=netfac.network_id) # we check if the target fac already has a netfac to the same network (that is currently undeleted), if it does we skip it if netfac_other.exclude(status="deleted").exists(): self.log( " - netfac %s : connection already exists at target, skipping." % netfac) continue # if it exists but is currently delete, we simply undelete it elif netfac_other.exists(): netfac_other = netfac_other.first() netfac_other.local_asn = netfac.local_asn netfac_other.avail_sonet = netfac.avail_sonet netfac_other.avail_ethernet = netfac.avail_ethernet netfac_other.avail_atm = netfac.avail_atm netfac_other.status = "ok" self.log(" - netfac %s (undeleting and updating)" % netfac_other) moved.append(netfac_other) if self.commit: netfac_other.save() # if it doesnt exist, we update the facility to the target facility and save else: self.log(" - netfac %s" % netfac) netfac.facility = self.target moved.append(netfac) if self.commit: netfac.save() for ixfac in pdbm.InternetExchangeFacility.objects.filter( facility=fac).exclude(status="deleted"): ixfac_other = pdbm.InternetExchangeFacility.objects.filter( facility=self.target, ix=ixfac.ix) # we check if the target fac already has an ixfac to the same exchange (that is currently undeleted), if it does, we skip it if ixfac_other.exclude(status="deleted").exists(): self.log( " - ixfac %s : connection already exists at target, skipping." % ixfac) continue # if it exists but is currently deleted, we undelete and copy elif ixfac_other.exists(): ixfac_other = ixfac_other.first() ixfac_other.status = "ok" moved.append(ixfac_other) self.log( " - ixfac %s (undeleting and updating)" % ixfac_other) if self.commit: ixfac_other.save() # if it doesnt exist, we update the facility to the target facility and save else: self.log(" - ixfac %s" % ixfac) ixfac.facility = self.target moved.append(ixfac) if self.commit: ixfac.save() soft_delete(fac, self) if self.commit: mail_users_entity_merge( fac.org.admin_usergroup.user_set.all(), self.target.org.admin_usergroup.user_set.all(), fac, self.target)