Skip to content

Commit

Permalink
Return dates comming from H API as datetime objects
Browse files Browse the repository at this point in the history
The new json_iso_utc serializer will serialize this dates to isoformat
and include timezone information.
  • Loading branch information
marcospri committed Oct 4, 2024
1 parent 1773877 commit b58d7f7
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lms/js_config_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class Pagination(TypedDict):
class AnnotationMetrics(TypedDict):
annotations: int
replies: int
last_activity: str | None
last_activity: datetime | None


class CourseMetrics(TypedDict):
assignments: int
last_launched: str | None
last_launched: datetime | None


class APICallInfo(TypedDict):
Expand Down
15 changes: 13 additions & 2 deletions lms/services/h_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Iterator, Sequence
from typing import Iterator, Sequence, TypedDict

from h_api.bulk_api import BulkAPI, CommandBuilder

Expand All @@ -14,6 +14,17 @@
from lms.services.http import HTTPService


class AnnotationCounts(TypedDict):
annotations: int
replies: int
page_notes: int
last_activity: str

assignment_id: str | None
display_name: str | None
userid: str | None


class HAPIError(ExternalRequestError):
"""
A problem with an h API request.
Expand Down Expand Up @@ -183,7 +194,7 @@ def get_annotation_counts(
group_by: str,
h_userids: list[str] | None = None,
resource_link_ids: list[str] | None = None,
) -> dict:
) -> list[AnnotationCounts]:
filters = {
"groups": group_authority_ids,
"assignment_ids": resource_link_ids,
Expand Down
7 changes: 4 additions & 3 deletions lms/views/dashboard/api/assignment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from marshmallow import fields, validate
from pyramid.view import view_config

Expand Down Expand Up @@ -50,10 +52,9 @@ class AssignmentsMetricsSchema(PyramidRequestSchema):
class AssignmentViews:
def __init__(self, request) -> None:
self.request = request
self.h_api = request.find_service(HAPI)
self.h_api: HAPI = request.find_service(HAPI)
self.assignment_service = request.find_service(name="assignment")
self.dashboard_service = request.find_service(name="dashboard")
self.course_service = request.find_service(name="course")
self.user_service: UserService = request.find_service(UserService)

@view_config(
Expand Down Expand Up @@ -179,7 +180,7 @@ def course_assignments_metrics(self) -> APIAssignments:
metrics = AnnotationMetrics(
annotations=h_stats["annotations"] + h_stats["page_notes"],
replies=h_stats["replies"],
last_activity=h_stats["last_activity"],
last_activity=datetime.fromisoformat(h_stats["last_activity"]),
)
else:
# Assignment with no annos, zeroing the stats
Expand Down
4 changes: 0 additions & 4 deletions lms/views/dashboard/api/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from lms.js_config_types import APICourse, APICourses, CourseMetrics
from lms.models import Course
from lms.security import Permissions
from lms.services.h_api import HAPI
from lms.services.organization import OrganizationService
from lms.validation._base import PyramidRequestSchema
from lms.views.dashboard.pagination import PaginationParametersMixin, get_page

Expand Down Expand Up @@ -45,8 +43,6 @@ class CourseViews:
def __init__(self, request) -> None:
self.request = request
self.course_service = request.find_service(name="course")
self.h_api = request.find_service(HAPI)
self.organization_service = request.find_service(OrganizationService)
self.dashboard_service = request.find_service(name="dashboard")
self.assignment_service = request.find_service(name="assignment")

Expand Down
5 changes: 3 additions & 2 deletions lms/views/dashboard/api/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from datetime import datetime

from marshmallow import fields, validate
from pyramid.view import view_config
Expand Down Expand Up @@ -67,7 +68,7 @@ def __init__(self, request) -> None:
self.request = request
self.assignment_service = request.find_service(name="assignment")
self.dashboard_service = request.find_service(name="dashboard")
self.h_api = request.find_service(HAPI)
self.h_api: HAPI = request.find_service(HAPI)
self.user_service: UserService = request.find_service(UserService)
self.auto_grading_service: AutoGradingService = request.find_service(
AutoGradingService
Expand Down Expand Up @@ -177,7 +178,7 @@ def students_metrics(self) -> APIStudents:
annotation_metrics=AnnotationMetrics(
annotations=s["annotations"] + s["page_notes"],
replies=s["replies"],
last_activity=s["last_activity"],
last_activity=datetime.fromisoformat(s["last_activity"]),
),
)
else:
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/lms/views/dashboard/api/assignment_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from unittest.mock import sentinel

import pytest
Expand Down Expand Up @@ -218,7 +219,7 @@ def test_course_assignments(
"annotation_metrics": {
"annotations": 4,
"replies": sentinel.replies,
"last_activity": sentinel.last_activity,
"last_activity": datetime(2024, 1, 1),
},
},
{
Expand Down Expand Up @@ -247,7 +248,7 @@ def assignments_metrics_response(self, assignment):
"page_notes": 2,
"replies": sentinel.replies,
"userid": "TEACHER",
"last_activity": sentinel.last_activity,
"last_activity": "2024-01-01",
},
]

Expand Down
9 changes: 5 additions & 4 deletions tests/unit/lms/views/dashboard/api/user_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from unittest.mock import call, sentinel

import pytest
Expand Down Expand Up @@ -120,7 +121,7 @@ def test_students_metrics( # pylint:disable=too-many-locals
"annotation_metrics": {
"annotations": 4,
"replies": sentinel.replies,
"last_activity": sentinel.last_activity,
"last_activity": datetime(2024, 1, 1),
},
},
{
Expand Down Expand Up @@ -209,7 +210,7 @@ def test_students_metrics_with_auto_grading( # pylint:disable=too-many-locals
"annotation_metrics": {
"annotations": 4,
"replies": sentinel.replies,
"last_activity": sentinel.last_activity,
"last_activity": datetime(2024, 1, 1),
},
},
{
Expand Down Expand Up @@ -272,15 +273,15 @@ def annotation_counts_response(self, student):
"page_notes": 2,
"replies": sentinel.replies,
"userid": student.h_userid,
"last_activity": sentinel.last_activity,
"last_activity": "2024-01-01",
},
{
"display_name": sentinel.display_name,
"annotations": sentinel.annotations,
"page_notes": sentinel.page_notes,
"replies": sentinel.replies,
"userid": "TEACHER",
"last_activity": sentinel.last_activity,
"last_activity": "2024-01-02",
},
]

Expand Down

0 comments on commit b58d7f7

Please sign in to comment.