-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathquery.py
More file actions
63 lines (51 loc) · 1.92 KB
/
query.py
File metadata and controls
63 lines (51 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from sqlalchemy import Select
from starlette.exceptions import HTTPException
from starlette.status import HTTP_400_BAD_REQUEST
from src.api.endpoints.review.enums import RejectionReason
from src.db.models.impl.flag.url_validated.enums import URLType
from src.db.models.impl.flag.url_validated.sqlalchemy import FlagURLValidated
from src.db.models.impl.url.core.sqlalchemy import URL
from src.db.models.impl.url.reviewing_user import ReviewingUserURL
from src.db.queries.base.builder import QueryBuilderBase
class RejectURLQueryBuilder(QueryBuilderBase):
def __init__(
self,
url_id: int,
user_id: int,
rejection_reason: RejectionReason
):
super().__init__()
self.url_id = url_id
self.user_id = user_id
self.rejection_reason = rejection_reason
async def run(self, session) -> None:
query = (
Select(URL)
.where(URL.id == self.url_id)
)
url = await session.execute(query)
url = url.scalars().first()
validation_type: URLType
match self.rejection_reason:
case RejectionReason.INDIVIDUAL_RECORD:
validation_type = URLType.INDIVIDUAL_RECORD
case RejectionReason.BROKEN_PAGE_404:
validation_type = URLType.BROKEN_PAGE
case RejectionReason.NOT_RELEVANT:
validation_type = URLType.NOT_RELEVANT
case _:
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail="Invalid rejection reason"
)
flag_url_validated = FlagURLValidated(
url_id=self.url_id,
type=validation_type
)
session.add(flag_url_validated)
# Add rejecting user
rejecting_user_url = ReviewingUserURL(
user_id=self.user_id,
url_id=self.url_id
)
session.add(rejecting_user_url)