def createItem(name, category, price, user_id): """Create a new catalog item Args: name (str): The name for the item. category (obj): The category object. price (str): The item price. user_id (int): The id of the creating user. Returns: Item: The created Item object. """ try: description = wikipedia.summary(name) except wikipedia.exceptions.DisambiguationError as e: description = wikipedia.summary(name + " " + category.name) i = Item(name=name, description=description, category_id=category.id, price=price, user_id=user_id) session.add(i) session.commit() print 'Item "' + name + '" added.' return i
def create_user_and_sample_Items(name, phone_num): customer = Customer(name, phone_num) cookie = Item(name + "_Cookie", 1.50) muffin = Item(name + "_Muffin", 2.00, "Carby stuff") latte = Item(name + "_Latte", 4.35, "Steamed milk over expresso") mocha = Item(name + "_Mocha", 5.00, "Steamed milk over expresso and chocolate syrup") session.add(customer) session.add(cookie) session.add(muffin) session.add(latte) session.add(mocha) session.commit() result = {"customer": customer} item_names = ['cookie', 'muffin', 'latte', 'mocha'] for name in item_names: item = session.query(Item).\ filter(func.lower(Item.name).like('%' + name + '%')).\ filter(func.lower(Item.name).like('%' + customer_name + '%')).\ scalar() result[name] = item return result
def test_user_creation_and_deletion(): print "Inside test_creation" # Create user test_user = create_in_memory_user() session.add(test_user) session.commit() print "Created user with id " + str(test_user.id) # Find user from db and check if it exists found_user = session.query(TestUser).\ filter(TestUser.id == test_user.id).scalar() print "Found user in db with id " + str(found_user.id) assert test_user.id is not None, "Created user id should not be None" assert found_user.id is not None, "User found in db should not be None" assert test_user.id == found_user.id, \ "Both created user and user found in db should have the same id" # Delete created user session.delete(found_user) session.commit() print "Deleting the found user" found_user = session.query(TestUser).\ filter(TestUser.id == test_user.id).scalar() assert found_user is None, "Once deleted the user should not be found in db" print "We were able to successfully delete the found User"
def create_user_and_sample_Items(name, phone_num): customer = Customer(name, phone_num) cookie = Item(name + "_Cookie", 1.50) muffin = Item(name + "_Muffin", 2.00, "Carby stuff") latte = Item(name + "_Latte", 4.35, "Steamed milk over expresso") mocha = Item(name + "_Mocha", 5.00, "Steamed milk over expresso and chocolate syrup") session.add(customer) session.add(cookie) session.add(muffin) session.add(latte) session.add(mocha) session.commit() result = {"customer": customer} item_names = ['cookie', 'muffin', 'latte', 'mocha'] for name in item_names: item = session.query(Item).\ filter(func.lower(Item.name).like('%' + name + '%')).\ filter(func.lower(Item.name).like('%' + customer_name + '%')).\ scalar() result[name] = item return result
def add_node(): parser = reqparse.RequestParser() parser.add_argument('tag_pid', type=str, required=True) parser.add_argument('tag_level', type=str, required=True) parser.add_argument('tag_node', type=str, required=True) parser.add_argument('tag_name', type=str, required=True) args = parser.parse_args() if len(args['tag_pid']) == 0 or len(args['tag_level']) == 0 or len( args['tag_node']) == 0: flash("不能够为空目录添加属性,请走正确渠道") return render_template('error.html') if len(args['tag_name']) == 0: flash("属性名不能为空") return admin_add(args['tag_node'], args['tag_pid'], args['tag_level']) tag = Tag() tag.tag_pid = args['tag_pid'] tag.tag_level = args['tag_level'] tag.tag_node = args['tag_node'] tag.tag_name = args['tag_name'] session.add(tag) session.commit() return admin_index()
def test_user_creation_and_deletion(): print "Inside test_creation" # Create user test_user = create_in_memory_user() session.add(test_user) session.commit() print "Created user with id " + str(test_user.id) # Find user from db and check if it exists found_user = session.query(TestUser).\ filter(TestUser.id == test_user.id).scalar() print "Found user in db with id " + str(found_user.id) assert test_user.id is not None, "Created user id should not be None" assert found_user.id is not None, "User found in db should not be None" assert test_user.id == found_user.id, \ "Both created user and user found in db should have the same id" # Delete created user session.delete(found_user) session.commit() print "Deleting the found user" found_user = session.query(TestUser).\ filter(TestUser.id == test_user.id).scalar() assert found_user is None, "Once deleted the user should not be found in db" print "We were able to successfully delete the found User"
def add_node(): parser = reqparse.RequestParser() parser.add_argument('tag_pid', type=str, required=True) parser.add_argument('tag_level', type=str, required=True) parser.add_argument('tag_node', type=str, required=True) parser.add_argument('tag_name', type=str, required=True) args = parser.parse_args() if len(args['tag_pid']) == 0 or len(args['tag_level']) == 0 or len(args['tag_node']) == 0: flash("不能够为空目录添加属性,请走正确渠道") return render_template('error.html') if len(args['tag_name']) == 0: flash("属性名不能为空") return admin_add(args['tag_node'], args['tag_pid'], args['tag_level']) tag = Tag() tag.tag_pid = args['tag_pid'] tag.tag_level = args['tag_level'] tag.tag_node = args['tag_node'] tag.tag_name = args['tag_name'] session.add(tag) session.commit() return admin_index()
def register(**kwargs): try: user = User(**kwargs) session.add(user) session.commit() token = user.get_token() except Exception as e: logger.warning( f'registration failed with errors: {e}') return {'message': str(e)}, 400 return {'access_token': token}
def post(self, **kwargs): try: user = User(**kwargs) session.add(user) session.commit() #logger.info( # f'user {user.id} created') except Exception as e: #logger.warning( # f'user creation failed: {e}') return {'message': str(e)}, 400 return user
def create(client): try: # Verify client doesn't exist # Insert client to the databasex session.add(client) session.commit() # Get a response from database and return it msg = "Client created successfully" response = { "data":msg } return response except Exception as e: print(e) session.rollback()
def createCategory(name, user_id): """Create a new item category Args: name (str): The name for the category. user_id (int): The id of the creating user. Returns: Category: The created Category object. """ c = Category(name=name, user_id=user_id) session.add(c) session.commit() print 'Category "' + name + '" created.' return c
def insert_img(self): path, img_time = meteorological_img.main() # image_file = self.img_io(path) now = datetime.now() cloud = Cloud() cloud.img_name = os.path.basename(path) cloud.img_path = path cloud.img_time = img_time cloud.created_at = now cloud.tag = "synthetic" cloud.zoom_level = 2 session.add(cloud) session.commit() print(path) print("insert")
def createUser(name, email, picture): """Create a new user Args: name (str): The name for the user. email (str): The user's email address. picture (str): The URL for the user avatar. Returns: User: The created User object. """ u = User(name=name, email=email, picture=picture) session.add(u) session.commit() print 'User "' + name + '" created.' return u
def create_a_user(base, prefix, email): name = base + prefix fullname = name + " singh" pwd = "password-" + name user = TestUser(name, fullname, pwd) if email: emails = [ name + '*****@*****.**', name + '*****@*****.**', name + '*****@*****.**' ] user.add_email(emails[0]) user.add_email(emails[1]) user.add_email(emails[2]) print "user id before commit - " + str(user.id) + "for user - " + str(user) session.add(user) session.commit() print "user id before commit - " + str(user.id) + "for user - " + str(user)
def update_area(): area = session.query(Area).filter_by(area_level=3).all() for area_child in area: parent_id = area_child.area_pid parent_line = [] while parent_id != 21: parent = session.query(Area).filter_by(area_id=parent_id).first() if parent: parent_line.append(parent.area_id) parent_id = parent.area_pid if len(parent_line) > 1: parent_line = ','.join(parent_line) area_child.parent_line = parent_line session.commit() return render_template('status.html')
def update_node(): node = session.query(Node).filter_by(nod_level=101).all() for node_child in node: parent_id = node_child.nod_pid parent_line = [] while parent_id != 21: parent = session.query(Node).filter_by(node_id=parent_id).first() if parent: parent_line.append(parent.node_id) parent_id = parent.nod_pid if len(parent_line) > 1: parent_line = ','.join(parent_line) node_child.parent_line = parent_line session.commit() return render_template('status.html')
def test_ordering_system(): dict = create_user_and_sample_Items(customer_name, customer_phone_num) customer = dict['customer'] cookie = dict['cookie'] muffin = dict['muffin'] latte = dict['latte'] mocha = dict['mocha'] # filter(lambda x: session.add(x), [customer, cookie, muffin, latte, mocha]) customer.orders.append(Order()) customer.orders.append(Order()) session.commit() customer = session.query(Customer).\ filter(func.lower(Customer.name) == func.lower(customer_name)).scalar() assert customer is not None, "Customer should be present" print "Successfully created Customer" orders = session.query(Order).filter( Order.customer_id == customer.id).all() assert len(orders) == 2, "Order size isn't accurate" print "Successfully created Orders" order1, order2 = orders[0], orders[1] order1.order_items.append(OrderItem(cookie.id, 2)) order1.order_items.append( OrderItem(latte.id, 2, "Sugar please! Lots of it, in both")) order1.order_items.append( OrderItem(mocha.id, 10, "Buying for the whole group")) order2.order_items.append(OrderItem(cookie.id, 20)) order2.order_items.append(OrderItem(muffin.id, 20, "In two boxes")) session.commit() order1_items = session.query(OrderItem).\ filter(OrderItem.order_id == order1.id).all() order2_items = session.query(OrderItem).\ filter(OrderItem.order_id == order2.id).all() assert len(order1_items) == 3, "Number of items in order1 is not accurate" assert len(order2_items) == 2, "Number of items in order2 is not accurate" print "Successfully created order items" # Test deleting an order item, has no effect on the customer, or the order, # or the items session.delete(order1_items[0]) session.commit() customer = session.query(Customer). \ filter(func.lower(Customer.name) == func.lower(customer_name)).scalar() assert customer is not None,\ "Deleting an order item should not delete the customer" orders = session.query(Order).filter( Order.customer_id == customer.id).all() assert len(orders) == 2,\ "Deleting the order item should not delete the order" order1, order2 = orders[0], orders[1] assert len(order1.order_items) == 2, \ "Deleting the order item should delete it from db" print "Successfully deleted order item" all_items = session.query(Item).\ filter(func.lower(Item.name).like('%' + customer_name + '%')).all() assert len(all_items) == 4, \ "Deleting the order item should not delete any item" # Test deleting an order, removes all the order items, but leaves the # items and customers untouched order1_id = order1.id session.delete(order1) session.commit() order_items = session.query(OrderItem).\ filter(OrderItem.order_id == order1_id).all() assert len(order_items) == 0, \ "Deleting an order should remove all its order items" customer = session.query(Customer). \ filter(func.lower(Customer.name) == func.lower(customer_name)).scalar() assert customer is not None,\ "Deleting an order should not delete the customer" orders = session.query(Order).filter( Order.customer_id == customer.id).all() assert len(orders) == 1,\ "Deleting an order item should only delete that order, no other order" print "Successfully deleted the order" # Test deleting an customer deletes all the orders, and the order items, # but leaves all the items untouched order_id = customer.orders[0].id customer_id = customer.id session.delete(customer) session.commit() customer = session.query(Customer). \ filter(func.lower(Customer.name) == func.lower(customer_name)).scalar() assert customer is None,\ "Deleting a customer should remove it from db" print "Successfully deleted the Customer" orders = session.query(Order).filter( Order.customer_id == customer_id).all() assert len(orders) == 0, "Deleting a customer should delete its orders" order_items = session.query(OrderItem).\ filter(OrderItem.order_id == order_id).all() assert len(order_items) == 0, \ "Deleting a customer should delete all its order_items" print " And the order items associated with the customer" all_items = session.query(Item). \ filter(func.lower(Item.name).like('%' + customer_name + '%')).all() assert len(all_items) == 4, \ "Deleting the order item should not delete any item" filter(lambda x: session.delete(x), all_items) session.commit() all_items = session.query(Item). \ filter(func.lower(Item.name).like('%' + customer_name + '%')).all() assert len(all_items) == 0,\ "Removing items to undo db changes during test should not fail" print "Ran test successfully to try adding/deleting orders," \ " order items and customers"
def test_ordering_system(): dict = create_user_and_sample_Items(customer_name, customer_phone_num) customer = dict['customer'] cookie = dict['cookie'] muffin = dict['muffin'] latte = dict['latte'] mocha = dict['mocha'] # filter(lambda x: session.add(x), [customer, cookie, muffin, latte, mocha]) customer.orders.append(Order()) customer.orders.append(Order()) session.commit() customer = session.query(Customer).\ filter(func.lower(Customer.name) == func.lower(customer_name)).scalar() assert customer is not None, "Customer should be present" print "Successfully created Customer" orders = session.query(Order).filter(Order.customer_id == customer.id).all() assert len(orders) == 2, "Order size isn't accurate" print "Successfully created Orders" order1, order2 = orders[0], orders[1] order1.order_items.append(OrderItem(cookie.id, 2)) order1.order_items.append(OrderItem(latte.id, 2, "Sugar please! Lots of it, in both")) order1.order_items.append(OrderItem(mocha.id, 10, "Buying for the whole group")) order2.order_items.append(OrderItem(cookie.id, 20)) order2.order_items.append(OrderItem(muffin.id, 20, "In two boxes")) session.commit() order1_items = session.query(OrderItem).\ filter(OrderItem.order_id == order1.id).all() order2_items = session.query(OrderItem).\ filter(OrderItem.order_id == order2.id).all() assert len(order1_items) == 3, "Number of items in order1 is not accurate" assert len(order2_items) == 2, "Number of items in order2 is not accurate" print "Successfully created order items" # Test deleting an order item, has no effect on the customer, or the order, # or the items session.delete(order1_items[0]) session.commit() customer = session.query(Customer). \ filter(func.lower(Customer.name) == func.lower(customer_name)).scalar() assert customer is not None,\ "Deleting an order item should not delete the customer" orders = session.query(Order).filter(Order.customer_id == customer.id).all() assert len(orders) == 2,\ "Deleting the order item should not delete the order" order1, order2 = orders[0], orders[1] assert len(order1.order_items) == 2, \ "Deleting the order item should delete it from db" print "Successfully deleted order item" all_items = session.query(Item).\ filter(func.lower(Item.name).like('%' + customer_name + '%')).all() assert len(all_items) == 4, \ "Deleting the order item should not delete any item" # Test deleting an order, removes all the order items, but leaves the # items and customers untouched order1_id = order1.id session.delete(order1) session.commit() order_items = session.query(OrderItem).\ filter(OrderItem.order_id == order1_id).all() assert len(order_items) == 0, \ "Deleting an order should remove all its order items" customer = session.query(Customer). \ filter(func.lower(Customer.name) == func.lower(customer_name)).scalar() assert customer is not None,\ "Deleting an order should not delete the customer" orders = session.query(Order).filter(Order.customer_id == customer.id).all() assert len(orders) == 1,\ "Deleting an order item should only delete that order, no other order" print "Successfully deleted the order" # Test deleting an customer deletes all the orders, and the order items, # but leaves all the items untouched order_id = customer.orders[0].id customer_id = customer.id session.delete(customer) session.commit() customer = session.query(Customer). \ filter(func.lower(Customer.name) == func.lower(customer_name)).scalar() assert customer is None,\ "Deleting a customer should remove it from db" print "Successfully deleted the Customer" orders = session.query(Order).filter(Order.customer_id == customer_id).all() assert len(orders) == 0, "Deleting a customer should delete its orders" order_items = session.query(OrderItem).\ filter(OrderItem.order_id == order_id).all() assert len(order_items) == 0, \ "Deleting a customer should delete all its order_items" print " And the order items associated with the customer" all_items = session.query(Item). \ filter(func.lower(Item.name).like('%' + customer_name + '%')).all() assert len(all_items) == 4, \ "Deleting the order item should not delete any item" filter(lambda x: session.delete(x), all_items) session.commit() all_items = session.query(Item). \ filter(func.lower(Item.name).like('%' + customer_name + '%')).all() assert len(all_items) == 0,\ "Removing items to undo db changes during test should not fail" print "Ran test successfully to try adding/deleting orders," \ " order items and customers"
def delete_user(id): query = session.query(TestUser).filter(TestUser.id == id) user = query.one() print 'About to delete', type(user), '---', user session.delete(user) session.commit()