summaryrefslogtreecommitdiff
path: root/kodereviewer/data.py
diff options
context:
space:
mode:
Diffstat (limited to 'kodereviewer/data.py')
-rw-r--r--kodereviewer/data.py115
1 files changed, 91 insertions, 24 deletions
diff --git a/kodereviewer/data.py b/kodereviewer/data.py
index 7d3b73a..eb86097 100644
--- a/kodereviewer/data.py
+++ b/kodereviewer/data.py
@@ -1,8 +1,9 @@
from datetime import datetime
from enum import Enum
-from typing import Any, Optional
+import json
+from typing import Any, Optional, Self
-from PySide6.QtCore import QObject, QSettings, Signal, Slot, Property, qDebug
+from PySide6.QtCore import QByteArray, QObject, QSettings, Signal, Slot, Property, qDebug
from PySide6.QtNetwork import QHttpHeaders, QNetworkAccessManager, QNetworkReply, QNetworkRequestFactory
from PySide6.QtQml import QmlElement
@@ -29,6 +30,23 @@ class Label(QObject):
self.description = data['description']
+class Comment(QObject):
+ body: str
+ reactions: dict[str, int]
+
+ created_at: datetime
+ updated_at: datetime
+
+ user: User
+
+ def __init__(self, data: dict[str, Any]):
+ super().__init__()
+ self.body = data['body']
+ self.reactions = data['reactions']
+ self.created_at = datetime.fromisoformat(data['created_at'])
+ self.updated_at = datetime.fromisoformat(data['updated_at'])
+ self.user = User(data['user'])
+
class State(Enum):
OPEN = 'open'
@@ -38,33 +56,82 @@ class State(Enum):
class PullRequest(QObject):
- number: int
- title: str
- state: State
- url: str
- body: str | None
- created_at: datetime
- updated_at: datetime
+ _number: int
+ _title: str
+ _state: State
+ _url: str
+ _body: str | None
+ _created_at: datetime
+ _updated_at: datetime
- user: User
- assignee: User | None
- labels: list[str]
+ _user: User
+ _assignee: User | None
+ _labels: list[Label]
+ _comments: list[Comment]
+
+ commentsLoaded = Signal()
def __init__(self, data: dict[str, Any]):
super().__init__()
- self.number = data['number']
- self.state = data['state']
- self.title = data['title']
- self.url = data['html_url']
- self.body = data['body']
- self.created_at = datetime.fromisoformat(data['created_at'])
- self.updated_at = datetime.fromisoformat(data['updated_at'])
-
- self.user = User(data['user'])
- self.assignee = None
+ self._number = data['number']
+ self._state = data['state']
+ self._title = data['title']
+ self._url = data['html_url']
+ self._body = data['body']
+ self._created_at = datetime.fromisoformat(data['created_at'])
+ self._updated_at = datetime.fromisoformat(data['updated_at'])
+
+ self._user = User(data['user'])
+ self._assignee = None
if data['assignee'] is not None:
- self.assignee = User(data['assignee'])
+ self._assignee = User(data['assignee'])
+
+ self._labels = [Label(label) for label in data['labels']]
+
+ # At first this is empty until it's updated with a request
+ self._comments = []
+
+ def __eq__(self, other: object) -> bool:
+ if isinstance(other, PullRequest):
+ return self._number == other._number
+ return False
+
+ def __str__(self) -> str:
+ return self._title
+
+ @Property(int, constant=True)
+ def number(self) -> int:
+ return self._number
+
+ @Property(str, constant=True)
+ def title(self) -> str:
+ return self._title
+
+ @Property(str, constant=True)
+ def state(self) -> str:
+ return self._state.value
+
+ @Property(str, constant=True)
+ def url(self) -> str:
+ return self._url
+
+ @Property(str, constant=True)
+ def body(self) -> str:
+ if self._body is not None:
+ return self._body
+ return ''
+
+ @Property(datetime, constant=True)
+ def created_at(self) -> datetime:
+ return self._created_at
- labels = [Label(label) for label in data['labels']]
+ @Property(str, constant=True)
+ def username(self) -> str:
+ return self._user.username
+ def load_comments(self, response: QByteArray) -> None:
+ data = json.loads(response.toStdString())
+ self._comments = [Comment(comment) for comment in data]
+ print(f'emiting new comments {len(self._comments)}')
+ self.commentsLoaded.emit()