Skip to content
This repository was archived by the owner on Dec 19, 2021. It is now read-only.

Commit 354c03d

Browse files
authored
Replace widget with dashboard (#415)
1 parent cf48e21 commit 354c03d

File tree

9 files changed

+75
-110
lines changed

9 files changed

+75
-110
lines changed

config/routes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from resources.tickets import Ticket, Tickets
1818
from resources.notes import Notes, Note
1919
from resources.lease import Lease, Leases
20-
from resources.widgets import Widgets
2120
from resources.dashboard_resource import DashboardResource
2221
from tests.cypress.cypress_resource import CypressResource
2322

@@ -36,7 +35,6 @@ def routing(app):
3635
api.add_resource(UsersPending, "users/pending")
3736
api.add_resource(ArchiveUser, "user/archive/<int:user_id>")
3837
api.add_resource(UserLogin, "login")
39-
api.add_resource(Widgets, "widgets")
4038
api.add_resource(DashboardResource, "dashboard")
4139
api.add_resource(UserAccessRefresh, "refresh")
4240
api.add_resource(StaffTenants, "staff-tenants")

models/dashboard.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,6 @@
99
class Dashboard:
1010
@staticmethod
1111
def json():
12-
return {
13-
"opentickets": {
14-
"new": {
15-
"allNew": {
16-
"stat": TicketModel.new().count(),
17-
},
18-
"unseen24Hrs": {
19-
"stat": TicketModel.new().updated_within(days=1).count(),
20-
},
21-
},
22-
"inProgress": {
23-
"allInProgress": {
24-
"stat": TicketModel.in_progress().count(),
25-
},
26-
"inProgress1Week": {
27-
"stat": TicketModel.in_progress()
28-
.updated_within(days=7)
29-
.count(),
30-
},
31-
},
32-
},
33-
"managers": [Dashboard._manager_json(m) for m in PropertyManager.take(3)],
34-
}
35-
36-
# Below is what I would like to use instead of the above json^^^
37-
@staticmethod
38-
def proposed_json():
3912
return {
4013
"tickets": {
4114
"new": {
@@ -49,7 +22,7 @@ def proposed_json():
4922
.count(),
5023
},
5124
},
52-
"managers": [Dashboard.__manager_json(m) for m in PropertyManager.take(3)],
25+
"managers": [Dashboard._manager_json(m) for m in PropertyManager.take(3)],
5326
"pending_users": UserModel.query.active().pending().json(),
5427
"staff": Staff.query.json(),
5528
"tenants": TenantModel.query.active()
@@ -60,18 +33,6 @@ def proposed_json():
6033

6134
@staticmethod
6235
def _manager_json(manager):
63-
return {
64-
"id": manager.id,
65-
"firstName": manager.firstName,
66-
"lastName": manager.lastName,
67-
"date": Dashboard._humanize_date(manager.created_at.date()),
68-
"propertyName": manager.properties[0].name
69-
if len(manager.properties) > 0
70-
else "Not Assigned",
71-
}
72-
73-
@staticmethod
74-
def __manager_json(manager):
7536
return {
7637
"id": manager.id,
7738
"first_name": manager.firstName,

resources/dashboard_resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
class DashboardResource(Resource):
88
@admin_required
99
def get(self):
10-
return Dashboard.proposed_json()
10+
return Dashboard.json()

resources/widgets.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/factory_fixtures/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
from .emergency_contact import *
77
from .ticket import *
88
from .notes import *
9+
from .dashboard import *
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
3+
from models.tickets import TicketModel
4+
from utils.time import TimeStamp
5+
6+
7+
@pytest.fixture
8+
def create_dashboard(
9+
create_ticket, create_property_manager, create_property, create_unauthorized_user
10+
):
11+
def _create_dashboard(**kwargs):
12+
def _create_tickets(num, status, tenant=None, author=None, updated_at=None):
13+
updated_at = updated_at or TimeStamp.now()
14+
for _ in range(num):
15+
create_ticket(
16+
status=status, tenant=tenant, author=author, updated_at=updated_at
17+
)
18+
19+
dashboard = {}
20+
ticket = create_ticket(status=TicketModel.NEW)
21+
tenant = ticket.tenant
22+
author = ticket.author
23+
dashboard["tenant"] = tenant
24+
dashboard["author"] = author
25+
_create_tickets(1, TicketModel.NEW, tenant, author)
26+
_create_tickets(5, TicketModel.NEW, tenant, author, TimeStamp.weeks_ago(1))
27+
_create_tickets(3, TicketModel.IN_PROGRESS, tenant, author)
28+
_create_tickets(
29+
7, TicketModel.IN_PROGRESS, tenant, author, TimeStamp.weeks_ago(2)
30+
)
31+
32+
dashboard["pending_user"] = create_unauthorized_user()
33+
pm = create_property_manager()
34+
pm2 = create_property_manager(created_at=TimeStamp.days_ago(1))
35+
pm3 = create_property_manager(created_at=TimeStamp.days_ago(2))
36+
dashboard["pm"] = pm
37+
dashboard["pm2"] = pm2
38+
dashboard["pm3"] = pm3
39+
dashboard["prop"] = create_property(manager_ids=[pm.id])
40+
dashboard["prop2"] = create_property(manager_ids=[pm.id, pm3.id])
41+
42+
return dashboard
43+
44+
yield _create_dashboard
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import pytest
22

3+
from models.dashboard import Dashboard
4+
35

46
@pytest.mark.usefixtures("client_class")
57
class TestDashboardResource:
6-
def test_GET(self, valid_header):
8+
def test_GET(self, valid_header, create_dashboard):
9+
create_dashboard()
710
response = self.client.get("/api/dashboard", headers=valid_header)
811

912
assert response.status_code == 200
13+
assert response.json == Dashboard.json()

tests/integration/test_widgets.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/unit/test_dashboard.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,46 @@
1-
from models.tickets import TicketModel
2-
from utils.time import TimeStamp
31
from models.dashboard import Dashboard
42

53

64
class TestDashboard:
7-
def test_json(
8-
self, valid_header, create_ticket, create_property_manager, create_property
9-
):
10-
def _create_tickets(num, status, updated_at=None):
11-
updated_at = updated_at or TimeStamp.now()
12-
for _ in range(num):
13-
create_ticket(status=status, updated_at=updated_at)
14-
15-
_create_tickets(2, TicketModel.NEW)
16-
_create_tickets(5, TicketModel.NEW, TimeStamp.weeks_ago(1))
17-
_create_tickets(3, TicketModel.IN_PROGRESS)
18-
_create_tickets(7, TicketModel.IN_PROGRESS, TimeStamp.weeks_ago(2))
19-
20-
pm = create_property_manager()
21-
pm2 = create_property_manager(created_at=TimeStamp.days_ago(1))
22-
pm3 = create_property_manager(created_at=TimeStamp.days_ago(2))
23-
prop = create_property(manager_ids=[pm.id])
24-
prop2 = create_property(manager_ids=[pm.id, pm3.id])
25-
5+
def test_json(self, create_dashboard):
6+
dashboard = create_dashboard()
267
response = Dashboard.json()
278

289
assert response == {
2910
"managers": [
3011
{
3112
"date": "Today",
32-
"firstName": pm.firstName,
33-
"id": pm.id,
34-
"lastName": pm.lastName,
35-
"propertyName": prop.name,
13+
"first_name": dashboard["pm"].firstName,
14+
"id": dashboard["pm"].id,
15+
"last_name": dashboard["pm"].lastName,
16+
"property_name": dashboard["prop"].name,
3617
},
3718
{
3819
"date": "Yesterday",
39-
"firstName": pm2.firstName,
40-
"id": pm2.id,
41-
"lastName": pm2.lastName,
42-
"propertyName": "Not Assigned",
20+
"first_name": dashboard["pm2"].firstName,
21+
"id": dashboard["pm2"].id,
22+
"last_name": dashboard["pm2"].lastName,
23+
"property_name": "Not Assigned",
4324
},
4425
{
4526
"date": "This Week",
46-
"firstName": pm3.firstName,
47-
"id": pm3.id,
48-
"lastName": pm3.lastName,
49-
"propertyName": prop2.name,
27+
"first_name": dashboard["pm3"].firstName,
28+
"id": dashboard["pm3"].id,
29+
"last_name": dashboard["pm3"].lastName,
30+
"property_name": dashboard["prop2"].name,
5031
},
5132
],
52-
"opentickets": {
33+
"pending_users": [dashboard["pending_user"].json()],
34+
"staff": [dashboard["author"].json()],
35+
"tenants": [dashboard["tenant"].json()],
36+
"tickets": {
5337
"new": {
54-
"allNew": {
55-
"stat": 7,
56-
},
57-
"unseen24Hrs": {
58-
"stat": 2,
59-
},
38+
"total_count": 7,
39+
"latent_count": 2,
6040
},
61-
"inProgress": {
62-
"allInProgress": {
63-
"stat": 10,
64-
},
65-
"inProgress1Week": {
66-
"stat": 3,
67-
},
41+
"in_progress": {
42+
"total_count": 10,
43+
"latent_count": 3,
6844
},
6945
},
7046
}

0 commit comments

Comments
 (0)