Skip to content

Commit 14494e4

Browse files
authored
Merge pull request #241 from adamspd/237-staff-shows-none-none-not-showing-email
Enhance staff member name retrieval with additional fallbacks
2 parents 9a5886d + fb39cd9 commit 14494e4

File tree

5 files changed

+97
-51
lines changed

5 files changed

+97
-51
lines changed

appointment/models.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,14 @@ def works_on_both_weekends_day(self):
211211
return self.work_on_saturday and self.work_on_sunday
212212

213213
def get_staff_member_name(self):
214-
if hasattr(self.user, 'get_full_name') and callable(getattr(self.user, 'get_full_name')):
215-
name = self.user.get_full_name()
216-
else:
217-
name = f"{self.user.first_name} {self.user.last_name}".strip()
218-
if not name:
219-
name = self.user.username
220-
return name
214+
name_options = [
215+
getattr(self.user, 'get_full_name', lambda: '')(),
216+
f"{self.user.first_name} {self.user.last_name}",
217+
self.user.username,
218+
self.user.email,
219+
f"Staff Member {self.id}"
220+
]
221+
return next((name.strip() for name in name_options if name.strip()), "Unknown")
221222

222223
def get_staff_member_first_name(self):
223224
return self.user.first_name

appointment/tests/base/base_test.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ class BaseTest(TestCase, UserMixin, StaffMemberMixin, ServiceMixin, AppointmentR
1919
users = None
2020

2121
USER_SPECS = {
22-
'staff1': {"first_name": "Daniel", "email": "[email protected]",
22+
'staff1': {"first_name": "Daniel", "last_name": "Jackson", "email": "[email protected]",
2323
"username": "daniel.jackson"},
24-
'staff2': {"first_name": "Samantha", "email": "[email protected]",
24+
'staff2': {"first_name": "Samantha", "last_name": "Carter", "email": "[email protected]",
2525
"username": "samantha.carter"},
26-
'client1': {"first_name": "Georges", "email": "[email protected]",
27-
"username": "georges.hammond"},
28-
'client2': {"first_name": "Tealc", "email": "[email protected]", "username": "tealc.kree"},
29-
'superuser': {"first_name": "Jack", "email": "[email protected]", "username": "jack.o.neill"},
26+
'client1': {"first_name": "Georges", "last_name": "Hammond",
27+
"email": "[email protected]", "username": "georges.hammond"},
28+
'client2': {"first_name": "Tealc", "last_name": "Kree", "email": "[email protected]",
29+
"username": "tealc.kree"},
30+
'superuser': {"first_name": "Jack", "last_name": "O'Neill", "email": "[email protected]",
31+
"username": "jack.o.neill"},
3032
}
3133

3234
@classmethod

appointment/tests/mixins/base_mixin.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ def __init__(self):
1111
pass
1212

1313
@classmethod
14-
def create_user_(cls, first_name="Janet", email="[email protected]", username="janet.fraiser",
14+
def create_user_(cls, first_name="Janet", last_name="Fraiser", email="[email protected]",
15+
username="janet.fraiser",
1516
password="G0a'uld$Emp1re"):
1617
return get_user_model().objects.create_user(
17-
first_name=first_name,
18-
email=email,
19-
username=username,
20-
password=password
18+
first_name=first_name,
19+
last_name=last_name,
20+
email=email,
21+
username=username,
22+
password=password
2123
)
2224

2325

@@ -29,10 +31,10 @@ def __init__(self):
2931
def create_service_(cls, name="Quantum Mirror Assessment", duration=timedelta(hours=1), price=50000,
3032
description="Assess the Quantum Mirror"):
3133
return Service.objects.create(
32-
name=name,
33-
duration=duration,
34-
price=price,
35-
description=description
34+
name=name,
35+
duration=duration,
36+
price=price,
37+
description=description
3638
)
3739

3840

@@ -55,11 +57,11 @@ def __init__(self):
5557
def create_appointment_request_(cls, service, staff_member, date_=date.today(), start_time=time(9, 0),
5658
end_time=time(10, 0)):
5759
return AppointmentRequest.objects.create(
58-
date=date_,
59-
start_time=start_time,
60-
end_time=end_time,
61-
service=service,
62-
staff_member=staff_member
60+
date=date_,
61+
start_time=start_time,
62+
end_time=end_time,
63+
service=service,
64+
staff_member=staff_member
6365
)
6466

6567
@classmethod
@@ -75,10 +77,10 @@ def __init__(self):
7577
def create_appointment_(cls, user, appointment_request, phone="+12392340543",
7678
address="Stargate Command, Cheyenne Mountain Complex, Colorado Springs, CO"):
7779
return Appointment.objects.create(
78-
client=user,
79-
appointment_request=appointment_request,
80-
phone=phone,
81-
address=address
80+
client=user,
81+
appointment_request=appointment_request,
82+
phone=phone,
83+
address=address
8284
)
8385

8486
@classmethod
@@ -94,12 +96,12 @@ def __init__(self):
9496
def create_reschedule_history_(cls, appointment_request, date_, start_time, end_time, staff_member,
9597
reason_for_rescheduling="Zat'nik'tel Discharge"):
9698
return AppointmentRescheduleHistory.objects.create(
97-
appointment_request=appointment_request,
98-
date=date_,
99-
start_time=start_time,
100-
end_time=end_time,
101-
staff_member=staff_member,
102-
reason_for_rescheduling=reason_for_rescheduling
99+
appointment_request=appointment_request,
100+
date=date_,
101+
start_time=start_time,
102+
end_time=end_time,
103+
staff_member=staff_member,
104+
reason_for_rescheduling=reason_for_rescheduling
103105
)
104106

105107

@@ -111,8 +113,8 @@ def __init__(self):
111113
def create_config_(cls, lead_time=time(9, 0), finish_time=time(17, 0), slot_duration=30,
112114
appointment_buffer_time=0):
113115
return Config.objects.create(
114-
lead_time=lead_time,
115-
finish_time=finish_time,
116-
slot_duration=slot_duration,
117-
appointment_buffer_time=appointment_buffer_time
116+
lead_time=lead_time,
117+
finish_time=finish_time,
118+
slot_duration=slot_duration,
119+
appointment_buffer_time=appointment_buffer_time
118120
)

appointment/tests/models/test_appointment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_appointment_getters(self):
7575
def test_conversion_to_dict(self):
7676
response = {
7777
'id': 1,
78-
'client_name': self.client_.first_name,
78+
'client_name': self.client_.first_name + ' ' + self.client_.last_name,
7979
'client_email': self.client_.email,
8080
'start_time': '1900-01-01 09:00',
8181
'end_time': '1900-01-01 10:00',

appointment/tests/models/test_staff_member.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def test_default_attributes_on_creation(self):
3535

3636
def test_creation_without_service(self):
3737
"""A staff member can be created without a service."""
38-
new_staff = self.create_user_(
39-
first_name="Jonas", email="[email protected]", username="jonas.quinn")
38+
new_staff = self.create_user_(first_name="Jonas", last_name="Quinn", email="[email protected]",
39+
username="jonas.quinn")
4040
new_staff_member = StaffMember.objects.create(user=new_staff)
4141
self.assertIsNotNone(new_staff_member)
4242
self.assertEqual(new_staff_member.services_offered.count(), 0)
@@ -46,6 +46,47 @@ def test_creation_fails_without_user(self):
4646
with self.assertRaises(IntegrityError):
4747
StaffMember.objects.create()
4848

49+
def test_get_staff_member_name_with_email(self):
50+
# Simulate create a staff member with only an email and username
51+
# (in my case, username is mandatory, but should work with email as well)
52+
email_only_user = self.create_user_(
53+
first_name="",
54+
last_name="",
55+
56+
username="[email protected]"
57+
)
58+
email_only_staff = StaffMember.objects.create(user=email_only_user)
59+
60+
# Test that the email is returned when no other name information is available
61+
self.assertEqual(email_only_staff.get_staff_member_name(), "[email protected]")
62+
63+
def test_get_staff_member_name_priority(self):
64+
# Create a staff member with all name fields filled
65+
full_user = self.create_user_(
66+
first_name="Rodney",
67+
last_name="McKay",
68+
69+
username="rodney.mckay"
70+
)
71+
full_staff = StaffMember.objects.create(user=full_user)
72+
73+
# Test that the full name is returned when available
74+
self.assertEqual(full_staff.get_staff_member_name(), "Rodney McKay")
75+
76+
# Modify the user to test different scenarios
77+
full_user.first_name = ""
78+
full_user.last_name = ""
79+
full_user.save()
80+
81+
# Test that the username is returned when full name is not available
82+
self.assertEqual(full_staff.get_staff_member_name(), "rodney.mckay")
83+
84+
full_user.username = ""
85+
full_user.save()
86+
87+
# Test that the email is returned when username and full name are not available
88+
self.assertEqual(full_staff.get_staff_member_name(), "[email protected]")
89+
4990

5091
class StaffMemberServiceTests(BaseTest):
5192
@classmethod
@@ -84,14 +125,14 @@ def test_services_offered(self):
84125

85126
def test_staff_member_with_non_existent_service(self):
86127
"""A staff member cannot offer a non-existent service."""
87-
new_staff = self.create_user_(
88-
first_name="Vala", email="[email protected]", username="vala.mal-doran")
128+
new_staff = self.create_user_(first_name="Vala", last_name="Mal Doran",
129+
email="[email protected]", username="vala.mal-doran")
89130
new_staff_member = StaffMember.objects.create(user=new_staff)
90131

91132
# Trying to add a non-existent service to the staff member's services_offered
92133
with self.assertRaises(ValueError):
93134
new_staff_member.services_offered.add(
94-
Service(id=9999, name="Prometheus Acquisition", duration=timedelta(hours=2), price=200))
135+
Service(id=9999, name="Prometheus Acquisition", duration=timedelta(hours=2), price=200))
95136

96137

97138
class StaffMemberWorkingTimeTests(BaseTest):
@@ -212,10 +253,10 @@ def test_get_staff_member_first_name(self):
212253
def test_config_values_takes_over_when_sm_values_null(self):
213254
"""When some values are null in the StaffMember, the Config values should be used."""
214255
config = Config.objects.create(
215-
lead_time=datetime.time(9, 34),
216-
finish_time=datetime.time(17, 11),
217-
slot_duration=37,
218-
appointment_buffer_time=16
256+
lead_time=datetime.time(9, 34),
257+
finish_time=datetime.time(17, 11),
258+
slot_duration=37,
259+
appointment_buffer_time=16
219260
)
220261
# Checking that the StaffMember's values are None
221262
self.assertIsNone(self.staff_member.slot_duration)

0 commit comments

Comments
 (0)