Skip to content

Commit

Permalink
[22] Implement global Fonts setting in Card creation/API
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinHeist committed Sep 4, 2024
1 parent aeee01b commit db97a4b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
25 changes: 22 additions & 3 deletions app/internal/cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from sqlalchemy import and_, or_
from sqlalchemy.exc import OperationalError, PendingRollbackError
from sqlalchemy.orm import Query, Session
from sqlalchemy.orm.session import object_session

from app.database.query import get_interface
from app.database.query import get_font, get_interface
from app.dependencies import get_database, get_preferences
from app.internal.availability import get_remote_card_hash
from app.internal.episodes import refresh_episode_data
Expand Down Expand Up @@ -434,11 +435,13 @@ def resolve_card_settings(
MissingSourceImage: The required Source Image is missing.
"""

# Get effective Template for this Series and Episode
# Get effective Template(s) for this Series and Episode
preferences = get_preferences()
series = episode.series
global_template, series_template, episode_template =get_effective_templates(
series, episode, library
)

global_template_dict, series_template_dict, episode_template_dict = {},{},{}
if global_template is not None:
global_template_dict = global_template.card_properties
Expand All @@ -447,6 +450,16 @@ def resolve_card_settings(
if episode_template is not None:
episode_template_dict = episode_template.card_properties

# Determine the card type
card_type: str = TieredSettings.resolve_singular_setting(
preferences.default_card_type,
global_template_dict.get('card_type'),
series_template_dict.get('card_type'),
series.card_type,
episode_template_dict.get('card_type'),
episode.card_type,
)

# Get effective Font for this Series and Episode
global_font_dict, series_font_dict, episode_font_dict = {}, {}, {}
if episode.font:
Expand All @@ -459,9 +472,13 @@ def resolve_card_settings(
series_font_dict = series_template.font.card_properties
elif global_template and global_template.font:
global_font_dict = global_template.font.card_properties
elif preferences.default_fonts.get(card_type) is not None:
global_font_dict = get_font(
object_session(episode),
preferences.default_fonts[card_type],
).card_properties

# Resolve all settings from global -> Episode
preferences = get_preferences()
card_settings = TieredSettings.new_settings(
{'hide_season_text': False, 'hide_episode_text': False},
DefaultFont,
Expand Down Expand Up @@ -704,6 +721,8 @@ def create_episode_card(
Raises:
HTTPException: If the card settings are invalid and `raise_exc`
is True.
InvalidCardSettings: If the card settings are invalid and
`raise_exc` is True.
"""

# Resolve Card settings
Expand Down
3 changes: 2 additions & 1 deletion app/models/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Preferences:
'current_version', 'available_version', 'blacklisted_blueprints',
'advanced_scheduling', 'require_auth', 'task_crontabs',
'simplified_data_table', 'home_page_size', 'episode_data_page_size',
'stylize_unmonitored_posters', 'sources_as_table',
'stylize_unmonitored_posters', 'sources_as_table', 'default_fonts',
'card_type_directory', 'local_card_types', 'imported_blueprints',
'colorblind_mode', 'library_unique_cards', 'invalid_connections',
'home_page_table_view', 'reduced_animations', 'currently_running_sync',
Expand Down Expand Up @@ -211,6 +211,7 @@ def __initialize_defaults(self) -> None:
self.default_watched_style = 'unique'
self.default_unwatched_style = 'unique'
self.default_templates: list[int] = []
self.default_fonts: dict[str, int] = {}
self.global_extras: dict[str, dict[str, str]] = {}

self.currently_running_sync: Optional[int] = None
Expand Down
11 changes: 11 additions & 0 deletions app/routers/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def delete_font(
request: Request,
font_id: int,
db: Session = Depends(get_database),
preferences: Preferences = Depends(get_preferences),
) -> None:
"""
Delete the Font with the given ID. This also deletes the font's
Expand All @@ -219,6 +220,15 @@ def delete_font(
# Get specified Font, raise 404 if DNE
font = get_font(db, font_id, raise_exc=True)

# Delete from global setting if indicated
if font_id in preferences.default_fonts.values():
preferences.default_fonts = {
card_type: id_
for card_type, id_ in preferences.default_fonts.items()
if id_ != font_id
}
log.debug(f'{preferences.default_fonts = }')

# If Font file is specified (and exists), delete
if (font_file := font.file) is not None:
try:
Expand All @@ -233,6 +243,7 @@ def delete_font(

db.delete(font)
db.commit()
preferences.commit()


@font_router.get('/{font_id}/analysis')
Expand Down
8 changes: 6 additions & 2 deletions app/routers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import APIRouter, Body, Depends, Request
from sqlalchemy.orm import Session

from app.database.query import get_template
from app.database.query import get_font, get_template
from app.dependencies import get_database, get_preferences
from app.internal.auth import get_current_user
from app.internal.backup import list_available_backups
Expand Down Expand Up @@ -60,7 +60,11 @@ def update_global_settings(
- update_preferences: UpdatePreferences containing fields to update.
"""

# Verify all specified Templates exist
# Verify any specified Fonts/Templates exist
if (hasattr(update_preferences, 'default_fonts')
and update_preferences.default_fonts != UNSPECIFIED):
for font_id in update_preferences.default_fonts.values():
get_font(db, font_id, raise_exc=True)
if (hasattr(update_preferences, 'default_templates')
and update_preferences.default_templates != UNSPECIFIED):
for template_id in update_preferences.default_templates:
Expand Down
6 changes: 4 additions & 2 deletions app/schemas/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
PositiveInt,
conint,
constr,
validator
validator,
)

from app.schemas.base import (
Base,
ImageSource,
InterfaceType,
UpdateBase,
UNSPECIFIED
UNSPECIFIED,
)

from modules.FormatString import FormatString
Expand Down Expand Up @@ -99,6 +99,7 @@ class UpdatePreferences(UpdateBase):
default_watched_style: Style = UNSPECIFIED
default_unwatched_style: Style = UNSPECIFIED
default_templates: list[int] = UNSPECIFIED
default_fonts: dict[str, int] = UNSPECIFIED
global_extras: dict[str, dict[str, str]] = UNSPECIFIED
home_page_size: PositiveInt = UNSPECIFIED
episode_data_page_size: PositiveInt = UNSPECIFIED
Expand Down Expand Up @@ -180,6 +181,7 @@ class Preferences(Base):
default_watched_style: Style
default_unwatched_style: Style
default_templates: list[int]
default_fonts: dict[str, int]
global_extras: dict[str, dict[str, str]]
home_page_size: PositiveInt
episode_data_page_size: PositiveInt
Expand Down
2 changes: 1 addition & 1 deletion modules/ref/version_webui
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0-alpha.12.0-webui21
v2.0-alpha.12.0-webui22

0 comments on commit db97a4b

Please sign in to comment.