async def date_time_attribute(session: AsyncSession): adal = AsyncDal(session) attribute = adal.add( Attribute, slug="release-date-time", name="Release date time", type=Attribute.Type.PRODUCT, input_type=Attribute.InputType.DATE_TIME, filterable_in_storefront=True, filterable_in_dashboard=True, available_in_grid=True, ) for value in [ datetime.datetime(2020, 10, 5), datetime.datetime(2020, 11, 5), ]: adal.add( AttributeValue, attribute=attribute, name=f"{attribute.name}: {value.date()}", slug=f"{value.date()}_{attribute.id}", value=f"{value.date()}", ) await adal.commit() return attribute
async def staff(session: AsyncSession): adal = AsyncDal(session) ins = adal.add( Staff, name="admin", ) await adal.commit() return ins
async def user(session: AsyncSession): adal = AsyncDal(session) ins = adal.add( User, email="*****@*****.**", is_active=True, password="******", last_login_at=datetime.datetime.now(), first_name="alpha", last_name="go", ) await adal.commit() return ins
async def test_filtering_by_attribute( session: AsyncSession, product_type, # color_attribute, # size_attribute, # # category, # date_attribute, # date_time_attribute, # # boolean_attribute, ): adal = AsyncDal(session) assert len(await adal.all(ProductType)) == 1 ...
def shell_plus(): from IPython import embed import cProfile import pdb import_submodules("tifa.models") models = {cls.__name__: cls for cls in db.Model.__subclasses__()} dal = Dal(db.session) adal = AsyncDal(db.async_session) main = importlib.import_module("__main__") ctx = main.__dict__ ctx.update({ **models, "dal": dal, "adal": adal, "db": db, "ipdb": pdb, "cProfile": cProfile, }) embed(user_ns=ctx, banner2="", using="asyncio", colors="neutral")
async def before_request( request: Request, ): adal = AsyncDal(db.async_session) g.adal = adal if request.url.path not in ALLOW_LIST: try: authorization = request.headers.get("Authorization", None) if not authorization: raise NotAuthorized("Authorization Headers Required") token = authorization.split(" ")[1] content = decode_jwt(token) staff = await adal.get_or_404(User, json.loads(content["sub"])["user"]) g.staff = staff except NotAuthorized as e: raise e except NotFound as e: raise NotAuthorized("no such staff") except Exception: # TODO: more specific error raise NotAuthorized("token not correct")
async def product_type(session: AsyncSession, color_attribute, size_attribute): adal = AsyncDal(session) product_type = adal.add( ProductType, name="Default Type", slug="default-type", has_variants=True, is_shipping_required=True, weight=0.1, is_digital=True, ) adal.add( AttributeProduct, attribute=color_attribute, product_type=product_type, ) adal.add( AttributeProduct, attribute=size_attribute, product_type=product_type, ) await adal.commit() return product_type
async def color_attribute(session: AsyncSession): adal = AsyncDal(session) attribute = adal.add( Attribute, slug="color", name="Color", type=Attribute.Type.PRODUCT, input_type=Attribute.InputType.DROPDOWN, filterable_in_storefront=True, filterable_in_dashboard=True, available_in_grid=True, ) adal.add(AttributeValue, attribute=attribute, name="Red", slug="red", value="red") adal.add(AttributeValue, attribute=attribute, name="Blue", slug="blue", value="blue") await adal.commit() return attribute
async def size_attribute(session: AsyncSession): adal = AsyncDal(session) attribute = adal.add( Attribute, slug="size", name="Size", type=Attribute.Type.PRODUCT, input_type=Attribute.InputType.DROPDOWN, filterable_in_storefront=True, filterable_in_dashboard=True, available_in_grid=True, ) adal.add(AttributeValue, attribute=attribute, name="3XL", slug="3xl", value="3xl") adal.add(AttributeValue, attribute=attribute, name="2XL", slug="2xl", value="2xl") adal.add(AttributeValue, attribute=attribute, name="XL", slug="xl", value="xl") adal.add(AttributeValue, attribute=attribute, name="L", slug="l", value="l") await adal.commit() return attribute
async def test_user_count(session: AsyncSession, user): adal = AsyncDal(session) assert len(await adal.all(User)) == 1
async def test_staff_count(session: AsyncSession, staff): adal = AsyncDal(session) assert len(await adal.all(Staff)) == 1