Begynt på commit.
Det er foreløpig delvis støtte for å redigere bøker.
This commit is contained in:
parent
73be593cd8
commit
e185f784c6
|
@ -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':
|
||||
|
|
Reference in New Issue