-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
226 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
from pydantic import BaseModel | ||
from pydantic import BaseModel, HttpUrl | ||
from typing import Generic, List, Optional, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class ApiSettings(BaseModel): | ||
auth_audience: str | ||
auth_domain: str | ||
auth_client_id: str | ||
|
||
|
||
class Pagination(BaseModel, Generic[T]): | ||
count: int | ||
next: Optional[HttpUrl] | ||
previous: Optional[HttpUrl] | ||
results: List[T] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from pydantic import BaseModel | ||
from typing import Optional | ||
from uuid import UUID | ||
|
||
|
||
class Tag(BaseModel): | ||
id: Optional[UUID] | ||
tag: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from PySide6.QtCore import QJsonDocument | ||
from typing import Dict | ||
|
||
|
||
class JsonService: | ||
@staticmethod | ||
def dict_to_json(dict: Dict): | ||
doc = QJsonDocument(dict) | ||
return doc.toJson() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from models.dto.api import Pagination | ||
from models.dto.audio import Tag | ||
from PySide6.QtNetwork import QNetworkAccessManager, QNetworkReply | ||
from services.authentication import AuthenticationService | ||
from services.logging import LoggingService, Logger | ||
from services.settings import SettingsService | ||
from typing import Callable, List | ||
from urllib.parse import urljoin | ||
|
||
|
||
class TagService: | ||
__authentication_service: AuthenticationService | ||
__logger: Logger | ||
__settings_service: SettingsService | ||
__network_access_manager: QNetworkAccessManager | ||
|
||
ENCODING = "utf-8" | ||
|
||
def __init__( | ||
self, | ||
authetication_service: AuthenticationService = None, | ||
logger: Logger = LoggingService().get_logger(__name__), | ||
settings_service: SettingsService = None, | ||
): | ||
self.__authentication_service = authetication_service | ||
self.__logger = logger | ||
self.__network_access_manager = QNetworkAccessManager() | ||
self.__settings_service = settings_service | ||
|
||
def __get_page( | ||
self, | ||
url: str, | ||
success: Callable[[List[Tag]], None], | ||
failure: Callable[[List[str]], None], | ||
tags_so_far: List[Tag], | ||
): | ||
self.__logger.info("GET Tags request", url=url) | ||
|
||
def callback(reply: QNetworkReply): | ||
self.__network_access_manager.finished.disconnect(callback) | ||
content = str(reply.readAll().data(), encoding=self.ENCODING) | ||
|
||
if reply.error() is not QNetworkReply.NoError: | ||
self.__logger.error( | ||
"GET Tags request failed", | ||
url=url, | ||
error=reply.error(), | ||
response=content, | ||
) | ||
else: | ||
self.__logger.info("GET Tags request successful", url=url) | ||
page_of_results = Pagination[Tag].model_validate_json(content) | ||
tags_so_far.extend(page_of_results.results) | ||
if page_of_results.next: | ||
self.__get_page(page_of_results.next, success, failure, tags_so_far) | ||
else: | ||
success(tags_so_far) | ||
|
||
self.__network_access_manager.finished.connect(callback) | ||
self.__network_access_manager.get( | ||
self.__authentication_service.get_authenticated_request(url) | ||
) | ||
|
||
def get_all_tags( | ||
self, success: Callable[[List[Tag]], None], failure: Callable[[List[str]], None] | ||
): | ||
url = urljoin(str(self.__settings_service.get().base_url), "/api/tag") | ||
self.__get_page(url, success, failure, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from models.dto.audio import Tag | ||
from PySide6.QtCore import QAbstractListModel, Qt | ||
from services.factory import TagService, ServiceFactory | ||
from typing import List | ||
|
||
|
||
class TagListModel(QAbstractListModel): | ||
__tags: List[Tag] | ||
__tag_service: TagService | ||
|
||
def __init__( | ||
self, | ||
*args, | ||
tags=None, | ||
tag_service: TagService = ServiceFactory().tagService(), | ||
**kwargs | ||
): | ||
super().__init__(*args, **kwargs) | ||
self.__tag_service = tag_service | ||
self.__tags = tags or [] | ||
self.refresh() | ||
|
||
def refresh(self): | ||
self.__tag_service.get_all_tags(self.__update_tags, None) | ||
|
||
def __update_tags(self, tags: List[Tag]): | ||
self.beginResetModel() | ||
self.__tags = tags | ||
self.endResetModel() | ||
|
||
def data(self, index, role): | ||
if role is Qt.DisplayRole: | ||
return self.__tags[index.row()].tag | ||
|
||
def rowCount(self, index): | ||
return len(self.__tags) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .settings import Settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from PySide6.QtWidgets import QTabWidget, QVBoxLayout, QWidget | ||
from ui.views.settings.tag import TagSettings | ||
|
||
|
||
class Settings(QWidget): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
tabs = QTabWidget(movable=True) | ||
tabs.addTab(TagSettings(), "Tags") | ||
|
||
layout = QVBoxLayout() | ||
layout.addWidget(tabs) | ||
self.setLayout(layout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from PySide6.QtWidgets import ( | ||
QHBoxLayout, | ||
QLabel, | ||
QLineEdit, | ||
QListView, | ||
QPushButton, | ||
QVBoxLayout, | ||
QWidget, | ||
) | ||
from ui.viewmodels.tag import TagListModel | ||
|
||
|
||
class TagSettings(QWidget): | ||
__add_button: QPushButton | ||
__add_text: QLineEdit | ||
__tag_list: QListView | ||
__tag_model: TagListModel | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
layout = QVBoxLayout() | ||
layout.addLayout(self.__generate_tag_list()) | ||
layout.addLayout(self.__generate_delete()) | ||
layout.addLayout(self.__generate_add()) | ||
|
||
self.setLayout(layout) | ||
|
||
def __generate_tag_list(self): | ||
layout = QHBoxLayout() | ||
|
||
self.__tag_model = TagListModel() | ||
|
||
self.__tag_list = QListView() | ||
self.__tag_list.setModel(self.__tag_model) | ||
layout.addWidget(self.__tag_list) | ||
|
||
return layout | ||
|
||
def __generate_delete(self): | ||
layout = QHBoxLayout() | ||
|
||
layout.addWidget(QLabel(""), 1) | ||
layout.addWidget(QPushButton("Delete")) | ||
|
||
return layout | ||
|
||
def __generate_add(self): | ||
layout = QHBoxLayout() | ||
|
||
self.__add_text = QLineEdit() | ||
layout.addWidget(self.__add_text, 1) | ||
|
||
self.__add_button = QPushButton("Add") | ||
layout.addWidget(self.__add_button) | ||
|
||
return layout |