Skip to content

Commit

Permalink
Loading relationships for repository products
Browse files Browse the repository at this point in the history
  • Loading branch information
bklvsky committed May 16, 2024
1 parent 43ccd9b commit 7fdada9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 93 deletions.
105 changes: 68 additions & 37 deletions alws/crud/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy import or_
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.orm import selectinload
from sqlalchemy.orm import joinedload, selectinload
from sqlalchemy.sql.expression import func

from alws import models
Expand All @@ -15,6 +15,7 @@
from alws.crud.user import get_user
from alws.dramatiq import perform_product_modification
from alws.errors import DataNotFoundError, PermissionDenied, ProductError
from alws.models import Build, Product, Repository, Team, UserRole
from alws.perms import actions
from alws.perms.authorization import can_perform
from alws.schemas.product_schema import ProductCreate
Expand Down Expand Up @@ -58,9 +59,7 @@ async def create_product(
if teams:
team = teams[0]
else:
team_payload = TeamCreate(
team_name=team_name, user_id=payload.owner_id
)
team_payload = TeamCreate(team_name=team_name, user_id=payload.owner_id)
team = await create_team(db, team_payload, flush=True)
team_roles = await create_team_roles(db, team_name)

Expand All @@ -84,20 +83,18 @@ async def create_product(

for platform in product.platforms:
platform_name = platform.name.lower()
repo_tasks.extend(
(
create_product_repo(
pulp_client,
product.name,
owner.username,
platform_name,
arch,
is_debug,
)
for arch in platform.arch_list
for is_debug in (True, False)
repo_tasks.extend((
create_product_repo(
pulp_client,
product.name,
owner.username,
platform_name,
arch,
is_debug,
)
)
for arch in platform.arch_list
for is_debug in (True, False)
))
repo_tasks.append(
create_product_repo(
pulp_client,
Expand Down Expand Up @@ -175,18 +172,12 @@ def generate_query(count=False):
selectinload(models.Product.roles).selectinload(
models.UserRole.actions
),
selectinload(models.Product.team).selectinload(
models.Team.owner
),
selectinload(models.Product.team).selectinload(models.Team.owner),
selectinload(models.Product.team)
.selectinload(models.Team.roles)
.selectinload(models.UserRole.actions),
selectinload(models.Product.team).selectinload(
models.Team.members
),
selectinload(models.Product.team).selectinload(
models.Team.products
),
selectinload(models.Product.team).selectinload(models.Team.members),
selectinload(models.Product.team).selectinload(models.Team.products),
)
)
if count:
Expand All @@ -205,9 +196,7 @@ def generate_query(count=False):
if page_number:
return {
'products': (await db.execute(generate_query())).scalars().all(),
'total_products': (
await db.execute(generate_query(count=True))
).scalar(),
'total_products': (await db.execute(generate_query(count=True))).scalar(),
'current_page': page_number,
}
if product_id or product_name:
Expand Down Expand Up @@ -241,12 +230,10 @@ async def remove_product(
.join(models.BuildTask)
.where(
models.Build.team_id == db_product.team_id,
models.BuildTask.status.in_(
[
BuildTaskStatus.IDLE,
BuildTaskStatus.STARTED,
]
),
models.BuildTask.status.in_([
BuildTaskStatus.IDLE,
BuildTaskStatus.STARTED,
]),
)
)
)
Expand Down Expand Up @@ -300,8 +287,7 @@ async def modify_product(
raise DataNotFoundError(f"User={user_id} doesn't exist")
if not can_perform(db_product, db_user, actions.ReleaseToProduct.name):
raise PermissionDenied(
'User has no permissions '
f'to modify the product "{db_product.name}"'
'User has no permissions ' f'to modify the product "{db_product.name}"'
)

db_build = await db.execute(
Expand Down Expand Up @@ -338,3 +324,48 @@ async def modify_product(
raise ProductError(error_msg)

perform_product_modification.send(db_build.id, db_product.id, modification)


async def get_repo_product(
session: AsyncSession,
repository: str,
):
product_relationships = (
selectinload(Product.owner),
selectinload(Product.roles).selectinload(UserRole.actions),
selectinload(Product.team)
.selectinload(Team.roles)
.selectinload(UserRole.actions),
)
if repository.endswith("br"):
build = (
(
await session.execute(
select(Build)
.filter(Build.repos.any(Repository.name.ilike(f'%{repository}')))
.options(
joinedload(Build.team)
.joinedload(Team.products)
.options(*product_relationships)
)
)
)
.scalars()
.first()
)
if build:
return build.team.products[0]
return None
return (
(
await session.execute(
select(Product)
.filter(
Product.repositories.any(Repository.name.ilike(f'%{repository}'))
)
.options(*product_relationships)
)
)
.scalars()
.first()
)
54 changes: 5 additions & 49 deletions alws/routers/uploads.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import re
import typing

from fastapi import APIRouter, Depends, Form, UploadFile
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.orm import selectinload

from alws.auth import get_current_user
from alws.crud.products import get_products
from alws.crud import user as user_crud
from alws.crud import products as products_crud
from alws.dependencies import get_db
from alws.errors import PermissionDenied
from alws.models import Build, Product, Repository, User
from alws.models import User
from alws.perms import actions
from alws.perms.authorization import can_perform
from alws.utils.uploader import MetadataUploader
Expand All @@ -22,48 +20,6 @@
)


async def get_repo_product(
session: AsyncSession,
repository: str,
):
if repository.endswith("br"):
build = (
(
await session.execute(
select(Build).filter(
Build.repos.any(Repository.name.ilike(f'%{repository}'))
)
)
)
.scalars()
.first()
)
if build:
return (
(
await session.execute(
select(Product).filter(Product.team_id == build.team_id)
)
)
.scalars()
.first()
)
else:
return (
(
await session.execute(
select(Product).filter(
Product.repositories.any(
Repository.name.ilike(f'%{repository}')
)
)
)
)
.scalars()
.first()
)


@router.post("/upload_repometada/")
async def upload_repometada(
modules: typing.Optional[UploadFile] = None,
Expand All @@ -73,10 +29,10 @@ async def upload_repometada(
user: User = Depends(get_current_user),
):
msg = ""
user = await user_crud.get_user(session, user_id=user.id)
uploader = MetadataUploader(session, repository)
repo_product = await get_repo_product(session, repository)
repo_product = await products_crud.get_repo_product(session, repository)
if not repo_product:
print(f"couldn't find a product or build repository {repository}")
return {"error": f"couldn't find a product or build repository {repository}"}
if not can_perform(repo_product, user, actions.ReleaseToProduct.name):
raise PermissionDenied(
Expand Down
15 changes: 8 additions & 7 deletions tests/fixtures/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ async def func(
for module in _index.iter_modules():
pulp_href = get_repo_href()
db_module = models.RpmModule(
name=module.name,
stream=module.stream,
context=module.context,
arch=module.arch,
version=str(module.version),
pulp_href=pulp_href,
)
name=module.name,
stream=module.stream,
context=module.context,
arch=module.arch,
version=str(module.version),
pulp_href=pulp_href,
)
db_modules.append(db_module)
module_hrefs.append(pulp_href)
return db_modules, module_hrefs, defaults_hrefs

monkeypatch.setattr(MetadataUploader, "upload_rpm_modules", func)

0 comments on commit 7fdada9

Please sign in to comment.