示例#1
0
def edit_book(number):
	amazon_img = False
	if number == 'new':
		book = Book()
		book.mass = 0
		book.width = 0
		book.length = 0
		book.thickness = 0
		title = 'Ajouter un livre dans la bibliotheque'
	elif number[0:7] == 'amazon:':
		import urllib
		asin=unicode(number[7:])
		amazon = bottlenose.Amazon(AWS_KEY,AMAZON_SECRET_KEY,LANG)
		#
		# we fetch the primary informations from large group amazon search
		#
		fetch = amazon.ItemLookup(IdType='ASIN', ItemId= asin, ResponseGroup='Large')
		xml = objectify.fromstring(fetch)
		book = Book()
		try:
			book.title = unicode(xml.Items.Item.ItemAttributes.Title)
		except AttributeError:
			book.title = ''
		try:
			amazon_img = unicode(xml.Items.Item.LargeImage.URL)
		except AttributeError:
			amazon_img = ''
		try:
			book.isbn = unicode(xml.Items.Item.ItemAttributes.ISBN)
		except AttributeError:
			book.isbn = ''
		try:
			book.ean = unicode(xml.Items.Item.ItemAttributes.EAN)
		except AttributeError:
			book.ean = ''
		try:
			book.publisher = unicode(xml.Items.Item.ItemAttributes.Publisher)
		except AttributeError:
			book.publisher = ''
		
		#
		# amazon works in US units (hundreds-inches and pounds). I want them in kilos and cm, so I import the data in float and translate.
		#
		try:
			thickness = float(xml.Items.Item.ItemAttributes.PackageDimensions.Height)
			book.thickness = round(thickness * 2.54/100, 1)
		except AttributeError:
			book.thickness = ''
		try:
			length = float(xml.Items.Item.ItemAttributes.PackageDimensions.Length)
			book.length = round(length * 2.54/100, 1)
		except AttributeError:
			book.length = ''
		try:
			width = float(xml.Items.Item.ItemAttributes.PackageDimensions.Width)
			book.width = round(width * 2.54/100, 1)
		except AttributeError:
			book.width = ''
		try:
			mass = float(xml.Items.Item.ItemAttributes.PackageDimensions.Weight)
			book.mass = round(mass * 0.45/100,2)
		except AttributeError:
			book.mass = ''
		try:
			book.numberofpages = int(xml.Items.Item.ItemAttributes.NumberOfPages)
		except AttributeError:
			book.numberofpages = ''
		try:
			book.summary = unicode(xml.Items.Item.EditorialReviews.EditorialReview.Content)
		except AttributeError:
			book.summary = ''
		title = 'Ajouter un livre d\'Amazon dans la bibliotheque'

	else:
		book = Book.query.get(number)
		title = book.title
		if os.path.exists('app/static/covers/' + str(book.id)):
			book.img = True
	form = BookForm()
	form.authortoadd.choices = Author.query.filter(Author!=book.authors)
	update = False
	#if form.validate_on_submit():
	if request.form=='POST' and form.validate():
		#print form.errors
		book.title = unicode(form.title.data)
		# on ajoute les élements en dessous, mais s'ils ne sont pas là, c'est pas grave !
		if form.ean.data != book.ean:
			book.ean = unicode(form.ean.data)
			update = True
		if form.isbn.data != book.isbn:
			book.isbn = unicode(form.isbn.data)
			update = True
		if form.thickness.data != book.thickness:
			book.thickness = unicode(form.thickness.data)
			update = True
		if form.width.data != book.width:
			book.width = unicode(form.width.data)
			update = True
		if form.length.data != book.length:
			book.length = form.length.data
			update = True
		if form.summary.data != book.summary and form.summary.data != None:
			book.summary = unicode(form.summary.data)
			update = True
		if form.mass.data != book.mass:
			book.mass = form.mass.data
			update = True
		#if form.numberofpages.data != book.numberofpages:
		#	book.numberofpages = int(form.numberofpages.data)
		#	if type(book.numberofpages) == 'int'
		#		update = True
		if form.publisher.data != book.publisher and form.publisher.data != None:
			book.publisher = unicode(form.publisher.data)
			update = True
		#
		# gestion des auteurs
		if request.form.getlist('authortodelete'):
			for item in request.form.getlist('authortodelete'):
				a = Author.query.get(item)
				book.remove_author(a)
				update = True
		#
		if request.form.getlist('authortoadd'):
			for item in request.form.getlist('authortoadd'):
				# Same as authors, see upside.
				if item != '__None':
					a = Author.query.get(item)
					book.add_author(a)
					update = True
		# adding the book to the db
		if update:
			db.session.add(book)
			db.session.commit()
			db.session.refresh(book)
		
		image_finale = False
		
		if amazon_img:
			image_finale = urllib.urlopen(amazon_img)
			
		if request.method == 'POST' and request.files['cover']:
			image_finale = request.files['cover']
			
		if image_finale:
			fileurl = 'app/static/covers/' + str(book.id)
			image = FileStorage(image_finale).save(fileurl)
				
		return redirect('/admin')
	return render_template('edit_book.html', form = form, book = book, title = title, amazon_img = amazon_img)