Skip to content

Commit

Permalink
Organization dashboard entry point for staff
Browse files Browse the repository at this point in the history
Include a public_id parameter to access the dashboards
when launched from the organization view in the admin pages
  • Loading branch information
marcospri committed Jul 30, 2024
1 parent 268352c commit 1c89a0f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lms/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ def includeme(config): # noqa: PLR0915
)
config.add_route("admin.organization.toggle", "/admin/orgs/{id_}/toggle")
config.add_route("admin.organizations", "/admin/orgs")
config.add_route(
"admin.organization.dashboard", "/admin/organization/{id_}/dashboard"
)
config.add_route("admin.organization.move_org", "/admin/orgs/{id_}/move_org")
config.add_route("admin.organization.new", "/admin/org/new")
config.add_route(
Expand Down
3 changes: 3 additions & 0 deletions lms/templates/admin/organization/show.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<legend class="label has-text-centered">Organization</legend>
<form method="POST"
action="{{ request.route_url("admin.organization", id_=org.id) }}">
<div class="block has-text-right">
<a class="button is-primary" target="_blank" href="{{ request.route_url('admin.organization.dashboard', id_=org.id) }}">Open instructor dashboard</a>
</div>
<input type="hidden" name="csrf_token" value="{{ get_csrf_token() }}">
{{ macros.disabled_text_field("ID", org.public_id) }}
{{ macros.form_text_field(request, "Name", "name", org.name) }}
Expand Down
35 changes: 33 additions & 2 deletions lms/views/admin/organization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import asdict
from datetime import datetime

import sqlalchemy
Expand All @@ -6,8 +7,8 @@
from pyramid.view import view_config, view_defaults
from webargs import fields

from lms.events import AuditTrailEvent
from lms.models import Organization
from lms.events import AuditTrailEvent, ModelChange
from lms.models import EventType, Organization
from lms.security import Permissions
from lms.services import (
HubSpotService,
Expand Down Expand Up @@ -280,6 +281,36 @@ def delete_organization_dashboard_admin(self):
)
)

@view_config(
route_name="admin.organization.dashboard",
request_method="GET",
permission=Permissions.STAFF,
)
def org_dashboard(self):
org = self._get_org_or_404(self.request.matchdict["id_"])
self.request.registry.notify(
AuditTrailEvent(
request=self.request,
type=EventType.Type.AUDIT_TRAIL,
data=asdict(
ModelChange(
model=Organization.__name__,
id=org.id,
action="view_dashboard",
source="admin_pages",
userid=self.request.identity.userid,
changes={},
)
),
)
)

return HTTPFound(
location=self.request.route_url(
"dashboard", _query={"public_id": org.public_id}
),
)

def _get_org_or_404(self, id_) -> Organization:
if org := self.organization_service.get_by_id(id_):
return org
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/lms/views/admin/organization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from h_matchers import Any
from pyramid.httpexceptions import HTTPBadRequest, HTTPFound, HTTPNotFound

from lms.models import EventType
from lms.services.organization import InvalidOrganizationParent, InvalidPublicId
from lms.views.admin.organization import AdminOrganizationViews
from tests import factories
Expand Down Expand Up @@ -327,6 +328,38 @@ def test_delete_organization_dashboard_admin(
sentinel.dashboard_admin_id
)

def test_course_dashboard(
self,
pyramid_request,
organization_service,
views,
AuditTrailEvent,
organization,
):
pyramid_request.matchdict["id_"] = sentinel.id
organization_service.get_by_id.return_value = organization

response = views.org_dashboard()

AuditTrailEvent.assert_called_once_with(
request=pyramid_request,
type=EventType.Type.AUDIT_TRAIL,
data={
"action": "view_dashboard",
"id": organization.id,
"model": "Organization",
"source": "admin_pages",
"userid": "TEST_USER_ID",
"changes": {},
},
)
pyramid_request.registry.notify.has_call_with(AuditTrailEvent.return_value)
assert response == Any.instance_of(HTTPFound).with_attrs(
{
"location": f"http://example.com/dashboard?public_id={organization.public_id}",
}
)

@pytest.fixture
def with_valid_params_for_usage(self, pyramid_request):
pyramid_request.POST["since"] = "2023-01-01"
Expand Down

0 comments on commit 1c89a0f

Please sign in to comment.