-
Notifications
You must be signed in to change notification settings - Fork 0
Refector/130 email imgcnt #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,12 @@ | |
|
|
||
| from datetime import date | ||
|
|
||
| from sqlalchemy import select, update | ||
| from sqlalchemy import func, select, update | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
| from sqlalchemy.orm import selectinload | ||
|
|
||
| from app.models.ComplaintPetition import ComplaintPetition | ||
| from app.models.IssuePin import IssuePin | ||
| from app.models.LocationDepartment import LocationDepartment | ||
| from app.models.enum.ComplaintPetitionStatus import ComplaintPetitionStatus | ||
| from app.repositories.BaseRepo import BaseRepo | ||
|
|
@@ -16,6 +17,14 @@ class ComplaintPetitionRepo(BaseRepo[ComplaintPetition]): | |
| def __init__(self, session: AsyncSession) -> None: | ||
| super().__init__(session, ComplaintPetition) | ||
|
|
||
| @staticmethod | ||
| def _review_load_options(): | ||
| return ( | ||
| selectinload(ComplaintPetition.location_department).selectinload(LocationDepartment.department), | ||
| selectinload(ComplaintPetition.location_department).selectinload(LocationDepartment.location), | ||
| selectinload(ComplaintPetition.issue_pin).selectinload(IssuePin.pin), | ||
| ) | ||
|
|
||
| async def exists_by_issue_pin_and_generated_on( | ||
| self, | ||
| *, | ||
|
|
@@ -45,14 +54,47 @@ async def get_by_petition_ids(self, petition_ids: list[int]) -> list[ComplaintPe | |
| result = await self.session.execute( | ||
| select(ComplaintPetition) | ||
| .where(ComplaintPetition.petition_id.in_(petition_ids)) | ||
| .options( | ||
| selectinload(ComplaintPetition.location_department).selectinload(LocationDepartment.department), | ||
| selectinload(ComplaintPetition.location_department).selectinload(LocationDepartment.location), | ||
| selectinload(ComplaintPetition.issue_pin), | ||
| ), | ||
| .options(*self._review_load_options()), | ||
| ) | ||
| return list(result.scalars().all()) | ||
|
|
||
| async def list_for_admin( | ||
| self, | ||
| *, | ||
| status: str | None = None, | ||
| limit: int = 50, | ||
| offset: int = 0, | ||
| ) -> tuple[list[ComplaintPetition], int]: | ||
| filters = [] | ||
| if status is not None: | ||
| filters.append(ComplaintPetition.status == status) | ||
|
|
||
| count_stmt = select(func.count()).select_from(ComplaintPetition) | ||
| if filters: | ||
| count_stmt = count_stmt.where(*filters) | ||
| total_result = await self.session.execute(count_stmt) | ||
| total = int(total_result.scalar_one()) | ||
|
|
||
| list_stmt = ( | ||
| select(ComplaintPetition) | ||
| .options(*self._review_load_options()) | ||
| .order_by(ComplaintPetition.created_at.desc()) | ||
| .limit(limit) | ||
| .offset(offset) | ||
| ) | ||
| if filters: | ||
| list_stmt = list_stmt.where(*filters) | ||
|
Comment on lines
+78
to
+86
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SQLAlchemy์์ list_stmt = select(ComplaintPetition).options(*self._review_load_options())
if filters:
list_stmt = list_stmt.where(*filters)
list_stmt = (
list_stmt.order_by(ComplaintPetition.created_at.desc())
.limit(limit)
.offset(offset)
) |
||
| result = await self.session.execute(list_stmt) | ||
| return list(result.scalars().all()), total | ||
|
|
||
| async def get_by_petition_id_for_review(self, petition_id: int) -> ComplaintPetition | None: | ||
| result = await self.session.execute( | ||
| select(ComplaintPetition) | ||
| .where(ComplaintPetition.petition_id == petition_id) | ||
| .options(*self._review_load_options()), | ||
| ) | ||
| return result.scalar_one_or_none() | ||
|
|
||
| async def update_status(self, *, petition_id: int, status: str) -> bool: | ||
| result = await self.session.execute( | ||
| update(ComplaintPetition) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,8 @@ | |||||||||||||||||
| ComplaintPetitionApplyResponse, | ||||||||||||||||||
| ComplaintPetitionBulkSendItem, | ||||||||||||||||||
| ComplaintPetitionBulkSendResponse, | ||||||||||||||||||
| ComplaintPetitionReviewItem, | ||||||||||||||||||
| ComplaintPetitionReviewListResponse, | ||||||||||||||||||
| ) | ||||||||||||||||||
| from app.schemas.IssueDTO import ImageWithLocation | ||||||||||||||||||
| from app.services.ComplaintEmailService import ComplaintEmailService | ||||||||||||||||||
|
|
@@ -240,23 +242,36 @@ async def create_petition_for_issue_pin( | |||||||||||||||||
| ) | ||||||||||||||||||
| await self.complaint_petition_repo.save(petition, flush_immediately=True) | ||||||||||||||||||
|
|
||||||||||||||||||
| return ComplaintPetitionApplyResponse( | ||||||||||||||||||
| petition_id=petition.petition_id, | ||||||||||||||||||
| issue_pin_id=issue_pin.issue_pin_id, | ||||||||||||||||||
| location_department_id=location_department.location_department_id, | ||||||||||||||||||
| location_id=location_id, | ||||||||||||||||||
| department_name=location_department.department.department_name, | ||||||||||||||||||
| location_department_email=location_department.location_department_email, | ||||||||||||||||||
| generated_on=target_generated_on.isoformat(), | ||||||||||||||||||
| pdf_s3_key=petition.pdf_s3_key, | ||||||||||||||||||
| pdf_s3_url=petition.pdf_s3_url, | ||||||||||||||||||
| email_subject=petition.email_subject, | ||||||||||||||||||
| email_body=petition.email_body, | ||||||||||||||||||
| reliability_score=petition.reliability_score, | ||||||||||||||||||
| reliability_basis=petition.reliability_basis, | ||||||||||||||||||
| status=petition.status, | ||||||||||||||||||
| petition.location_department = location_department | ||||||||||||||||||
| petition.issue_pin = issue_pin | ||||||||||||||||||
| review = self._to_review_item(petition) | ||||||||||||||||||
| return ComplaintPetitionApplyResponse.model_validate(review.model_dump()) | ||||||||||||||||||
|
|
||||||||||||||||||
| async def list_for_review( | ||||||||||||||||||
| self, | ||||||||||||||||||
| *, | ||||||||||||||||||
| status: str | None = None, | ||||||||||||||||||
| limit: int = 50, | ||||||||||||||||||
| offset: int = 0, | ||||||||||||||||||
| ) -> ComplaintPetitionReviewListResponse: | ||||||||||||||||||
| rows, total = await self.complaint_petition_repo.list_for_admin( | ||||||||||||||||||
| status=status, | ||||||||||||||||||
| limit=limit, | ||||||||||||||||||
| offset=offset, | ||||||||||||||||||
| ) | ||||||||||||||||||
| return ComplaintPetitionReviewListResponse( | ||||||||||||||||||
| items=[self._to_review_item(row) for row in rows], | ||||||||||||||||||
| total=total, | ||||||||||||||||||
| limit=limit, | ||||||||||||||||||
| offset=offset, | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| async def get_for_review(self, petition_id: int) -> ComplaintPetitionReviewItem: | ||||||||||||||||||
| petition = await self.complaint_petition_repo.get_by_petition_id_for_review(petition_id) | ||||||||||||||||||
| if petition is None: | ||||||||||||||||||
| raise_business_exception(ErrorCode.NOT_FOUND, "๋ฏผ์ ์ ์ฒญ ์ด๋ ฅ์ ์ฐพ์ง ๋ชปํ์ต๋๋ค.") | ||||||||||||||||||
| return self._to_review_item(petition) | ||||||||||||||||||
|
|
||||||||||||||||||
| async def create_scheduled_petitions(self, *, default_threshold: int = _DEFAULT_TARGET_PETITION) -> dict[str, int]: | ||||||||||||||||||
| generated_on = self._today_kst() | ||||||||||||||||||
| offset = 0 | ||||||||||||||||||
|
|
@@ -538,6 +553,45 @@ def _build_pretty_attachment_filename(petition: ComplaintPetition) -> str: | |||||||||||||||||
| category = re.sub(r'[\\/:*?"<>|]+', "-", category).strip("-") or "category" | ||||||||||||||||||
| return f"๋ฏผ์์๊ฒฌ์_{date_text}_{category}_issue{petition.issue_pin_id}.pdf" | ||||||||||||||||||
|
|
||||||||||||||||||
| @staticmethod | ||||||||||||||||||
| def _to_review_item(petition: ComplaintPetition) -> ComplaintPetitionReviewItem: | ||||||||||||||||||
| location_department = petition.location_department | ||||||||||||||||||
| location_id = location_department.location_id if location_department is not None else 0 | ||||||||||||||||||
| department_name = "" | ||||||||||||||||||
| location_department_email = "" | ||||||||||||||||||
| location_region: str | None = None | ||||||||||||||||||
| if location_department is not None: | ||||||||||||||||||
| location_department_email = location_department.location_department_email | ||||||||||||||||||
| if location_department.department is not None: | ||||||||||||||||||
| department_name = location_department.department.department_name | ||||||||||||||||||
|
Comment on lines
+563
to
+566
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ฐ์ดํฐ๋ฒ ์ด์ค์์
Suggested change
|
||||||||||||||||||
| if location_department.location is not None: | ||||||||||||||||||
| location_region = location_department.location.region | ||||||||||||||||||
|
|
||||||||||||||||||
| pin_title: str | None = None | ||||||||||||||||||
| if petition.issue_pin is not None and petition.issue_pin.pin is not None: | ||||||||||||||||||
| pin_title = petition.issue_pin.pin.pin_title | ||||||||||||||||||
|
|
||||||||||||||||||
| return ComplaintPetitionReviewItem( | ||||||||||||||||||
| petition_id=petition.petition_id, | ||||||||||||||||||
| issue_pin_id=petition.issue_pin_id, | ||||||||||||||||||
| location_department_id=petition.location_department_id, | ||||||||||||||||||
| location_id=location_id, | ||||||||||||||||||
| department_name=department_name, | ||||||||||||||||||
| location_department_email=location_department_email, | ||||||||||||||||||
| generated_on=petition.generated_on.isoformat(), | ||||||||||||||||||
| pdf_s3_key=petition.pdf_s3_key, | ||||||||||||||||||
| pdf_s3_url=petition.pdf_s3_url, | ||||||||||||||||||
| email_subject=petition.email_subject, | ||||||||||||||||||
| email_body=petition.email_body, | ||||||||||||||||||
| reliability_score=petition.reliability_score, | ||||||||||||||||||
| reliability_basis=petition.reliability_basis, | ||||||||||||||||||
| status=petition.status, | ||||||||||||||||||
| created_at=petition.created_at, | ||||||||||||||||||
| updated_at=petition.updated_at, | ||||||||||||||||||
| location_region=location_region, | ||||||||||||||||||
| pin_title=pin_title, | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| async def _target_petition_for_location(self, *, location_id: int) -> int: | ||||||||||||||||||
| if location_id <= 0: | ||||||||||||||||||
| return _DEFAULT_TARGET_PETITION | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋์ผํ ๊ด๊ณ ๊ฒฝ๋ก(
ComplaintPetition.location_department)์ ๋ํด ์ฌ๋ฌ ๋ฒselectinload๋ฅผ ํธ์ถํ๋ ๋์ ,options()๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ์ ๊ด๊ณ๋ค์ ํ ๋ฒ์ ์ฒด์ด๋ํ๋ ๊ฒ์ด ๋ ๊น๋ํ๊ณ SQLAlchemy์์ ๊ถ์ฅํ๋ ๋ฐฉ์์ ๋๋ค.