def show_user_home(user_id): """Using the unique user id number, display this user's profile to anyone that is not logged in, or is not this user. If the current user matches this ID, then show them their own user dashboard. """ # Try to lookup the user id in the database try: this_user = session.query(User).filter_by(id=user_id).one() # ... if no result is found route to the main page with a flash except NoResultFound: flash("This user does not exist.") return redirect(url_for('show_home')) # However, if no exceptions are raised then proceed as normal by # getting the user's disc collection and makers else: list_of_makers = session.query(Manufacturer).all() this_users_discs = session.query(Disc).filter_by(user_id=user_id).all() this_users_makers = session.query( Manufacturer).filter_by(user_id=user_id).all() # Check if this is the user's own profile if user_id == login_session.get('user_id'): return render_template('user.html', user_info=this_user, discs=this_users_discs, makers=list_of_makers, this_makers=this_users_makers) else: return render_template('public_user.html', user_info=this_user, discs=this_users_discs, makers=list_of_makers, this_makers=this_users_makers)
def show_user_home(user_id): """Using the unique user id number, display this user's profile to anyone that is not logged in, or is not this user. If the current user matches this ID, then show them their own user dashboard. """ # Try to lookup the user id in the database try: this_user = session.query(User).filter_by(id=user_id).one() # ... if no result is found route to the main page with a flash except NoResultFound: flash("This user does not exist.") return redirect(url_for('show_home')) # However, if no exceptions are raised then proceed as normal by # getting the user's disc collection and makers else: list_of_makers = session.query(Manufacturer).all() this_users_discs = session.query(Disc).filter_by(user_id=user_id).all() this_users_makers = session.query(Manufacturer).filter_by( user_id=user_id).all() # Check if this is the user's own profile if user_id == login_session.get('user_id'): return render_template('user.html', user_info=this_user, discs=this_users_discs, makers=list_of_makers, this_makers=this_users_makers) else: return render_template('public_user.html', user_info=this_user, discs=this_users_discs, makers=list_of_makers, this_makers=this_users_makers)
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 view(): context = {} max_id = session.query(func.max(Cloud.id)).scalar() cloud_img_path = session.query(Cloud.img_path).filter(Cloud.id == max_id).scalar() # context["cloud_img_path"] = cloud_img_path.split("./static/")[1] context["cloud_img_path"] = cloud_img_path print(context["cloud_img_path"]) return render_template("test.html", **context)
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 show_discs(disc_type): """Show a list of all discs of a certain type. So far only five types of disc are supported. Those five types are stored in the DISCTYPES global variable. """ if disc_type in DISCTYPES: discs_by_type = session.query( Disc).filter_by(disc_type=disc_type).all() list_of_makers = session.query(Manufacturer).all() return render_template("discsbytype.html", makers=list_of_makers, disc_type=disc_type, discs=discs_by_type) else: return ("%s is not a type of disc. Choose" "from this list %s" % (disc_type, DISCTYPES))
def show_discs(disc_type): """Show a list of all discs of a certain type. So far only five types of disc are supported. Those five types are stored in the DISCTYPES global variable. """ if disc_type in DISCTYPES: discs_by_type = session.query(Disc).filter_by( disc_type=disc_type).all() list_of_makers = session.query(Manufacturer).all() return render_template("discsbytype.html", makers=list_of_makers, disc_type=disc_type, discs=discs_by_type) else: return ("%s is not a type of disc. Choose" "from this list %s" % (disc_type, DISCTYPES))
def show_maker_all(maker): """Show Information about this disc manufacturer, and a list of all discs made by this manufacturer. """ try: manufacturer = session.query(Manufacturer).filter_by(id=maker).one() except NoResultFound: flash("Manufacturer does not exist.") return redirect(url_for('show_home')) else: list_of_makers = session.query(Manufacturer).all() list_all_by_maker = session.query(Disc).filter_by( manufacturer_id=maker).all() return render_template("makerAll.html", makers=list_of_makers, listofAllByMaker=list_all_by_maker, maker=manufacturer)
def show_maker_all(maker): """Show Information about this disc manufacturer, and a list of all discs made by this manufacturer. """ try: manufacturer = session.query(Manufacturer).filter_by(id=maker).one() except NoResultFound: flash("Manufacturer does not exist.") return redirect(url_for('show_home')) else: list_of_makers = session.query(Manufacturer).all() list_all_by_maker = session.query( Disc).filter_by(manufacturer_id=maker).all() return render_template("makerAll.html", makers=list_of_makers, listofAllByMaker=list_all_by_maker, maker=manufacturer)
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 get_all(): try: clients = session.query(Client).all() if len(clients) == 0: return "No hay clientes registrados" elif len(clients) > 0: clients = [client.to_dict() for client in clients] return clients except Exception as e: print(e)
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 show_tag(node_id): node_title = Node.get_node(node_id) rnt = {} tags = session.query(Tag).filter_by(tag_node=node_id).filter_by(tag_level=1).all() rnt[node_id] = tags for i in tags: two = i.get_tag_child() if two: rnt[i.tag_id] = two return render_template('/admin/show_tag.html', node_title=node_title, start=node_id, rnt=rnt)
def index(): rnt = {} nodes = session.query(Node).filter_by(nod_pid=21).all() rnt[21] = nodes lists = [21] for x in lists: for i in rnt[x]: two = i.get_node_child() if two: rnt[i.node_id] = two lists.append(i.node_id) return render_template('index.html', start=21, rnt=rnt)
def admin_index(): rnt = {} nodes = session.query(Node).filter_by(nod_pid=21).all() rnt[21] = nodes lists = [21] for x in lists: for i in rnt[x]: two = i.get_node_child() if two: rnt[i.node_id] = two lists.append(i.node_id) return render_template('admin/index.html', start=21, rnt=rnt)
def get_all(self): # Userテーブルのnameカラムをすべて取得 clouds = session.query(Cloud).all() data = [] for cloud in clouds: data.append({ "id": cloud.id, "img_name": cloud.img_name, "img_path": cloud.img_path, "created_at": cloud.created_at, "tag": cloud.tag, "zoom_level": cloud.zoom_level, }) return data
def show_disc(disc_id): """ Show details about a unique disc on the site. Because any user can submit a description of their unique disc, the unique id from the 'disc' table will be used to pull the first record matching the disc_id from the URL. """ # Try to lookup the disc in the database try: this_disc = session.query(Disc).filter_by(id=disc_id).one() # ... if no result is found set this_disc to None except NoResultFound: this_disc = None # However, if no exceptions are raised then proceed as normal by # getting the disc owner info else: disc_owner = get_user_info(this_disc.user_id) # Verify this disc exists AND the currently logged in user owns the disc. # If so show the disc owner page which includes edit/delete links. list_of_makers = session.query(Manufacturer).all() if this_disc and this_disc.user_id == login_session.get('user_id'): return render_template('disc.html', makers=list_of_makers, disc=this_disc, disc_owner=disc_owner, UPLOAD_FOLDER=UPLOAD_FOLDER) # If the disc exists but the user is not the owner, show the public # disc page which does not include edit/delete. elif this_disc: return render_template('public_disc.html', makers=list_of_makers, disc=this_disc, disc_owner=disc_owner, UPLOAD_FOLDER=UPLOAD_FOLDER) # Finally if the disc doesn't exist, route to main with a flash else: flash("Disc does not exist.") return redirect(url_for('show_home'))
def show_tag(node_id): node_title = Node.get_node(node_id) rnt = {} tags = session.query(Tag).filter_by(tag_node=node_id).filter_by( tag_level=1).all() rnt[node_id] = tags for i in tags: two = i.get_tag_child() if two: rnt[i.tag_id] = two return render_template('/admin/show_tag.html', node_title=node_title, start=node_id, rnt=rnt)
def get_node_child(self): return session.query(Node).filter_by(nod_pid=self.node_id).all()
def get_node(id): return session.query(Node).filter_by(node_id=id).first()
def get_area_child(self): return session.query(Area).filter_by(area_pid=self.area_id).all()
def get_node_parent(parent_line): if type(parent_line) == list or type(parent_line) == tuple: return session.query(Node).filter(Node.node_id.in_(parent_line)).order_by(asc(Node.nod_level)).all()
def get_tag_child(self): return session.query(Tag).filter_by(tag_pid=self.tag_id).all()
def find_all_users(): r = session.query(TestUser).all() for x in r: print x
def show_home(): """Main page will provide different experience for the user if they are currently logged in or not. """ list_of_makers = session.query(Manufacturer).all() return render_template('main.html', makers=list_of_makers)
def get_area(area_id): return session.query(Area).filter_by(area_id=area_id).first()
def get_node_parent(parent_line): if type(parent_line) == list or type(parent_line) == tuple: return session.query(Node).filter( Node.node_id.in_(parent_line)).order_by(asc( Node.nod_level)).all()
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 get_topic(tpc_id): return session.query(Topic).get(tpc_id)
def list_all_users(): q = session.query(TestUser) print q.all()
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()
def get_user(user_id): return session.query(User).get(user_id)
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 get_area_parents(parent_line): if type(parent_line) == list or type(parent_line) == tuple: return session.query(Area).filter(Area.area_id.in_(parent_line)).order_by(asc(Area.area_level)).all()
def get_latest_img_path(self): max_id = session.query(func.max(Cloud.id)).scalar() cloud_img_path = (session.query( Cloud.img_path).filter(Cloud.id == max_id).scalar()) return cloud_img_path
def get_latest_img_time(self): max_id = session.query(func.max(Cloud.id)).scalar() result = session.query( Cloud.img_time).filter(Cloud.id == max_id).scalar() cloud_img_time = result.strftime("%Y年%m月%d日%H時%M分") return cloud_img_time
def get_area_parents(parent_line): if type(parent_line) == list or type(parent_line) == tuple: return session.query(Area).filter( Area.area_id.in_(parent_line)).order_by(asc( Area.area_level)).all()