From ef0724ca543619837a873c99cd7d1ab27f9916be Mon Sep 17 00:00:00 2001 From: oysteini Date: Sun, 6 Mar 2011 18:52:14 +0000 Subject: [PATCH] =?UTF-8?q?Fikset=20mer=20p=C3=A5=20commit-kommandoen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nå ser det ut til å være mulig å redigere/lage personer og bøker. Koden er grusomt stygg. --- python/worblehat.py | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/python/worblehat.py b/python/worblehat.py index 0094ff3..c4f0797 100755 --- a/python/worblehat.py +++ b/python/worblehat.py @@ -103,8 +103,11 @@ def commit_actions(actions): actions.sort(action_cmp) try: + new_persons = {} for action in actions: - perform_action(action) + obj = perform_action(action, new_persons) + if isinstance(obj, Person): + new_persons[obj.id] = obj transaction.commit() except Exception, e: print 'Error when commiting.' @@ -112,9 +115,14 @@ def commit_actions(actions): traceback.print_exc() transaction.rollback() -def perform_action(a): - if a['action'] == 'edit-book': - b = Book.objects.get(isbn=a['isbn']) +def perform_action(a, new_persons): + action = a['action'] + if action == 'edit-book' or action == 'new-book': + print 'Commiting %s action, ISBN: %s' % (action, a['isbn']) + if action == 'edit-book': + b = Book.objects.get(isbn=a['isbn']) + else: + b = Book(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']) @@ -131,17 +139,23 @@ def perform_action(a): if 'description' in a: b.description = a['description'] if 'persons' in a: for bp in b.persons.all(): - if bp.person.id not in a['persons'][bp.relation.name]: + if (bp.relation.name not in a['persons'] + or bp.person.id not in a['persons'][bp.relation.name]): bp.delete() for rel, person_list in a['persons'].items(): for person_id in person_list: if person_id not in map(lambda p: p.id, b.get_persons(rel)): + if person_id in new_persons.keys(): + person = new_persons[person_id] + else: + person = Person.objects.get(id=person_id) BookPerson(book=b, - person=Person.objects.get(id=person_id), + person=person, relation=Relation.objects.get(name=rel)).save() if 'references' in a: for ref in b.references.all(): - if ref.text not in a['references'][ref.reference_type]: + if (ref.reference_type not in a['references'] + or ref.text not in a['references'][ref.reference_type]): ref.delete() for reftype, text_list in a['references'].items(): for text in text_list: @@ -151,6 +165,17 @@ def perform_action(a): text=text).save() # TODO pictures b.save() + return b + if action == 'edit-person' or action == 'new-person': + print 'Commiting %s action, id: %s' % (action, a['id']) + if action == 'edit-person': + p = Person.objects.get(id=a['id']) + else: + p = Person(id=a['id']) + if 'first_name' in a: p.first_name = a['first_name'] + if 'last_name' in a: p.last_name = a['last_name'] + p.save() + return p def edit_book_or_person(ids): filename = show_book_or_person(ids, commit_format=True, tmp_file=True)