From 5ff68c6ac1077fcbe969fcc0938c311f49c9d987 Mon Sep 17 00:00:00 2001 From: Library Admin Date: Tue, 10 Sep 2013 16:22:52 +0200 Subject: [PATCH] move-chunk script --- scripts/move-chunk.py | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 scripts/move-chunk.py diff --git a/scripts/move-chunk.py b/scripts/move-chunk.py new file mode 100755 index 00000000..ea448428 --- /dev/null +++ b/scripts/move-chunk.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +import sys +sys.path.append('.') +sys.path.append('./apps') +sys.path.append('./lib') + +from django.core.management import setup_environ +from redakcja import settings + +setup_environ(settings) + +from catalogue.models import Book, Chunk +import re + +from django.db import transaction + +if len(sys.argv) != 4: + print "dump-book slug-part chunk-no to-position" + sys.exit(-1) + +term = sys.argv[1] +chunk_idx = int(sys.argv[2]) +new_position = int(sys.argv[3]) + +with transaction.commit_on_success(): + books = Book.objects.filter(slug__contains=term) + if len(books) > 1: + for b in range(len(books)): + print "%d) %s" % (b+1, books[b].slug) + print "Which one? " + ch = int(raw_input()) + book = books[ch-1] + else: + book = books[0] + + chunks_total = len(book) + max_number = max(c.number for c in book) + moving_chunk = next(c for c in book if c.number == chunk_idx) + + moving_chunk.number = max_number+2 + moving_chunk.save() + + adjust = 1 + for i in range(chunks_total-1, -1, -1): + chunk = book[i] + chunk.number = i + 1 + adjust + chunk.save() + if i + 1 == new_position: + adjust = 0 + + + moving_chunk.number = new_position + moving_chunk.save() + + book = Book.objects.get(pk=book.pk) + for c in book: + print c + while True: + print "is this ok (y/n)? " + resp = raw_input() + if resp[0] == 'y': + break + elif resp[0] == 'n': + raise Exception("Aborting") + + -- 2.20.1