Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

Fikset mer på commit-kommandoen.

Nå ser det ut til å være mulig å redigere/lage personer og bøker.

Koden er grusomt stygg.
This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-03-06 18:52:14 +00:00
parent 11c67cd8bf
commit ef0724ca54
1 changed files with 32 additions and 7 deletions

View File

@ -103,8 +103,11 @@ def commit_actions(actions):
actions.sort(action_cmp) actions.sort(action_cmp)
try: try:
new_persons = {}
for action in actions: for action in actions:
perform_action(action) obj = perform_action(action, new_persons)
if isinstance(obj, Person):
new_persons[obj.id] = obj
transaction.commit() transaction.commit()
except Exception, e: except Exception, e:
print 'Error when commiting.' print 'Error when commiting.'
@ -112,9 +115,14 @@ def commit_actions(actions):
traceback.print_exc() traceback.print_exc()
transaction.rollback() transaction.rollback()
def perform_action(a): def perform_action(a, new_persons):
if a['action'] == 'edit-book': action = a['action']
b = Book.objects.get(isbn=a['isbn']) 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 'title' in a: b.title = a['title']
if 'subtitle' in a: b.subtitle = a['subtitle'] if 'subtitle' in a: b.subtitle = a['subtitle']
if 'category' in a: b.category = Category.objects.get(id=a['category']) 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 'description' in a: b.description = a['description']
if 'persons' in a: if 'persons' in a:
for bp in b.persons.all(): 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() bp.delete()
for rel, person_list in a['persons'].items(): for rel, person_list in a['persons'].items():
for person_id in person_list: for person_id in person_list:
if person_id not in map(lambda p: p.id, b.get_persons(rel)): 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, BookPerson(book=b,
person=Person.objects.get(id=person_id), person=person,
relation=Relation.objects.get(name=rel)).save() relation=Relation.objects.get(name=rel)).save()
if 'references' in a: if 'references' in a:
for ref in b.references.all(): 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() ref.delete()
for reftype, text_list in a['references'].items(): for reftype, text_list in a['references'].items():
for text in text_list: for text in text_list:
@ -151,6 +165,17 @@ def perform_action(a):
text=text).save() text=text).save()
# TODO pictures # TODO pictures
b.save() 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): def edit_book_or_person(ids):
filename = show_book_or_person(ids, commit_format=True, tmp_file=True) filename = show_book_or_person(ids, commit_format=True, tmp_file=True)