Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/api/endpoints/annotate/all/get/queries/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from src.api.endpoints.annotate.all.get.models.url_type import URLTypeAnnotationSuggestion
from src.core.enums import RecordType
from src.db.models.impl.flag.url_validated.enums import URLType
from src.db.models.impl.annotation.record_type.user.user import AnnotationUserRecordType
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationUserURLType
from src.db.models.impl.annotation.record_type.user.user import AnnotationRecordTypeUser
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationURLTypeUser


def convert_user_url_type_suggestion_to_url_type_annotation_suggestion(
db_suggestions: list[AnnotationUserURLType]
db_suggestions: list[AnnotationURLTypeUser]
) -> list[URLTypeAnnotationSuggestion]:
counter: Counter[URLType] = Counter()
for suggestion in db_suggestions:
Expand All @@ -26,7 +26,7 @@ def convert_user_url_type_suggestion_to_url_type_annotation_suggestion(
return anno_suggestions

def convert_user_record_type_suggestion_to_record_type_annotation_suggestion(
db_suggestions: list[AnnotationUserRecordType]
db_suggestions: list[AnnotationRecordTypeUser]
) -> RecordTypeAnnotationResponseOuterInfo:
counter: Counter[RecordType] = Counter()
for suggestion in db_suggestions:
Expand Down
16 changes: 8 additions & 8 deletions src/api/endpoints/annotate/all/get/queries/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from src.api.endpoints.annotate.all.get.models.response import GetNextURLForAllAnnotationResponse
from src.db.models.impl.annotation.agency.user.sqlalchemy import AnnotationAgencyUser
from src.db.models.impl.annotation.location.user.sqlalchemy import AnnotationLocationUser
from src.db.models.impl.annotation.record_type.user.user import AnnotationUserRecordType
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationUserURLType
from src.db.models.impl.annotation.record_type.user.user import AnnotationRecordTypeUser
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationURLTypeUser
from src.db.models.impl.link.batch_url.sqlalchemy import LinkBatchURL
from src.db.models.impl.url.core.sqlalchemy import URL
from src.db.queries.base.builder import QueryBuilderBase
Expand Down Expand Up @@ -42,10 +42,10 @@ async def run(
.where(
# Must not have been previously annotated by user
~exists(
select(AnnotationUserURLType.url_id)
select(AnnotationURLTypeUser.url_id)
.where(
AnnotationUserURLType.url_id == URL.id,
AnnotationUserURLType.user_id == self.user_id,
AnnotationURLTypeUser.url_id == URL.id,
AnnotationURLTypeUser.user_id == self.user_id,
)
),
~exists(
Expand All @@ -66,11 +66,11 @@ async def run(
),
~exists(
select(
AnnotationUserRecordType.url_id
AnnotationRecordTypeUser.url_id
)
.where(
AnnotationUserRecordType.url_id == URL.id,
AnnotationUserRecordType.user_id == self.user_id,
AnnotationRecordTypeUser.url_id == URL.id,
AnnotationRecordTypeUser.user_id == self.user_id,
)
)
)
Expand Down
10 changes: 5 additions & 5 deletions src/api/endpoints/annotate/all/get/queries/name/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from src.db.helpers.session import session_helper as sh
from src.db.models.impl.annotation.name.suggestion.enums import NameSuggestionSource
from src.db.models.impl.annotation.name.suggestion.sqlalchemy import AnnotationNameSuggestion
from src.db.models.impl.annotation.name.user.sqlalchemy import LinkUserNameSuggestion
from src.db.models.impl.annotation.name.user.sqlalchemy import AnnotationNameUserEndorsement
from src.db.queries.base.builder import QueryBuilderBase


Expand All @@ -26,16 +26,16 @@ async def run(self, session: AsyncSession) -> NameAnnotationResponseOuterInfo:
AnnotationNameSuggestion.id.label('id'),
AnnotationNameSuggestion.suggestion.label('display_name'),
func.count(
LinkUserNameSuggestion.user_id
AnnotationNameUserEndorsement.user_id
).label('user_count'),
case(
(AnnotationNameSuggestion.source == NameSuggestionSource.HTML_METADATA_TITLE, 1),
else_=0
).label("robo_count")
)
.outerjoin(
LinkUserNameSuggestion,
LinkUserNameSuggestion.suggestion_id == AnnotationNameSuggestion.id,
AnnotationNameUserEndorsement,
AnnotationNameUserEndorsement.suggestion_id == AnnotationNameSuggestion.id,
)
.where(
AnnotationNameSuggestion.url_id == self.url_id,
Expand All @@ -45,7 +45,7 @@ async def run(self, session: AsyncSession) -> NameAnnotationResponseOuterInfo:
AnnotationNameSuggestion.suggestion,
)
.order_by(
func.count(LinkUserNameSuggestion.user_id).desc(),
func.count(AnnotationNameUserEndorsement.user_id).desc(),
AnnotationNameSuggestion.id.asc(),
)
.limit(3)
Expand Down
14 changes: 7 additions & 7 deletions src/api/endpoints/annotate/all/post/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from src.db.models.impl.annotation.name.suggestion.enums import NameSuggestionSource
from src.db.models.impl.annotation.name.suggestion.sqlalchemy import AnnotationNameSuggestion
from src.db.models.impl.flag.url_validated.enums import URLType
from src.db.models.impl.annotation.name.user.sqlalchemy import LinkUserNameSuggestion
from src.db.models.impl.annotation.name.user.sqlalchemy import AnnotationNameUserEndorsement
from src.db.models.impl.link.user_suggestion_not_found.agency.sqlalchemy import LinkUserSuggestionAgencyNotFound
from src.db.models.impl.link.user_suggestion_not_found.location.sqlalchemy import LinkUserSuggestionLocationNotFound
from src.db.models.impl.annotation.record_type.user.user import AnnotationUserRecordType
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationUserURLType
from src.db.models.impl.annotation.record_type.user.user import AnnotationRecordTypeUser
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationURLTypeUser
from src.db.templates.requester import RequesterBase


Expand All @@ -33,7 +33,7 @@ def optionally_add_record_type(
) -> None:
if rt is None:
return
record_type_suggestion = AnnotationUserRecordType(
record_type_suggestion = AnnotationRecordTypeUser(
url_id=self.url_id,
user_id=self.user_id,
record_type=rt.value
Expand All @@ -44,7 +44,7 @@ def add_relevant_annotation(
self,
url_type: URLType,
) -> None:
relevant_suggestion = AnnotationUserURLType(
relevant_suggestion = AnnotationURLTypeUser(
url_id=self.url_id,
user_id=self.user_id,
type=url_type
Expand Down Expand Up @@ -77,7 +77,7 @@ async def optionally_add_name_suggestion(
if name_info.empty:
return
if name_info.existing_name_id is not None:
link = LinkUserNameSuggestion(
link = AnnotationNameUserEndorsement(
user_id=self.user_id,
suggestion_id=name_info.existing_name_id,
)
Expand All @@ -90,7 +90,7 @@ async def optionally_add_name_suggestion(
)
self.session.add(name_suggestion)
await self.session.flush()
link = LinkUserNameSuggestion(
link = AnnotationNameUserEndorsement(
user_id=self.user_id,
suggestion_id=name_suggestion.id,
)
Expand Down
8 changes: 4 additions & 4 deletions src/api/endpoints/annotate/anonymous/get/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from src.api.endpoints.annotate.anonymous.get.response import GetNextURLForAnonymousAnnotationResponse
from src.db.models.impl.annotation.agency.anon.sqlalchemy import AnnotationAgencyAnon
from src.db.models.impl.annotation.location.anon.sqlalchemy import AnnotationLocationAnon
from src.db.models.impl.annotation.record_type.anon.sqlalchemy import AnnotationAnonRecordType
from src.db.models.impl.annotation.url_type.anon.sqlalchemy import AnnotationAnonURLType
from src.db.models.impl.annotation.record_type.anon.sqlalchemy import AnnotationRecordTypeAnon
from src.db.models.impl.annotation.url_type.anon.sqlalchemy import AnnotationURLTypeAnon
from src.db.models.impl.url.core.sqlalchemy import URL
from src.db.queries.base.builder import QueryBuilderBase

Expand All @@ -34,11 +34,11 @@ async def run(self, session: AsyncSession) -> GetNextURLForAnonymousAnnotationRe
# Must not have been previously annotated by user
not_exists_anon_annotation(
session_id=self.session_id,
anon_model=AnnotationAnonURLType
anon_model=AnnotationURLTypeAnon
),
not_exists_anon_annotation(
session_id=self.session_id,
anon_model=AnnotationAnonRecordType
anon_model=AnnotationRecordTypeAnon
),
not_exists_anon_annotation(
session_id=self.session_id,
Expand Down
8 changes: 4 additions & 4 deletions src/api/endpoints/annotate/anonymous/post/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from src.db.models.impl.annotation.location.anon.sqlalchemy import AnnotationLocationAnon
from src.db.models.impl.annotation.name.suggestion.enums import NameSuggestionSource
from src.db.models.impl.annotation.name.suggestion.sqlalchemy import AnnotationNameSuggestion
from src.db.models.impl.annotation.record_type.anon.sqlalchemy import AnnotationAnonRecordType
from src.db.models.impl.annotation.url_type.anon.sqlalchemy import AnnotationAnonURLType
from src.db.models.impl.annotation.record_type.anon.sqlalchemy import AnnotationRecordTypeAnon
from src.db.models.impl.annotation.url_type.anon.sqlalchemy import AnnotationURLTypeAnon
from src.db.models.impl.annotation.name.anon.sqlalchemy import AnnotationNameAnonEndorsement
from src.db.queries.base.builder import QueryBuilderBase

Expand All @@ -27,7 +27,7 @@ def __init__(

async def run(self, session: AsyncSession) -> None:

url_type_suggestion = AnnotationAnonURLType(
url_type_suggestion = AnnotationURLTypeAnon(
url_id=self.url_id,
url_type=self.post_info.suggested_status,
session_id=self.session_id
Expand Down Expand Up @@ -57,7 +57,7 @@ async def run(self, session: AsyncSession) -> None:
session.add(name_suggestion)

if self.post_info.record_type is not None:
record_type_suggestion = AnnotationAnonRecordType(
record_type_suggestion = AnnotationRecordTypeAnon(
url_id=self.url_id,
record_type=self.post_info.record_type,
session_id=self.session_id
Expand Down
Empty file.
131 changes: 131 additions & 0 deletions src/api/endpoints/annotate/migrate/query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
from typing import Any

Check warning on line 1 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L1 <100>

Missing docstring in public module
Raw output
./src/api/endpoints/annotate/migrate/query.py:1:1: D100 Missing docstring in public module
from uuid import UUID

from sqlalchemy import insert, select, delete
from sqlalchemy.ext.asyncio import AsyncSession

from src.db.models.impl.annotation.agency.anon.sqlalchemy import AnnotationAgencyAnon
from src.db.models.impl.annotation.agency.user.sqlalchemy import AnnotationAgencyUser
from src.db.models.impl.annotation.location.anon.sqlalchemy import AnnotationLocationAnon
from src.db.models.impl.annotation.location.user.sqlalchemy import AnnotationLocationUser
from src.db.models.impl.annotation.name.anon.sqlalchemy import AnnotationNameAnonEndorsement
from src.db.models.impl.annotation.name.user.sqlalchemy import AnnotationNameUserEndorsement
from src.db.models.impl.annotation.record_type.anon.sqlalchemy import AnnotationRecordTypeAnon
from src.db.models.impl.annotation.record_type.user.user import AnnotationRecordTypeUser
from src.db.models.impl.annotation.url_type.anon.sqlalchemy import AnnotationURLTypeAnon
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationURLTypeUser
from src.db.queries.base.builder import QueryBuilderBase


class MigrateAnonymousAnnotationsQueryBuilder(QueryBuilderBase):

Check warning on line 20 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L20 <101>

Missing docstring in public class
Raw output
./src/api/endpoints/annotate/migrate/query.py:20:1: D101 Missing docstring in public class

def __init__(

Check warning on line 22 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L22 <107>

Missing docstring in __init__
Raw output
./src/api/endpoints/annotate/migrate/query.py:22:1: D107 Missing docstring in __init__
self,
session_id: UUID,
user_id: int
):
super().__init__()
self.session_id = session_id
self.user_id = user_id

async def run(self, session: AsyncSession) -> Any:

Check warning on line 31 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L31 <102>

Missing docstring in public method
Raw output
./src/api/endpoints/annotate/migrate/query.py:31:1: D102 Missing docstring in public method
await self.migrate_agency_annotations(session)
await self.migrate_location_annotations(session)
await self.migrate_record_type_annotations(session)
await self.migrate_url_type_annotations(session)
await self.migrate_name_annotations(session)

async def migrate_agency_annotations(self, session: AsyncSession) -> None:

Check warning on line 38 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L38 <102>

Missing docstring in public method
Raw output
./src/api/endpoints/annotate/migrate/query.py:38:1: D102 Missing docstring in public method
# Copy all agency annotations from anonymous to user.
statement = insert(AnnotationAgencyUser).from_select(
["agency_id", "url_id", "user_id"],
select(
AnnotationAgencyAnon.agency_id,
AnnotationAgencyAnon.url_id,
self.user_id
).where(
AnnotationAgencyAnon.session_id == self.session_id
)
)
await session.execute(statement)
# Delete all anonymous agency annotations.
statement = delete(AnnotationAgencyAnon).where(
AnnotationAgencyAnon.session_id == self.session_id
)
await session.execute(statement)


async def migrate_location_annotations(self, session: AsyncSession) -> None:

Check warning on line 58 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L58 <102>

Missing docstring in public method
Raw output
./src/api/endpoints/annotate/migrate/query.py:58:1: D102 Missing docstring in public method

Check failure on line 58 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L58 <303>

too many blank lines (2)
Raw output
./src/api/endpoints/annotate/migrate/query.py:58:5: E303 too many blank lines (2)
# Copy all location annotations from anonymous to user.
statement = insert(AnnotationLocationUser).from_select(
['location_id', 'url_id', 'user_id'],
select(
AnnotationLocationAnon.location_id,
AnnotationLocationAnon.url_id,
self.user_id
).where(
AnnotationLocationAnon.session_id == self.session_id
)
)
await session.execute(statement)
# Delete all anonymous location annotations.
statement = delete(AnnotationLocationAnon).where(
AnnotationLocationAnon.session_id == self.session_id
)
await session.execute(statement)

async def migrate_record_type_annotations(self, session: AsyncSession) -> None:

Check warning on line 77 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L77 <102>

Missing docstring in public method
Raw output
./src/api/endpoints/annotate/migrate/query.py:77:1: D102 Missing docstring in public method
# Copy all record type annotations from anonymous to user.
statement = insert(AnnotationRecordTypeUser).from_select(
['record_type', 'url_id', 'user_id'],
select(
AnnotationRecordTypeAnon.record_type,
AnnotationRecordTypeAnon.url_id,
self.user_id
).where(
AnnotationRecordTypeAnon.session_id == self.session_id
)
)
await session.execute(statement)
# Delete all anonymous record type annotations.
statement = delete(AnnotationRecordTypeAnon).where(
AnnotationRecordTypeAnon.session_id == self.session_id
)
await session.execute(statement)

async def migrate_url_type_annotations(self, session: AsyncSession) -> None:

Check warning on line 96 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L96 <102>

Missing docstring in public method
Raw output
./src/api/endpoints/annotate/migrate/query.py:96:1: D102 Missing docstring in public method
# Copy all url type annotations from anonymous to user.
statement = insert(AnnotationURLTypeUser).from_select(
['type', 'url_id', 'user_id'],
select(
AnnotationURLTypeAnon.url_type,
AnnotationURLTypeAnon.url_id,
self.user_id
).where(
AnnotationURLTypeAnon.session_id == self.session_id
)
)
await session.execute(statement)
# Delete all anonymous url type annotations.
statement = delete(AnnotationURLTypeAnon).where(
AnnotationURLTypeAnon.session_id == self.session_id
)
await session.execute(statement)

async def migrate_name_annotations(self, session: AsyncSession) -> None:

Check warning on line 115 in src/api/endpoints/annotate/migrate/query.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/migrate/query.py#L115 <102>

Missing docstring in public method
Raw output
./src/api/endpoints/annotate/migrate/query.py:115:1: D102 Missing docstring in public method
# Copy all name annotations from anonymous to user.
statement = insert(AnnotationNameUserEndorsement).from_select(
['suggestion_id', 'user_id'],
select(
AnnotationNameAnonEndorsement.suggestion_id,
self.user_id
).where(
AnnotationNameAnonEndorsement.session_id == self.session_id
)
)
await session.execute(statement)
# Delete all anonymous name annotations.
statement = delete(AnnotationNameAnonEndorsement).where(
AnnotationNameAnonEndorsement.session_id == self.session_id
)
await session.execute(statement)
22 changes: 21 additions & 1 deletion src/api/endpoints/annotate/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from src.api.endpoints.annotate.anonymous.get.query import GetNextURLForAnonymousAnnotationQueryBuilder
from src.api.endpoints.annotate.anonymous.get.response import GetNextURLForAnonymousAnnotationResponse
from src.api.endpoints.annotate.anonymous.post.query import AddAnonymousAnnotationsToURLQueryBuilder
from src.api.endpoints.annotate.migrate.query import MigrateAnonymousAnnotationsQueryBuilder
from src.api.shared.models.message_response import MessageResponse
from src.core.core import AsyncCore
from src.db.queries.implementations.anonymous_session import MakeAnonymousSessionQueryBuilder
from src.security.dtos.access_info import AccessInfo
Expand Down Expand Up @@ -113,6 +115,23 @@
url_id=anno_url_id
)

@annotate_router.post('/migrate')
async def migrate_annotations_to_user(
async_core: AsyncCore = Depends(get_async_core),
access_info: AccessInfo = Depends(get_standard_user_access_info),
session_id: UUID = Query(description="The session id of the anonymous user")
) -> MessageResponse:
"""Migrate annotations from an anonymous session to a user's account."""
await async_core.adb_client.run_query_builder(
MigrateAnonymousAnnotationsQueryBuilder(
session_id=session_id,
user_id=access_info.user_id
)
)
return MessageResponse(
message="Annotations migrated successfully."
)

@annotate_router.get("/suggestions/agencies/{url_id}")
async def get_agency_suggestions(
url_id: int,
Expand All @@ -125,4 +144,5 @@
url_id=url_id,
location_id=location_id
)
)
)

Check warning on line 148 in src/api/endpoints/annotate/routes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/api/endpoints/annotate/routes.py#L148 <391>

blank line at end of file
Raw output
./src/api/endpoints/annotate/routes.py:148:1: W391 blank line at end of file
Loading