def model2user(model: User, db) -> UserInfo: """Creates API for for User model.""" # convert all the purcahes to the list of product slugs products: List[Product] = get_user_products(db, model) product_slugs: List[str] = list(map(lambda x: x.slug, products)) return UserInfo(name=model.name, email=model.email, disabled=model.disabled, products=product_slugs)
async def user_get_products(user: User = Depends(get_current_active_user), db: Session = Depends(db_session)): """Returns list products.""" result: Dict[int, Group] = {} products: List[Product] = get_user_products(db, user) for product in products: # type: ignore product_group = product.group if product_group.id not in result: result[product_group.id] = model2group_nv(product_group) result[product_group.id].products.append(model2product(product)) return list(result.values())
async def download_product( product_slug: str, ext: str, user: User = Depends(get_current_user_by_cookie), db: Session = Depends(db_session) ) -> FileResponse: """Download a product file if user have bought it.""" # Check if product exists product = db.query(Product).filter(Product.slug == product_slug).first() if not product: raise HTTPException(status_code=404, detail="No product found") # Has the user bought it before? if product not in get_user_products(db, user): raise HTTPException(status_code=403, detail="Doesn't belong to you") return FileResponse(join(ROOT, product_slug, product_slug + "." + ext))