From e185f784c650b837314143a2ff5e72d110bab5e7 Mon Sep 17 00:00:00 2001 From: oysteini Date: Sun, 6 Mar 2011 15:45:20 +0000 Subject: [PATCH] =?UTF-8?q?Begynt=20p=C3=A5=20commit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Det er foreløpig delvis støtte for å redigere bøker. --- python/worblehat.py | 61 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/python/worblehat.py b/python/worblehat.py index 5c28f2a..1f61e8c 100755 --- a/python/worblehat.py +++ b/python/worblehat.py @@ -6,10 +6,12 @@ os.environ['DJANGO_SETTINGS_MODULE']='web.settings' from web.library.models import * from web.library.fileformat import read_actionlist, write_actionlist from django.db.models import Q +from django.db import transaction from util import * import getopt import sys import types +import traceback import search import placement import tempfile @@ -74,13 +76,66 @@ def search_person(search_strings, search_description=False): Q(books__book__id__id__icontains=word)) return remove_duplicates(basic_query.all()) -def commit(filename=None): # TODO - pass +def commit(filename=None): + if filename: + try: + f = file(filename, 'r') + text = f.read() + f.close() + except IOError, e: + print 'commit: Error reading file %s: %s' % (filename, e) + print 'Exiting.' + sys.exit(1) + else: + text = sys.stdin.read() + + actions = read_actionlist(text) + commit_actions(actions) + +@transaction.commit_manually +def commit_actions(actions): + # Ensure that creation of new persons is performed first, so that + # books in the same commit can refer to them: + def action_cmp(a1, a2): + if a1['action'] == 'new-person' and a2['action'] != 'new-person': + return -1 + return cmp(a1, a2) + actions.sort(action_cmp) + + try: + for action in actions: + perform_action(action) + transaction.commit() + except Exception, e: + print 'Error when commiting.' + print e + traceback.print_exc() + transaction.rollback() + +def perform_action(a): + if a['action'] == 'edit-book': + b = Book.objects.get(isbn=a['isbn']) + if 'title' in a: b.title = a['title'] + if 'subtitle' in a: b.subtitle = a['subtitle'] + if 'category' in a: b.category = Category.objects.get(id=a['category']) + if 'publisher' in a: b.publisher = a['publisher'] + if 'published_year' in a: b.published_year = a['published_year'] + if 'edition' in a: b.edition = a['edition'] + if 'num_pages' in a: b.num_pages = a['num_pages'] + if 'series' in a: + series_lst = BookSeries.objects.filter(Q(id=a['series'])) + if len(series_lst) > 0: + b.series = series_lst[0] + else: + b.series = None + if 'description' in a: b.description = a['description'] + # TODO persons, references, pictures + b.save() def edit_book_or_person(ids): filename = show_book_or_person(ids, commit_format=True, tmp_file=True) print filename -# run_editor(filename) # TODO: må implementeres + run_editor(filename) commit(filename) commands = { 'show':