Exemplo n.º 1
0
def op_query():
    '''
    SELECT users.name AS users_name, users.extra AS users_extra 
    FROM users
    '''
    ret = session.query(Users).all()
    print('1=====', ret)
    
    '''
    SELECT users.name AS users_name, users.extra AS users_extra 
    FROM users
    '''
    ret = session.query(Users.name, Users.extra).all()
    print('2=====', ret)
    
    '''
    SELECT users.id AS users_id, users.name AS users_name, users.extra AS users_extra 
    FROM users 
    WHERE users.name = %s
    '''
    ret = session.query(Users).filter_by(name='alex0').all()
    print('3=====', ret)
    
    '''
    SELECT users.id AS users_id, users.name AS users_name, users.extra AS users_extra 
    FROM users 
    WHERE users.name = %s 
     LIMIT %s
    '''
    ret = session.query(Users).filter_by(name='alex0').first()
    print('4=====', ret)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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")
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
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")
Exemplo n.º 6
0
def showedit_customer(c_id):
    form = CustomerForm(request.form, session.query(Customer).get(c_id))
    return render_template('customer_form.html',
                           form=form,
                           method="POST",
                           action="/customers/%s/edit" % c_id,
                           submit_text="Save changes")
Exemplo n.º 7
0
def search_results():
    q = request.values['q']
    targets = session.query(Target).filter(
        or_(Target.tgt_name.ilike("%" + q + "%"),
            Target.value_x.ilike("%" + q + "%"),
            Target.value_y.ilike("%" + q + "%"),
            Target.value_z.ilike("%" + q + "%")))
    return render_template('search_results.html', targets=targets, q=q)
Exemplo n.º 8
0
def index():
    authors = session.query(Author).all()
    books = session.query(Book).all()
    if isinstance(current_user._get_current_object(), AnonymousUserMixin):
        return render_template('index.html',
                           authors=authors,
                           books=books,
                           user_name=None,
                           user_picture=None,
                           if_administrator=verify_administrator())
    else:
        return render_template('index.html',
                           authors=authors,
                           books=books,
                           user_name=current_user.name,
                           user_picture=current_user.picture,
                           if_administrator=verify_administrator())
Exemplo n.º 9
0
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())
Exemplo n.º 10
0
def search_name():
    q = request.values['q']
    customers = session.query(Customer).filter(
        or_(Customer.first_name.ilike("%" + q + "%"),
            Customer.last_name.ilike("%" + q + "%"),
            Customer.email.ilike("%" + q + "%"),
            Customer.age.ilike("%" + q + "%")))
    return render_template('search_results.html', customers=customers, q=q)
Exemplo n.º 11
0
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())
Exemplo n.º 12
0
def search_results():
    q = request.values['q']
    books = session.query(Book).filter(or_(Book.title.like("%"+q+"%"), Book.authors.any(Author.name.like("%"+q+"%"))))
    if isinstance(current_user._get_current_object(), AnonymousUserMixin):
        return render_template('search_results.html',
                           books=books,
                           user_name=None,
                           user_picture=None,
                           if_administrator=verify_administrator())
    else:
        return render_template('search_results.html',
                           books=books,
                           user_name=current_user.name,
                           user_picture=current_user.picture,
                           if_administrator=verify_administrator())
Exemplo n.º 13
0
def op_condition():
    
#     ret = session.query(Users).filter_by(name='alex0').all()
#     for user in ret:
#         print('1=====', user.__dict__)

    ''' AND
    WHERE users.id > %s AND users.name = %s
    '''
#     ret = session.query(Users).filter(Users.id > 1, Users.name == 'eric').all()
#      for user in ret:
#          print('2=====', user.__dict__)
     
     ''' AND, BETWEEN
     WHERE users.id BETWEEN %s AND %s AND users.name = %s
     '''
     ret = session.query(Users).filter(Users.id.between(1, 3), Users.name == 'alex0')  # 这里的加不加 all()都一样
     for user in ret:
          print('3=====', user.__dict__)
Exemplo n.º 14
0
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")
Exemplo n.º 15
0
def populate_edges(location):
  
  # Query for all routes from this location
  routes = session.query(Route)\
    .filter(Route.source == location.city_name)
  
  # If we're arriving from a previous route, eliminate nonconnectables
  if type(location._route) == Route:
    routes = routes.filter(Route.depart > location._route.arrive)
  

  routes = routes.all()
  

  for route in routes:
    
    next_location = Location(route)
    
    G.add_edge(location, next_location, weight=route.fare)
    
    if(next_location.city_name == END.city_name):
      G.add_edge(next_location, FINAL)
    else:
      populate_edges(next_location)
Exemplo n.º 16
0
	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)
Exemplo n.º 17
0
def get_author():
    return session.query(Author).all()
Exemplo n.º 18
0
def search_results():
    q = request.values['q']
    books = session.query(Book).filter(
        or_(Book.title.like("%" + q + "%"), Book.authors.any(Author.name.like("%" + q + "%"))))
    return render_template('search_results.html', books=books)
Exemplo n.º 19
0
def get_target():
    return session.query(Target).all()
Exemplo n.º 20
0
def targets():
    targets = session.query(Target).all()
    return render_template('targets.html', targets=targets)
Exemplo n.º 21
0
def run_target(tgt_id):
    targets = session.query(Target).all()
    result = None
    target_db = session.query(Target).get(tgt_id)
    ser = serial.Serial(port='COM10',
                        baudrate=115200,
                        parity=serial.PARITY_NONE,
                        stopbits=serial.STOPBITS_ONE,
                        bytesize=serial.EIGHTBITS,
                        timeout=0.1)
    #ser1.write('X1J'+ str(int(target_db.value_x)) +','+ str(int(target_db.value_y)) + ',' + str(int(target_db.value_z)) + '\r')
    #ser1.write('X2J'+ str(int(target_db.value_x)) +','+ str(int(target_db.value_y)) + ',' + str(int(target_db.value_z)) + '\r')
    #result = ser1.read(30)
    #ser1.close()

    speed = '100'
    countsX = None
    countsY = None
    countsZ = None
    broadcast = 'X127T1,2\r'
    okX = None
    okY, result = None, None

    if target_db.value_x:
        countsX = 'X1T' + str(int(float(target_db.value_x) / 1.25)) + ',1\r'
    if target_db.value_y:
        countsY = 'X2T' + str(int(float(target_db.value_y) / 1.25)) + ',1\r'

    if countsX != None:
        ser.write(countsX)
        ser.write('X1Y8,' + speed + '\r')
        ser.write('X1U\r')
        r = ser.readline()
        checker = str(r[-3:])

        c = int('0x' + str(checker[0]), 16)
        d = int('0x' + str(checker[1]), 16)

        checkedC = hex(c & int('0x2', 16))
        checkedD = hex(d & int('0x4', 16))

        if checkedD == 0x4:
            okX = '1'
        else:
            if checkedC == 0x2:
                okX = '0'
            else:
                okX = '1'

    if countsY != None:
        ser.write(countsY)
        ser.write('X2Y8,' + speed + '\r')
        ser.write('X2U\r')
        r = ser.readline()
        checker = str(r[-3:])

        c = int('0x' + str(checker[0]), 16)
        d = int('0x' + str(checker[1]), 16)

        checkedC = hex(c & int('0x2', 16))
        checkedD = hex(d & int('0x4', 16))

        if checkedD == 0x4:
            okY = '1'
        else:
            if checkedC == 0x2:
                okY = '0'
            else:
                okY = '1'

    if (okX == '1' and okY == '1'):
        ser.write(broadcast)
        result = ser.read(15)

    ser.close()

    return render_template('index.html', result=result, targets=targets)
Exemplo n.º 22
0
def op_delete():
    result = session.query(Users).filter(Users.id > 2).delete()
    print(type(result))    # <type 'long'>
    session.commit()
Exemplo n.º 23
0
def customers():
    customers = session.query(Customer).all()
    return render_template('index.html', customers=customers)
Exemplo n.º 24
0
def search():
    param = request.values['param']
    customers = session.query(Customer).filter(Customer.age > param)
    return render_template('search_results.html',
                           customers=customers,
                           param=param)
Exemplo n.º 25
0
def search_id():
    idq = request.values['idq']
    customers = session.query(Customer).filter(
        Customer.c_id.like("%" + idq + "%"))
    return render_template('search_results.html', customers=customers, idq=idq)
Exemplo n.º 26
0
def delete_customer(c_id):
    customer = session.query(Customer).get(c_id)
    session.delete(customer)
    session.commit()
    return redirect(url_for('customers'))
Exemplo n.º 27
0
def dev_ide():
    targets = session.query(Target).all()
    speed = '300'
    return render_template('dev_ide.html', targets=targets, speed=speed)
Exemplo n.º 28
0
def delete_target(tgt_id):
    target = session.query(Target).get(tgt_id)
    session.delete(target)
    session.commit()
    return redirect(url_for('index'))
Exemplo n.º 29
0
def index():
    authors = session.query(Author).all()
    books = session.query(Book).all()
    return render_template('index.html', authors=authors, books=books)
Exemplo n.º 30
0
def index():
    targets = session.query(Target).all()
    return render_template('index.html', targets=targets)
Exemplo n.º 31
0
def delete_author(author_id):
    author = session.query(Author).get(author_id)
    session.delete(author)
    session.commit()
    return redirect(url_for('index'))
Exemplo n.º 32
0
def run_commands():
    targets = session.query(Target).all()

    comm = request.values['comm']
    speed = request.values['speed']
    # if speed:
    #     speed = str(speed)
    # else:
    #     speed = '300'

    if comm:
        ser = serial.Serial(port='COM10',
                            baudrate=115200,
                            parity=serial.PARITY_NONE,
                            stopbits=serial.STOPBITS_ONE,
                            bytesize=serial.EIGHTBITS,
                            timeout=0.2)
        countsA = None
        countsB = None
        countsC = None
        countsX = None
        countsY = None
        countsZ = None
        broadcast = 'X127T1,2\r'
        okToRun = None
        okX = None
        okY = None
        hz = None
        f = open('tekst.txt', 'a')

        p = parseString(comm)
        for lista in p:
            for target in targets:
                ime = [i for i, x in enumerate(lista) if x == target.tgt_name]
                if ime != []:
                    i = ime[0]
                    if target.value_x:
                        lista[i] = 'X'
                        lista.insert(i + 1, target.value_x)
                    if target.value_y:
                        lista.append('Y')
                        lista.append(target.value_y)
                    if target.value_z:
                        lista.append('Z')
                        lista.append(target.value_z)

        for parsed in p:
            l = len(parsed)
            if parsed[0] == 'goto':
                for i in range(1, len(parsed) - 1):
                    if parsed[i] == 'S' or parsed[i] == 's':
                        hz = str(int(float(parsed[i + 1]) / 5))
                    if parsed[i] == 'X' or parsed[i] == 'x':
                        countsX = 'X1T' + str(int(
                            float(parsed[i + 1]) / 1.25)) + ',1\r'
                    if parsed[i] == 'Y' or parsed[i] == 'y':
                        countsY = 'X2T' + str(int(
                            float(parsed[i + 1]) / 1.25)) + ',1\r'
                    if parsed[i] == 'Z' or parsed[i] == 'z':
                        valueZ = float(parsed[i + 1])
                        countsZ = 'X3T' + str(int(valueZ / 1.25)) + ',1\r'
                    if parsed[i] == 'A' or parsed[i] == 'a':
                        valueA = float(parsed[i + 1])
                        countsA = 'X4T' + str(int(valueA / 1.25)) + ',1\r'
                    if parsed[i] == 'B' or parsed[i] == 'b':
                        valueB = float(parsed[i + 1])
                        countsB = 'X5T' + str(int(valueB / 1.25)) + ',1\r'
                    if parsed[i] == 'C' or parsed[i] == 'c':
                        valueC = float(parsed[i + 1])
                        countsC = 'X6T' + str(int(valueC / 1.25)) + ',1\r'

                if countsX != None:
                    f.write('X\n')
                    ser.write(countsX)
                    if hz != None:
                        ser.write('X1Y8,' + hz + '\r')
                    else:
                        ser.write('X1Y8,' + speed + '\r')
                    str3 = 'X1U\r'
                    ser.write(str3)
                    r = ser.readline()

                    f.write(r)
                    checker = str(r[-3:])

                    c = int('0x' + str(checker[0]), 16)
                    d = int('0x' + str(checker[1]), 16)

                    checkedC = hex(c & int('0x2', 16))
                    checkedD = hex(d & int('0x4', 16))

                    if checkedD == 0x4:
                        okX = '1'
                    else:
                        if checkedC == 0x2:
                            okX = '0'
                        else:
                            okX = '1'

                if countsY != None:
                    f.write('Y\n')
                    ser.write(countsY)
                    if hz != None:
                        ser.write('X2Y8,' + hz + '\r')
                    else:
                        ser.write('X2Y8,' + speed + '\r')
                    str3 = 'X2U\r'
                    ser.write(str3)
                    d = int('0x' + str(checker[1]), 16)
                    r = ser.readline()
                    f.write(r)
                    checker = str(r[-3:])

                    c = int('0x' + str(checker[0]), 16)

                    checkedC = hex(c & int('0x2', 16))
                    checkedD = hex(d & int('0x4', 16))

                    if checkedD == 0x4:
                        okY = '1'
                    else:
                        if checkedC == 0x2:
                            okY = '0'
                        else:
                            okY = '1'

                if (okX == '1' and okY == '1'):
                    ser.write(broadcast)

                    #else:
                    #    return "problem"

    else:
        return "error"
    f.close()
    ser.close()
    return render_template('dev_ide.html', targets=targets, speed=speed)
Exemplo n.º 33
0
def show_customer(c_id):
    cust = session.query(Customer).get(c_id)
    return render_template('one_cust.html', cust=cust)
Exemplo n.º 34
0
def show_target(tgt_id):
    target = session.query(Target).get(tgt_id)
    return render_template('show_target.html', target=target)
Exemplo n.º 35
0
def delete_book(book_id):
    book = session.query(Book).get(book_id)
    session.delete(book)
    session.commit()
    return redirect(url_for('index'))
Exemplo n.º 36
0
def get_customer():
    return session.query(Customer).all()