def new_author(): authors = Author.query.all() if request.method == 'POST': newAuthor = Author(name=request.form['name']) db.session.add(newAuthor) db.session.commit() return redirect(url_for('index_2')) else: return render_template('newAuthor.html', authors=authors)
def add_author(): if not request.json or Session.query(Author).filter( Author.id == request.json['id']).first(): abort(400) new_author = Author(id=request.json['id'], name=request.json['name'], created_at=datetime.now()) Session.add(new_author) Session.commit() return 'OK', 200
def newAuthor(): if request.method == 'POST': theAuthor = request.form['authorname'].title() theBio = request.form['bio'] thePhoto = request.form['picture'] userID = login_session['user_id'] if theAuthor and theBio and thePhoto: session.add( Author(last_name=theAuthor, bio=theBio, photo=thePhoto, user_id=userID)) session.commit() flash("Author has been added!") return redirect(url_for('showAllAuthors')) else: flash("You need to fill out all the fields!") return render_template('newauthor.html') else: return render_template('newauthor.html')
import flask_whooshalchemy as wa app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///books.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True #whooshalachemy才知道 app.config['DEBUG'] = True app.config['WHOOSH_BASE'] = 'whoosh' db = SQLAlchemy(app) db.create_all() wa.whoosh_index(app, Book) # Book for Alexis Angel author1 = Author(name="Alexis Angel") db.session.add(author1) db.session.commit() book1 = Book(name="Java Usage", description="how to use java", price="9.99", author=author1) db.session.add(book1) db.session.commit() book2 = Book(name="C++ Usage", description="how to use c++", price="5.50", author=author1) db.session.add(book2)
from datetime import datetime if __name__ == '__main__': Session.query(AuthorBookLink).delete() Session.query(Book).delete() Session.query(Author).delete() with open('books.json') as f: data = json.load(f) book_index = 1 index = 1 authors = {} for line in data: exists = Session.query(Author).filter_by(name=line['author']).first() if not exists: authors[line['author']] = index Session.add( Author(id=index, name=line['author'], created_at=datetime.now())) index += 1 Session.add( Book(id=book_index, title=line['title'], created_at=datetime.now())) Session.commit() Session.add( AuthorBookLink(author_id=authors[line['author']], book_id=book_index)) Session.commit() book_index += 1
def get(self): args = listV.parse_args() result = Author.getAll(args) return result['data'], result['code']
def delete(self, authorId): result = Author.delete(authorId) return result['data'], result['code']
def patch(self, authorId): args = updateV.parse_args() result = Author.updateById(authorId, args) return result['data'], result['code']
def get(self, authorId): result = Author.getById(authorId) return result['data'], result['code']
def post(self): args = createV.parse_args() Author.create(args) return args