Skip to content

Commit 01049eb

Browse files
committed
Refactor query
1 parent b33d4f3 commit 01049eb

File tree

3 files changed

+21
-41
lines changed

3 files changed

+21
-41
lines changed

src/api/endpoints/annotate/_shared/queries/helper.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
This module contains helper functions for the annotate GET queries
33
"""
44

5-
from sqlalchemy import Select, case
5+
from sqlalchemy import Select, case, exists, select
66
from sqlalchemy.orm import joinedload
77

8+
from src.collectors.enums import URLStatus
9+
from src.db.helpers.query import exists_url, not_exists_url
10+
from src.db.models.impl.flag.url_suspended.sqlalchemy import FlagURLSuspended
811
from src.db.models.impl.url.core.enums import URLSource
912
from src.db.models.impl.url.core.sqlalchemy import URL
1013
from src.db.models.views.unvalidated_url import UnvalidatedURL
@@ -15,11 +18,7 @@
1518
def get_select() -> Select:
1619
return (
1720
Select(URL)
18-
# URL Must be unvalidated
19-
.join(
20-
UnvalidatedURL,
21-
UnvalidatedURL.url_id == URL.id
22-
)
21+
2322
.join(
2423
URLAnnotationFlagsView,
2524
URLAnnotationFlagsView.url_id == URL.id
@@ -31,6 +30,19 @@ def get_select() -> Select:
3130
)
3231

3332
def conclude(query: Select) -> Select:
33+
# Add common where conditions
34+
query = query.where(
35+
URL.status == URLStatus.OK.value,
36+
not_exists_url(
37+
FlagURLSuspended
38+
),
39+
# URL Must be unvalidated
40+
exists_url(
41+
UnvalidatedURL
42+
)
43+
)
44+
45+
3446
query = (
3547
# Add load options
3648
query.options(

src/api/endpoints/annotate/all/get/queries/core.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
from src.api.endpoints.annotate._shared.extract import extract_and_format_get_annotation_result
55
from src.api.endpoints.annotate._shared.queries import helper
66
from src.api.endpoints.annotate.all.get.models.response import GetNextURLForAllAnnotationResponse
7-
from src.collectors.enums import URLStatus
87
from src.db.models.impl.annotation.agency.user.sqlalchemy import AnnotationAgencyUser
98
from src.db.models.impl.annotation.location.user.sqlalchemy import AnnotationLocationUser
10-
from src.db.models.impl.flag.url_suspended.sqlalchemy import FlagURLSuspended
11-
from src.db.models.impl.link.batch_url.sqlalchemy import LinkBatchURL
12-
from src.db.models.impl.url.core.sqlalchemy import URL
139
from src.db.models.impl.annotation.record_type.user.user import AnnotationUserRecordType
1410
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationUserURLType
15-
from src.db.models.views.unvalidated_url import UnvalidatedURL
16-
from src.db.models.views.url_anno_count import URLAnnotationCount
17-
from src.db.models.views.url_annotations_flags import URLAnnotationFlagsView
11+
from src.db.models.impl.link.batch_url.sqlalchemy import LinkBatchURL
12+
from src.db.models.impl.url.core.sqlalchemy import URL
1813
from src.db.queries.base.builder import QueryBuilderBase
1914

2015

@@ -45,7 +40,6 @@ async def run(
4540
query = (
4641
query
4742
.where(
48-
URL.status == URLStatus.OK.value,
4943
# Must not have been previously annotated by user
5044
~exists(
5145
select(AnnotationUserURLType.url_id)
@@ -78,14 +72,6 @@ async def run(
7872
AnnotationUserRecordType.url_id == URL.id,
7973
AnnotationUserRecordType.user_id == self.user_id,
8074
)
81-
),
82-
~exists(
83-
select(
84-
FlagURLSuspended.url_id
85-
)
86-
.where(
87-
FlagURLSuspended.url_id == URL.id,
88-
)
8975
)
9076
)
9177
)

src/api/endpoints/annotate/anonymous/get/query.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
from typing import Any
21
from uuid import UUID
32

4-
from sqlalchemy import Select, func, exists, select
53
from sqlalchemy.ext.asyncio import AsyncSession
6-
from sqlalchemy.orm import joinedload
74

85
from src.api.endpoints.annotate._shared.extract import extract_and_format_get_annotation_result
6+
from src.api.endpoints.annotate._shared.queries import helper
97
from src.api.endpoints.annotate.all.get.models.response import GetNextURLForAllAnnotationResponse
108
from src.api.endpoints.annotate.anonymous.get.helpers import not_exists_anon_annotation
119
from src.api.endpoints.annotate.anonymous.get.response import GetNextURLForAnonymousAnnotationResponse
12-
from src.collectors.enums import URLStatus
13-
from src.db.helpers.query import not_exists_url
1410
from src.db.models.impl.annotation.agency.anon.sqlalchemy import AnnotationAgencyAnon
1511
from src.db.models.impl.annotation.location.anon.sqlalchemy import AnnotationLocationAnon
1612
from src.db.models.impl.annotation.record_type.anon.sqlalchemy import AnnotationAnonRecordType
1713
from src.db.models.impl.annotation.url_type.anon.sqlalchemy import AnnotationAnonURLType
18-
from src.db.models.impl.flag.url_suspended.sqlalchemy import FlagURLSuspended
1914
from src.db.models.impl.url.core.sqlalchemy import URL
20-
from src.db.models.views.unvalidated_url import UnvalidatedURL
21-
from src.db.models.views.url_anno_count import URLAnnotationCount
22-
from src.db.models.views.url_annotations_flags import URLAnnotationFlagsView
2315
from src.db.queries.base.builder import QueryBuilderBase
24-
from src.api.endpoints.annotate._shared.queries import helper
2516

2617

2718
class GetNextURLForAnonymousAnnotationQueryBuilder(QueryBuilderBase):
@@ -40,7 +31,6 @@ async def run(self, session: AsyncSession) -> GetNextURLForAnonymousAnnotationRe
4031
query = (
4132
query
4233
.where(
43-
URL.status == URLStatus.OK.value,
4434
# Must not have been previously annotated by user
4535
not_exists_anon_annotation(
4636
session_id=self.session_id,
@@ -57,14 +47,6 @@ async def run(self, session: AsyncSession) -> GetNextURLForAnonymousAnnotationRe
5747
not_exists_anon_annotation(
5848
session_id=self.session_id,
5949
anon_model=AnnotationAgencyAnon
60-
),
61-
~exists(
62-
select(
63-
FlagURLSuspended.url_id
64-
)
65-
.where(
66-
FlagURLSuspended.url_id == URL.id,
67-
)
6850
)
6951
)
7052
)

0 commit comments

Comments
 (0)