def products_list(self): # XXX: Better to use form schema to validate params, but for simplicity # just do it manually. offset = int(self.request.params.get("offset", 0)) products = ( sess.query(Product) .join(Vendor, Product.vendor_id == Vendor.id) .order_by(Product.created_at.desc()) .offset(offset * DEFAULT_BATCH_AMOUNT) .limit(DEFAULT_BATCH_AMOUNT) ).all() resp = { "offset": offset, "filtered": {}, "data": [], } for product in products: resp["data"].append({ "id": product.id, "name": product.name, "description": product.description, "photo_url": product.photo_url, "price": float(product.price), "is_on_sale": product.is_on_sale, "target_gender": product.target_gender, "category": product.category, "created_at": product.created_at.isoformat(), "vendor_id": product.vendor_id, "vendor_name": product.vendor.name, }) return resp
def main(argv=sys.argv): if len(argv) < 2: cmd = os.path.basename(argv[0]) print("You are doing it wrong.\n" 'usage: %s <config_uri> [var=value]\n' '(example: "%s development.ini")' % (cmd, cmd) ) sys.exit(1) # Parse and merge passed args and config ini file. config_uri = argv[1] options = parse_vars(argv[2:]) settings = get_appsettings(config_uri, options=options) # Ensure we have a database url set. db_url = settings.get("sqlalchemy.url") if not db_url: print("You need to configure sqlalchemy.url to connect to postgres.") # Ensure we have a seed file to bootstrap to begin with. seedfile = settings.get("springboard.seedfile") is_seedfile_given = bool(seedfile) is_seedfile_found = os.path.isfile(seedfile) if seedfile else False if not is_seedfile_given or not is_seedfile_found: print("You need to provide a csv file for seed data.") sys.exit(1) # Drop the existing database if we already have one; if not create new one. # Either way start with a clean slate. engine = create_engine(db_url) if database_exists(engine.url): drop_database(engine.url) create_database(engine.url) # Create all tables based on defined model classes. sess.configure(bind=engine) Base.metadata.create_all(engine) # Import data from the seed file while mapping appropriate relationships. with transaction.manager: with open(seedfile) as csvfile: reader = csv.DictReader(csvfile) for row in reader: # Get or create vendor. vendor_id = row["vendor_id"] vendor = sess.query(Vendor).get(vendor_id) if not vendor: vendor = Vendor(id=vendor_id, name=row["vendor_name"]) sess.add(vendor) sess.flush() # Get or create product product_id = row["product_id"] product = sess.query(Product).get(product_id) if not product: # XXX: Ignore taxonomy hierarchy and track only the top # most level as 'category' in products table for simplicity. taxonomies_list = row["backend_taxonomy"] category = taxonomies_list.strip("{}").split(",")[0] category = category or "unassigned" product = Product(id=product_id, name=row["product_name"], description=row["product_desc"], original_price=row["original_price"], price=row["price"], is_on_sale=row["is_on_sale"], target_gender=row["gender"].strip().lower(), category=category, photo_url=row["photo_url"], created_at=row["created_at"]) product.vendor_id = vendor.id sess.add(product) sess.flush() # Create dummy user and board. # XXX: Limit the prototype app to one user, and only one board per user. user = User(username="******") sess.add(user) sess.flush() board = Board(name="2015W", description="Winter 2015 Favorites") board.user_id = user.id sess.add(board) sess.flush() print("All done!")