Folder creation

This commit is contained in:
StNicolay 2024-08-09 23:14:06 +03:00
parent b0f20680b4
commit 37940d633a
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
3 changed files with 87 additions and 39 deletions

View File

@ -0,0 +1,37 @@
from __future__ import annotations
import uuid
import file_widgets
from PyQt6.QtWidgets import QLineEdit, QMessageBox, QPushButton, QVBoxLayout, QWidget
from request_client import RequestClient
class CreateFolderWidget(QWidget):
def __init__(self, folder_id: uuid.UUID, file_list: file_widgets.FileListWidget):
super().__init__()
self.folder_id = folder_id
self.file_list = file_list
self.setWindowTitle("Folder creation")
layout = QVBoxLayout()
self.edit = QLineEdit()
layout.addWidget(self.edit)
button = QPushButton("Submit")
button.clicked.connect(self.submit)
layout.addWidget(button)
self.setLayout(layout)
def submit(self):
response = RequestClient().client.post(
"/folders",
json={
"folder_name": self.edit.text(),
"parent_folder_id": str(self.folder_id),
},
)
if not response.is_success:
QMessageBox.warning(None, "Error creating folder", response.text)
else:
QMessageBox.information(None, "Folder created", "Folder created")
self.file_list.update_response()
self.close()

View File

@ -1,23 +1,18 @@
from __future__ import annotations from __future__ import annotations
import dataclasses import dataclasses
import user
import datetime import datetime
import os import os
import uuid import uuid
from typing import Protocol, Self from typing import Protocol, Self
import create_folder_widget
import httpx import httpx
import pydantic import pydantic
import state import state
import user
from PyQt6.QtCore import QPoint, Qt from PyQt6.QtCore import QPoint, Qt
from PyQt6.QtGui import ( from PyQt6.QtGui import QAction, QDragEnterEvent, QDragMoveEvent, QDropEvent, QIcon
QAction,
QDragEnterEvent,
QDragMoveEvent,
QDropEvent,
QIcon,
)
from PyQt6.QtWidgets import ( from PyQt6.QtWidgets import (
QFileDialog, QFileDialog,
QHBoxLayout, QHBoxLayout,
@ -62,9 +57,7 @@ class File(pydantic.BaseModel):
return self.file_name return self.file_name
def delete(self) -> None: def delete(self) -> None:
RequestClient().client.delete( RequestClient().client.delete("/files", params={"file_id": self.file_id})
"/files", params={"file_id": self.file_id}
)
def details(self, list: FileListWidget) -> QWidget: def details(self, list: FileListWidget) -> QWidget:
del list del list
@ -121,8 +114,10 @@ class Folder(pydantic.BaseModel):
return self.folder_name return self.folder_name
def delete(self) -> None: def delete(self) -> None:
RequestClient().client.delete( print(
"/folders", params={"folder_id": self.folder_id} RequestClient()
.client.delete("/folders", params={"folder_id": self.folder_id})
.text
) )
def details(self, list: FileListWidget) -> QWidget: def details(self, list: FileListWidget) -> QWidget:
@ -144,6 +139,9 @@ class ResponseProtocol(Protocol):
def update(self) -> ResponseProtocol: def update(self) -> ResponseProtocol:
raise NotImplementedError raise NotImplementedError
def create_folder(self) -> QWidget | None:
raise NotImplementedError
class ListResponse(pydantic.BaseModel): class ListResponse(pydantic.BaseModel):
folder_id: uuid.UUID folder_id: uuid.UUID
@ -165,6 +163,32 @@ class ListResponse(pydantic.BaseModel):
def update(self) -> ResponseProtocol: def update(self) -> ResponseProtocol:
return self.get(self.folder_id) return self.get(self.folder_id)
def create_folder(self, file_list: FileListWidget):
return create_folder_widget.CreateFolderWidget(self.folder_id, file_list)
@dataclasses.dataclass(slots=True)
class TlpResponse:
folders: list[Folder]
@staticmethod
def get() -> Self:
url = "/permissions/get_top_level_permitted_folders"
return TlpResponse(
pydantic.TypeAdapter(list[Folder]).validate_json(
RequestClient().client.get(url).text
)
)
def items(self) -> list[DisplayProtocol]:
return self.folders
def update(self) -> ResponseProtocol:
return self.get()
def create_folder(self):
return # Not much to do
class FileListWidget(QListWidget): class FileListWidget(QListWidget):
def __init__(self, state: state.State): def __init__(self, state: state.State):
@ -217,9 +241,7 @@ class FileListWidget(QListWidget):
response = RequestClient().client.post( response = RequestClient().client.post(
"http://localhost:3000/files", "http://localhost:3000/files",
files=files, files=files,
params={ params={"parent_folder": self.current_response().folder_id},
"parent_folder": self.current_response().folder_id
},
) )
if response.is_success: if response.is_success:
QMessageBox.information( QMessageBox.information(
@ -276,6 +298,7 @@ class Sidebar(QWidget):
self.state = state self.state = state
self.file_list = file_list self.file_list = file_list
self.user_widget = None self.user_widget = None
self.folder_widget = None
layout = QVBoxLayout() layout = QVBoxLayout()
self.setLayout(layout) self.setLayout(layout)
buttons = [ buttons = [
@ -285,6 +308,7 @@ class Sidebar(QWidget):
("Go root", self.go_root), ("Go root", self.go_root),
("Get permitted", self.get_tlp), ("Get permitted", self.get_tlp),
("Sync", self.sync), ("Sync", self.sync),
("Create folder", self.create_folder),
] ]
for text, func in buttons: for text, func in buttons:
button = QPushButton(text) button = QPushButton(text)
@ -317,25 +341,12 @@ class Sidebar(QWidget):
# TODO # TODO
... ...
def create_folder(self):
@dataclasses.dataclass(slots=True) self.folder_widget = self.file_list.current_response().create_folder(
class TlpResponse: self.file_list
folders: list[Folder]
@staticmethod
def get() -> Self:
url = "/permissions/get_top_level_permitted_folders"
return TlpResponse(
pydantic.TypeAdapter(list[Folder]).validate_json(
RequestClient().client.get(url).text
)
) )
if self.folder_widget is not None:
def items(self) -> list[DisplayProtocol]: self.folder_widget.show()
return self.folders
def update(self) -> ResponseProtocol:
return self.get()
class MainFileWidget(QWidget): class MainFileWidget(QWidget):

View File

@ -3,13 +3,13 @@ from __future__ import annotations
import pydantic import pydantic
import state import state
from PyQt6.QtWidgets import ( from PyQt6.QtWidgets import (
QWidget,
QLineEdit,
QLabel,
QVBoxLayout,
QPushButton,
QHBoxLayout, QHBoxLayout,
QLabel,
QLineEdit,
QMessageBox, QMessageBox,
QPushButton,
QVBoxLayout,
QWidget,
) )
from request_client import RequestClient from request_client import RequestClient