mirror of
https://github.com/aquatix/digimarks.git
synced 2025-12-06 20:55:10 +01:00
Typing and docstring improvements
This commit is contained in:
@@ -2,20 +2,24 @@
|
|||||||
|
|
||||||
|
|
||||||
class BookmarkNotFound(Exception):
|
class BookmarkNotFound(Exception):
|
||||||
def __init__(self, message='Bookmark not found'):
|
"""A bookmark was not found."""
|
||||||
|
|
||||||
|
def __init__(self, message: str ='Bookmark not found'):
|
||||||
"""Initialise the exception.
|
"""Initialise the exception.
|
||||||
|
|
||||||
:param str message: The message for the exception
|
:param str message: The message for the exception
|
||||||
"""
|
"""
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
self.message = message
|
self.message: str = message
|
||||||
|
|
||||||
|
|
||||||
class BookmarkAlreadyExists(Exception):
|
class BookmarkAlreadyExists(Exception):
|
||||||
def __init__(self, message='Bookmark already exists'):
|
"""A bookmark already exists for this URL and this user."""
|
||||||
|
|
||||||
|
def __init__(self, message: str ='Bookmark already exists'):
|
||||||
"""Initialise the exception.
|
"""Initialise the exception.
|
||||||
|
|
||||||
:param str message: The message for the exception
|
:param str message: The message for the exception
|
||||||
"""
|
"""
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
self.message = message
|
self.message: str = message
|
||||||
|
|||||||
@@ -299,7 +299,7 @@ async def list_bookmarks_for_tag_for_user(
|
|||||||
tag_key: str,
|
tag_key: str,
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
"""List all tags in use by the user."""
|
"""List all tags in use by the user."""
|
||||||
logger.info('List bookmarks for tag %s user %s', user_key)
|
logger.info('List bookmarks for tag "%s" by user %s', tag_key, user_key)
|
||||||
return await tags_service.list_bookmarks_for_tag_for_user(session, user_key, tag_key)
|
return await tags_service.list_bookmarks_for_tag_for_user(session, user_key, tag_key)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Contains the bookmarks administration, users, tags, public tags and more.
|
|||||||
|
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import Optional, Type, TypeVar
|
from typing import TypeVar
|
||||||
|
|
||||||
from pydantic import AnyUrl, computed_field
|
from pydantic import AnyUrl, computed_field
|
||||||
from sqlmodel import AutoString, Field, SQLModel
|
from sqlmodel import AutoString, Field, SQLModel
|
||||||
@@ -16,8 +16,6 @@ DEFAULT_THEME = 'freshgreen'
|
|||||||
class User(SQLModel, table=True):
|
class User(SQLModel, table=True):
|
||||||
"""User account."""
|
"""User account."""
|
||||||
|
|
||||||
__tablename__ = 'user'
|
|
||||||
|
|
||||||
id: int = Field(primary_key=True)
|
id: int = Field(primary_key=True)
|
||||||
username: str
|
username: str
|
||||||
key: str
|
key: str
|
||||||
@@ -28,23 +26,23 @@ class User(SQLModel, table=True):
|
|||||||
class Visibility:
|
class Visibility:
|
||||||
"""Options for visibility of an object."""
|
"""Options for visibility of an object."""
|
||||||
|
|
||||||
VISIBLE = 0
|
VISIBLE: int = 0
|
||||||
DELETED = 1
|
DELETED: int = 1
|
||||||
HIDDEN = 2
|
HIDDEN: int = 2
|
||||||
|
|
||||||
|
|
||||||
# Type var used for building custom types for the DB
|
# Type var used for building custom types for the DB
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
|
||||||
def build_custom_type(internal_type: Type[T]) -> Type[AutoString]:
|
def build_custom_type(internal_type: type[T]) -> type[AutoString]:
|
||||||
"""Create a type that is compatible with the database.
|
"""Create a type that is compatible with the database.
|
||||||
|
|
||||||
Based on https://github.com/fastapi/sqlmodel/discussions/847
|
Based on https://github.com/fastapi/sqlmodel/discussions/847
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class CustomType(AutoString):
|
class CustomType(AutoString):
|
||||||
def process_bind_param(self, value, dialect) -> Optional[str]:
|
def process_bind_param(self, value, dialect) -> str | None:
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -57,7 +55,7 @@ def build_custom_type(internal_type: Type[T]) -> Type[AutoString]:
|
|||||||
|
|
||||||
return str(value)
|
return str(value)
|
||||||
|
|
||||||
def process_result_value(self, value, dialect) -> Optional[T]:
|
def process_result_value(self, value, dialect) -> T | None:
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -69,8 +67,6 @@ def build_custom_type(internal_type: Type[T]) -> Type[AutoString]:
|
|||||||
class Bookmark(SQLModel, table=True):
|
class Bookmark(SQLModel, table=True):
|
||||||
"""Bookmark object."""
|
"""Bookmark object."""
|
||||||
|
|
||||||
__tablename__ = 'bookmark'
|
|
||||||
|
|
||||||
id: int = Field(primary_key=True)
|
id: int = Field(primary_key=True)
|
||||||
user_key: str = Field(foreign_key='user.key', nullable=False)
|
user_key: str = Field(foreign_key='user.key', nullable=False)
|
||||||
title: str = Field(default='', nullable=False)
|
title: str = Field(default='', nullable=False)
|
||||||
@@ -93,7 +89,7 @@ class Bookmark(SQLModel, table=True):
|
|||||||
|
|
||||||
@computed_field
|
@computed_field
|
||||||
@property
|
@property
|
||||||
def tag_list(self) -> list:
|
def tag_list(self) -> list[str]:
|
||||||
"""The tags but as a proper list."""
|
"""The tags but as a proper list."""
|
||||||
if self.tags:
|
if self.tags:
|
||||||
return self.tags.split(',')
|
return self.tags.split(',')
|
||||||
@@ -104,8 +100,6 @@ class Bookmark(SQLModel, table=True):
|
|||||||
class PublicTag(SQLModel, table=True):
|
class PublicTag(SQLModel, table=True):
|
||||||
"""Public tag object."""
|
"""Public tag object."""
|
||||||
|
|
||||||
__tablename__ = 'publictag'
|
|
||||||
|
|
||||||
id: int = Field(primary_key=True)
|
id: int = Field(primary_key=True)
|
||||||
tag_key: str
|
tag_key: str
|
||||||
user_key: str = Field(foreign_key='user.key')
|
user_key: str = Field(foreign_key='user.key')
|
||||||
|
|||||||
@@ -53,7 +53,10 @@ def clean_tags(tags_list: list) -> list[str]:
|
|||||||
|
|
||||||
|
|
||||||
def list_tags_for_bookmarks(bookmarks: Sequence[Bookmark]) -> list[str]:
|
def list_tags_for_bookmarks(bookmarks: Sequence[Bookmark]) -> list[str]:
|
||||||
"""Generate a unique list of the tags from the list of bookmarks."""
|
"""Generate a unique list of the tags from the list of bookmarks.
|
||||||
|
|
||||||
|
:param Sequence[Bookmark] bookmarks: List of bookmarks to create the list of tags from
|
||||||
|
"""
|
||||||
tags = []
|
tags = []
|
||||||
for bookmark in bookmarks:
|
for bookmark in bookmarks:
|
||||||
tags += bookmark.tag_list
|
tags += bookmark.tag_list
|
||||||
|
|||||||
Reference in New Issue
Block a user