def migrate_tanks(self, old_tank_list): tank_list = Tag.objects.filter(tag_type=TagType.TANK) tank_title_map = dict([(t.name, t) for t in tank_list]) new_id = Tag.objects.all().aggregate(Max("id"))["id__max"] or 10000 new_id = max(new_id, 10000) session = sessionmaker(bind=SQL_ENGINE)() for old_tank in old_tank_list: if old_tank.title in tank_title_map: LegacyTank.objects.create(id=old_tank.id, tag=tank_title_map[old_tank.title]) continue manga = session.query(OldManga).filter(OldManga.tank_id == old_tank.id).order_by(OldManga.tank_chp)[:] if len(manga) > 0: cover = manga[0].cover else: cover = None new_id += 1 tag = Tag( id=new_id, tag_type=TagType.TANK, name=old_tank.title, slug=old_tank.slug, created_by_id=self.user.id, created_on=old_tank.date_created, cover=self.get_file(cover), ) tag.save(updated_by=None) tank_title_map[old_tank.title] = tag LegacyTank.objects.create(id=old_tank.id, tag=tag)
def get_or_create_tag_by_name_or_alias(tag_type, name, user): try: return Tag.objects.get(tag_type=tag_type, name=name) except Tag.DoesNotExist: pass try: return TagAlias.objects.select_related('tag').get(tag__tag_type=tag_type, language=get_language(), name=name).tag except TagAlias.DoesNotExist: pass tag = Tag(tag_type=tag_type, name=name) tag.save(updated_by=user) return tag