def edit_target(tgt_id): form = TargetForm(request.form) target = session.query(Target).get(tgt_id) if request.method == 'GET': form = TargetForm(request.form, session.query(Target).get(tgt_id)) if request.method == 'POST': if form.validate(): tgt_edited = Target(form.tgt_name.data, form.value_x.data, form.value_y.data, form.value_z.data) flash(u'You sucessfully edited target %s' % form.tgt_name.data) target_db = session.query(Target).get(tgt_id) target_db.tgt_name = tgt_edited.tgt_name target_db.value_x = tgt_edited.value_x target_db.value_y = tgt_edited.value_y target_db.value_z = tgt_edited.value_z session.commit() return redirect(url_for('index')) else: error = "Fields are not filled correctly!" return render_template('target_form.html', form=form, method="POST", action="/targets/%s/edit" % tgt_id, submit_text="Save changes", title="EDIT", target=target)
def add_author(): form = AuthorForm(request.form) if request.method == 'POST' and form.validate(): author = Author(form.name.data) session.add(author) session.commit() return redirect(url_for('index')) return render_template('author_form.html', form=form, action="/author/add")
def op_create(): session.add(Users(name="alex0", extra='sb')) session.add_all([ Users(name="alex1", extra='sb'), Users(name="alex2", extra='sb'), ]) session.commit() # 这里需要commit,不然不提交数据
def add_book(): form = BookForm(request.form) if request.method == 'POST' and form.validate(): book = Book(form.title.data, form.authors.data) session.add(book) session.commit() return redirect(url_for('index')) return render_template('book_form.html', form=form, action="/book/add")
def edit_author(author_id): form = AuthorForm(request.form) if request.method == 'GET': form = AuthorForm(request.form, session.query(Author).get(author_id)) if request.method == 'POST' and form.validate(): author_edited = Author(form.name.data) author_db = session.query(Author).get(author_id) author_db.name = author_edited.name session.commit() return redirect(url_for('index')) return render_template('author_form.html', form=form, action="/author/%s/edit" % author_id, submit_text="Save")
def op_update(): session.query(Users).filter(Users.id == 2).update({"name" : "099"}) ''' 2018-04-15 21:49:57,554 INFO sqlalchemy.engine.base.Engine UPDATE users SET name=(concat(users.name, %s)) WHERE users.id = %s 2018-04-15 21:49:57,554 INFO sqlalchemy.engine.base.Engine ('099', 2) ''' session.query(Users).filter(Users.id == 2).update({Users.name: Users.name + "099"}, synchronize_session=False) # session.query(Users).filter(Users.id == 2).update({"num": Users.num + 1}, synchronize_session="evaluate") session.commit()
def edit_book(book_id): form = BookForm(request.form) if request.method == 'GET': form = BookForm(request.form, session.query(Book).get(book_id)) if request.method == 'POST' and form.validate(): book_edited = Book(form.title.data, form.authors.data) book_db = session.query(Book).get(book_id) book_db.title = book_edited.title book_db.authors = book_edited.authors session.commit() return redirect(url_for('index')) return render_template('book_form.html', form=form, action="/book/%s/edit" % book_id, submit_text="Save")
def add_book(): form = BookForm(request.form) if request.method == 'POST' and form.validate(): book = Book(form.title.data, form.author.data) session.add(book) session.commit() return redirect(url_for('index')) return render_template('book_form.html', form=form, action="/book/add", user_name=current_user.name, user_picture=current_user.picture, if_administrator=verify_administrator())
def add_author(): form = AuthorForm(request.form) if request.method == 'POST' and form.validate(): author = Author(form.name.data) session.add(author) session.commit() return redirect(url_for('index')) return render_template('author_form.html', form=form, action="/author/add", user_name=current_user.name, user_picture=current_user.picture, if_administrator=verify_administrator())
def edit_author(author_id): form = AuthorForm(request.form) if request.method == 'GET': form = AuthorForm(request.form, session.query(Author).get(author_id)) if request.method == 'POST' and form.validate(): author_edited = Author(form.name.data) author_db = session.query(Author).get(author_id) author_db.name = author_edited.name session.commit() return redirect(url_for('index')) return render_template('author_form.html', form=form, action="/author/%s/edit" % (author_id), submit_text="Save", user_name=current_user.name, user_picture=current_user.picture, if_administrator=verify_administrator())
def edit_book(book_id): form = BookForm(request.form) if request.method == 'GET': form = BookForm(request.form, session.query(Book).get(book_id)) if request.method == 'POST' and form.validate(): book_edited = Book(form.title.data, form.author.data) book_db = session.query(Book).get(book_id) book_db.title = book_edited.title book_db.authors = book_edited.authors session.commit() return redirect(url_for('index')) return render_template('book_form.html', form=form, action="/book/%s/edit" % (book_id), submit_text="Save", user_name=current_user.name, user_picture=current_user.picture, if_administrator=verify_administrator())
def add_customers(): form = CustomerForm(request.form) error = None if form.validate(): customer = Customer(form.first_name.data, form.last_name.data, form.email.data, form.age.data) flash('You sucessfully added a new customer!') session.add(customer) session.commit() return redirect(url_for('customers')) else: error = "Fields are not filled correctly!" return render_template('customer_form.html', form=form, method="POST", action="/add", error=error, submit_text="Save")
def edit_customer(c_id): form = CustomerForm(request.form) if form.validate(): cust_edited = Customer(form.first_name.data, form.last_name.data, form.email.data, form.age.data) flash('You sucessfully edited customer') customer_db = session.query(Customer).get(c_id) customer_db.first_name = cust_edited.first_name customer_db.last_name = cust_edited.last_name customer_db.age = cust_edited.age customer_db.email = cust_edited.email session.commit() return redirect(url_for('customers')) else: error = "Fields are not filled correctly!" return render_template('customer_form.html', form=form, method="POST", action="/customer/%s/edit" % c_id, submit_text="Save changes")
def add_numbers(): form = TargetForm(request.form) error = None result = None if request.method == 'POST': if form.validate(): target = Target(form.tgt_name.data, form.value_x.data, form.value_y.data, form.value_z.data) flash('You sucessfully added a new target!') session.add(target) session.commit() return redirect(url_for('index')) else: error = "Fields are not filled correctly!" return render_template('target_form.html', form=form, method="POST", action="/add", error=error, submit_text="Save", title="ADD NEW")
def delete_author(author_id): author = session.query(Author).get(author_id) session.delete(author) session.commit() return redirect(url_for('index'))
def op_delete(): result = session.query(Users).filter(Users.id > 2).delete() print(type(result)) # <type 'long'> session.commit()
def delete_target(tgt_id): target = session.query(Target).get(tgt_id) session.delete(target) session.commit() return redirect(url_for('index'))
def delete_customer(c_id): customer = session.query(Customer).get(c_id) session.delete(customer) session.commit() return redirect(url_for('customers'))
def delete_book(book_id): book = session.query(Book).get(book_id) session.delete(book) session.commit() return redirect(url_for('index'))
if msg is None: # No packets on queue sleep(10) else: # Get packet id from the queue packet_id = None try: packet_id = int(msg.get_body()) print "Packet id:", str(packet_id) except: # If the message doesn't contain an integer (i.e someone put something on the queue that doesnt belong there) print "Erroneous msg" delMsg(msg) continue # Get that packet from the database try: packet = session.query(Packet).filter(Packet.id == packet_id).one() except: # Packet has been removed from db since it was added to the queue delMsg(msg) continue # Check if cartype has been added since the packet was enqueued (i.e packet.cartype != None) if packet.cartype is None: # Calculate cartype ctype = packet.getCarType() # if ctype is None that means the data was corrupt and the packet was deleted from db by the call to getCarType() if ctype is not None: session.add(packet) session.commit() delMsg(msg)