def new(): db = get_db() if request.method == 'POST': title = request.form['title'] author = request.form['author'] db = get_db() db.execute("INSERT INTO books(title,author) VALUES(?,?)", (title, author)) db.commit() return redirect(url_for('books.all')) authors = db.execute('SELECT * FROM authors').fetchall() return render_template('books/new.html', authors=authors)
def new(): if request.method == 'POST': name = request.form['name'] bio = request.form['bio'] db = get_db() db.execute("INSERT INTO authors (name,bio) VALUES (?,?)", (name, bio)) db.commit() return redirect(url_for('authors.all')) return render_template('authors/new.html')
def write(): csv_str = "" db = get_db() alldata = db.execute('SELECT * FROM books').fetchall() for data in alldata: csv_str += ",".join([data['title'], data['author'], data['cover']]) csv_str += "\n" write_file("books.csv", csv_str) return render_template('books/write.html', str=csv_str)
def edit(author_id): db = get_db() if request.method == 'POST': name = request.form['name'] bio = request.form['bio'] db.execute("UPDATE authors SET name=?, bio=? where id=?",(name,bio,author_id)) db.commit() return redirect(url_for('authors.all')) author = db.execute('SELECT * FROM authors where id=?',author_id).fetchone() return render_template('authors/edit.html',author=author)
def edit(book_id): db = get_db() if request.method == 'POST': name = request.form['name'] bio = request.form['bio'] db.execute("UPDATE books SET name=?, bio=? where id=?", (name, bio, book_id)) db.commit() return redirect(url_for('books.all')) book = db.execute('SELECT * FROM books where id=?', book_id).fetchone() return render_template('books/edit.html', book=book)
def books_data(): try: db = get_db() titles = db.execute( "SELECT title,username,timestamp FROM books" ).fetchall() db.commit() titles = [(title[0],title[1],title[2]) for title in titles] print(titles) return jsonify(res=":)",data=titles) except: return jsonify(res=":(")
def newbook(): if request.method == 'POST': title = request.form['title'] author = request.form['author'] genre = request.form['genre'] db = get_db() db.execute("INSERT INTO books (title, author, genre) VALUES (?, ?, ?)", (title, author, genre)) db.commit() return redirect(url_for('books.index')) return render_template('newbook.html')
def upload(book_id): db = get_db() if request.method == 'POST': if 'file' in request.files: file = request.files['file'] save_img(file) db.execute("UPDATE books SET cover=? where id=?", (file.filename, book_id)) db.commit() return redirect(url_for('books.show', book_id=book_id)) book = db.execute('SELECT * FROM books where id =?', book_id).fetchone() return render_template('books/upload.html', book=book)
def delbook(): try: posted_data = request.data.decode("utf-8") data = json.loads(posted_data) title = data["title"] db = get_db() db.execute( "DELETE FROM books WHERE title = '{0}'".format(title) ) db.commit() return jsonify(res=":)") except: return jsonify(res=":(")
def upload(): if request.method == 'POST': if 'file' in request.files: file = request.files['file'] save_csv(file) datadict=read_csv(file.filename) db=get_db() for data in datadict: db.execute( "INSERT INTO authors(name,bio) VALUES(?,?)",(data['name'],data['bio']) ) db.commit() return redirect(url_for('authors.all')) return render_template('authors/upload.html')
def newbook(): posted_data = request.data.decode("utf-8") data = json.loads(posted_data) title = data["title"] author = data["author"] genre = data["genre"] if not ((1<=len(title)<=20) and (1<=len(author)<=20) and (1<=len(genre)<=20)): return jsonify(res=":(",message="長さが1<=n<=20の範囲外です") db = get_db() titles = db.execute( "SELECT * FROM books WHERE title = '{0}'".format(title) ).fetchall() titles = [t[0] for t in titles] if (len(titles) == 1): return jsonify(res=":(",message="タイトルが重複しています") db.commit() db.execute( "INSERT INTO books (title, author, genre, lending, username, timestamp) VALUES (?, ?, ?, ?, ?, ?)", (title,author,genre,False,'','') ) db.commit() return jsonify(res=":)")
def index(): db = get_db() alldata = db.execute('SELECT * FROM books').fetchall() if request.method == 'POST': genre = request.form['genre'] message = '分野が「' + genre + '」を含む本は' searchdata = db.execute("SELECT * FROM books WHERE genre like '%" + genre + "%'").fetchall() if len(searchdata) > 0: message += '以下の通りです' else: searchdata = alldata message += '見つかりませんでした' return render_template('index.html', message=message, books=searchdata) else: message = 'お探しの分野は何ですか' return render_template('index.html', message=message, books=alldata)
def delete(book_id): db = get_db() db.execute('DELETE FROM books where id=?', book_id) db.commit() return redirect(url_for('books.all'))
def show(book_id): db = get_db() book = db.execute('SELECT * FROM books where id=?', book_id).fetchone() return render_template('books/show.html', book=book)
def all(): db = get_db() alldata = db.execute('SELECT * FROM authors').fetchall() return render_template('authors/all.html', authors=alldata)
def show(author_id): db = get_db() author = db.execute('SELECT * FROM authors where id = ?', author_id).fetchone() return render_template('authors/show.html', author=author)
def all(): db = get_db() alldata = db.execute('SELECT * FROM books').fetchall() return render_template('books/all.html', books=alldata)
def delete(author_id): db = get_db() db.execute("DELETE from authors where id = ?", author_id) db.commit() return redirect(url_for('authors.all'))