Beispiel #1
0
def edit_author(number):
	if number == 'new':
		author = Author()
		title = 'Ajouter un auteur dans la bibliotheque'
	else:
		author = Author.query.get(number)
		title = author
		if os.path.exists('app/static/photos/' + str(author.id)):
			author.img = True
	#global author
	#a = author
	form = AuthorForm()
	form.booktoadd.choices = Book.query.filter(Book!=author.books)
	update = False
	#if form.validate_on_submit():
	if request.form=='POST' and form.validate():
		#print form.errors
		author.firstname = unicode(form.firstname.data)
		author.familyname = unicode(form.familyname.data)
		if form.nationality.data != author.nationality:
			author.nationality= unicode(form.nationality.data)
			update = True
		#a.dateofbirth= form.dateofbirth.data
		if form.placeofbirth.data != author.placeofbirth:
			author.placeofbirth = unicode(form.placeofbirth.data)
			update = True
		if form.website.data != author.website:
			author.website = unicode(form.website.data)
			update = True
		if form.biography.data != author.biography:
			author.biography = unicode(form.biography.data)
			update = True

		# gestion des livres
		if request.form.getlist('booktodelete'):
			for item in request.form.getlist('booktodelete'):
				a = Book.query.get(item)
				author.remove_book(a)
				update = True
		
		if request.form.getlist('booktoadd'):
			for item in request.form.getlist('booktoadd'):
				# even if noting is select, the field return something, a unicode string '__None'
				# this "if" not to block the whole thing. Same on books !
				if item != '__None':
					a = Book.query.get(item)
					author.add_book(a)
					update = True
		
		if update:
			db.session.add(author)
			db.session.commit()
			db.session.refresh(author)
		
		# the photo will overwrite the previous if existing.
		# thus, only one per author and nothing else to check
		if request.method == 'POST' and request.files['portrait']:
			fileurl = 'app/static/photos/' + str(author.id)
			image = request.files['portrait'].save(fileurl)
		
		return redirect('/admin')
	return render_template('edit_author.html', form = form, author = author, title = title)