Skip to content

Commit

Permalink
Merge pull request #61 from PLD-Agile/44-Link-DeliveryMen-Modify
Browse files Browse the repository at this point in the history
44 link delivery men modify
  • Loading branch information
cfstcyr authored Nov 13, 2023
2 parents 6858c64 + 1d1655d commit 72b268b
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/models/delivery_man/delivery_man.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from dataclasses import dataclass
import uuid
from dataclasses import dataclass, field
from typing import List


@dataclass
class DeliveryMan:
name: str
availabilities: List[int]
id: str = field(default_factory=lambda: str(uuid.uuid4()))
16 changes: 9 additions & 7 deletions src/services/delivery_man/delivery_man_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@


class DeliveryManService(Singleton):
n1 = DeliveryMan("Josué stcyr", [8, 9, 10, 11])
n2 = DeliveryMan("clem farhat", [8, 9, 10, 11])
__delivery_men: BehaviorSubject[Dict[str, DeliveryMan]] = BehaviorSubject(
{
"Josué stcyr": DeliveryMan("Josué stcyr", [8, 9, 10, 11]),
"clem farhat": DeliveryMan("clem farhat", [8, 9, 10, 11]),
n1.id: n1,
n2.id: n2,
}
)
__selected_delivery_man: BehaviorSubject[Optional[DeliveryMan]] = BehaviorSubject(
Expand Down Expand Up @@ -61,7 +63,7 @@ def create_delivery_man(self, name: str) -> None:

deliveryman = DeliveryMan(name, availabilities)

self.__delivery_men.value[deliveryman.name] = deliveryman
self.__delivery_men.value[deliveryman.id] = deliveryman
self.__delivery_men.on_next(self.__delivery_men.value)

return deliveryman
Expand All @@ -80,7 +82,7 @@ def modify_delivery_man(
DeliveryMan: DeliveryMan instance
"""

delivery_man = self.__delivery_men.value[delivery_man.name]
delivery_man = self.__delivery_men.value[delivery_man.id]

name = delivery_man_info.get("name")
availabilities = delivery_man_info.get("availabilities")
Expand Down Expand Up @@ -108,15 +110,15 @@ def remove_delivery_man(self, delivery_man: DeliveryMan) -> None:

return

def set_selected_delivery_man(self, delivery_man_name: Optional[str]) -> None:
def set_selected_delivery_man(self, delivery_man_id: Optional[int]) -> None:
"""Set currently selected delivery man.
Args:
delivery_man_name (str): Name of the delivery man to be selected
"""
self.__selected_delivery_man.on_next(
self.__delivery_men.value[delivery_man_name]
if delivery_man_name is not None
self.__delivery_men.value[delivery_man_id]
if delivery_man_id is not None
else None
)

Expand Down
8 changes: 5 additions & 3 deletions src/services/delivery_man/tests/test_delivery_man_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ def setup_method(self):

@fixture
def root(self):
self.n1 = DeliveryMan("Josué stcyr", [8, 9, 10, 11])
self.n2 = DeliveryMan("clem farhat", [8, 9, 10, 11])

root: Dict[str, DeliveryMan] = {
"Josué stcyr": DeliveryMan("test", [8, 9, 10, 11]),
"clem farhat": DeliveryMan("clem farhat", [8, 9, 10, 11]),
self.n1.id: self.n1,
self.n2.id: self.n2,
}

return root
Expand All @@ -31,5 +34,4 @@ def test_should_create(self, root):
length = len(self.delivery_man_service.delivery_men.value)
delivery_man = self.delivery_man_service.create_delivery_man("test")

assert delivery_man == root["Josué stcyr"]
assert len(self.delivery_man_service.delivery_men.value) == length + 1
2 changes: 1 addition & 1 deletion src/views/main_page/delivery_form_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __build_delivery_man_form(self) -> QLayout:

delivery_man_combobox.currentIndexChanged.connect(
lambda: DeliveryManService.instance().set_selected_delivery_man(
delivery_man_combobox.currentData().name
delivery_man_combobox.currentData().id
if delivery_man_combobox.currentData()
else None
)
Expand Down
142 changes: 138 additions & 4 deletions src/views/manage_delivery_man_page/modify_delivery_man_form_view.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,157 @@
from typing import Dict, List

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QHBoxLayout, QLabel, QPushButton, QVBoxLayout, QWidget
from PyQt6.QtWidgets import (
QCheckBox,
QComboBox,
QHBoxLayout,
QLabel,
QLayout,
QLineEdit,
QMessageBox,
QVBoxLayout,
)

from models.delivery_man.delivery_man import DeliveryMan
from src.controllers.navigator.page import Page
from src.views.ui import Button, Callout, Separator, Text, TextSize
from src.services.delivery_man.delivery_man_service import DeliveryManService
from src.views.ui import Button, Text, TextSize


class ModifyDeliveryManFormView(Page):
__delivery_man_control: QComboBox
__name_input: QLineEdit
__availabilities_checkboxes: List[QCheckBox]

def __init__(self):
super().__init__()

# Define components to be used in this screen
layout = QVBoxLayout()
title_label = Text("Modify a deliveryman", TextSize.H2)
modify_button = Button("Modify")

options_label = Text("Select a deliveryman:", TextSize.H3)
modify_button.clicked.connect(self.__modify_delivery_man)

# Add components in the screen
layout.setAlignment(Qt.AlignmentFlag.AlignTop)
layout.addWidget(title_label)
layout.addWidget(options_label)
layout.addLayout(self.__build_delivery_man_combobox())
layout.addLayout(self.__build_delivery_man_inputs())

layout.addWidget(modify_button)

self.setLayout(layout)

DeliveryManService.instance().delivery_men.subscribe(
self.__update_delivery_man_combobox
)

def __build_delivery_man_combobox(self) -> QLayout:
delivery_man_combobox_layout = QVBoxLayout()
delivery_man_label = QLabel("Delivery man")
self.__delivery_man_control = QComboBox()

# Add components in the screen
delivery_man_combobox_layout.addWidget(delivery_man_label)
delivery_man_combobox_layout.addWidget(self.__delivery_man_control)

delivery_man_combobox_layout.setContentsMargins(0, 0, 0, 0)
delivery_man_combobox_layout.setAlignment(Qt.AlignmentFlag.AlignTop)

self.__delivery_man_control.currentIndexChanged.connect(
lambda: self.__update_delivery_man_inputs(
self.__delivery_man_control.currentData()
)
)

return delivery_man_combobox_layout

def __build_delivery_man_inputs(self) -> QLayout:
input_layout = QHBoxLayout()
name_layout = QVBoxLayout()
availabilities_layout = QVBoxLayout()
availabilities_label = QLabel("Availabilities")
name_label = QLabel("Name")
self.__name_input = QLineEdit()
self.__availabilities_checkboxes = [QCheckBox(f"{i} am") for i in range(8, 12)]

input_layout = QHBoxLayout()
name_layout = QVBoxLayout()
availabilities_layout = QVBoxLayout()

# Add components in the screen
name_layout.addWidget(name_label)
name_layout.addWidget(self.__name_input)

availabilities_layout.addWidget(availabilities_label)

for checkbox in self.__availabilities_checkboxes:
availabilities_layout.addWidget(checkbox)

input_layout.addLayout(name_layout)
input_layout.addLayout(availabilities_layout)

input_layout.setContentsMargins(0, 0, 0, 0)
input_layout.setAlignment(Qt.AlignmentFlag.AlignTop)

return input_layout

def __modify_delivery_man(self):
selected_delivery_man = self.__delivery_man_control.currentData()
if not selected_delivery_man:
return

name = self.__name_input.text()
availabilities = [
i
for i in range(8, 12)
if self.__availabilities_checkboxes[i - 8].isChecked()
]

if (
name == selected_delivery_man.name
and availabilities == selected_delivery_man.availabilities
):
return # No changes were made

delivery_man_info = {"name": name, "availabilities": availabilities}
modified_delivery_man = DeliveryManService.instance().modify_delivery_man(
selected_delivery_man, delivery_man_info
)

# Show a popup with the changes
message_box = QMessageBox()
message_box.setWindowTitle("Success")
message_box.setText(
f"Delivery man '{modified_delivery_man.name}' modified successfully."
)
message_box.setIcon(QMessageBox.Icon.Information)
message_box.setStandardButtons(QMessageBox.StandardButton.Ok)
message_box.exec()

def __update_delivery_man_combobox(
self, delivery_men: Dict[str, DeliveryMan]
) -> None:
current_value = self.__delivery_man_control.currentData()
self.__delivery_man_control.clear()

for delivery_man in delivery_men.values():
self.__delivery_man_control.addItem(delivery_man.name, delivery_man)

new_index = max(self.__delivery_man_control.findData(current_value), 0)

self.__delivery_man_control.setCurrentIndex(new_index)

def __update_delivery_man_inputs(self, delivery_man: DeliveryMan) -> None:
if delivery_man is not None:
self.__name_input.setText(delivery_man.name)
# Uncheck all checkboxes
for checkbox in self.__availabilities_checkboxes:
checkbox.setChecked(False)

# Check checkboxes based on delivery man's availabilities
for availability in delivery_man.availabilities:
index = availability - 8
if 0 <= index < len(self.__availabilities_checkboxes):
self.__availabilities_checkboxes[index].setChecked(True)

0 comments on commit 72b268b

Please sign in to comment.