Skip to content

Commit

Permalink
Expose the course an assignment belongs to in the admin pages
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed May 14, 2024
1 parent 60ed986 commit b0bea13
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 28 deletions.
18 changes: 15 additions & 3 deletions lms/models/assignment.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.orm import Mapped, mapped_column

from lms.db import Base
from lms.models._mixins import CreatedUpdatedMixin
from lms.models.grouping import Grouping


class Assignment(CreatedUpdatedMixin, Base):
Expand Down Expand Up @@ -78,13 +78,25 @@ class Assignment(CreatedUpdatedMixin, Base):
deep_linking_uuid: Mapped[str | None] = mapped_column(sa.Unicode, nullable=True)
"""UUID that identifies the deep linking that created this assignment."""

groupings = association_proxy("assignment_grouping", "grouping")
"""List of groupings this assigments is related to."""
groupings: Mapped[list[Grouping]] = sa.orm.relationship(
secondary="assignment_grouping", viewonly=True, lazy="dynamic"
)

membership = sa.orm.relationship(
"AssignmentMembership", lazy="dynamic", viewonly=True
)

@property
def course(self):
"""Course this assignment belongs to."""
return (
self.groupings.filter_by(type="course")
.order_by(Grouping.created.desc())
# While logically one assignment belongs to only one course our grouping table might have more
# than one row representing the same course. Return the last created one.
.first()
)

def get_canvas_mapped_file_id(self, file_id):
return self.extra.get("canvas_file_mappings", {}).get(file_id, file_id)

Expand Down
4 changes: 4 additions & 0 deletions lms/templates/admin/assignment/show.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
{{ macros.created_updated_fields(assignment) }}
</fieldset>

<fieldset class="box mt-6">
<legend class="label has-text-centered">Course</legend>
{{ macros.course_preview(request, assignment.course) }}
</fieldset>
</div>
{% endblock %}
26 changes: 1 addition & 25 deletions lms/templates/admin/course/search.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,7 @@
<fieldset class="box mt-6">
{% if courses %}
<legend class="label has-text-centered">Results</legend>
<div class="container">
<div class="table-container">
<table class="table is-fullwidth">
<thead>
<tr>
<th></th>
<th>Context ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{% for course in courses %}
<tr>
<td>
<a class="button"
href="{{ request.route_url('admin.course', id_=course.id) }}">View</a>
</td>
<td>{{ course.lms_id }}</td>
<td>{{ course.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{{ macros.courses_table(request, courses) }}
{% else %}
<legend class="label has-text-centered">No results found</legend>
{% endif %}
Expand Down
15 changes: 15 additions & 0 deletions lms/templates/admin/macros.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@
<div class="has-text-centered">No Application Instance</div>
{% endif %}
{% endmacro %}
{% macro course_preview(request, course) %}
<div class="block has-text-right">
<a class="button"
href="{{ request.route_url("admin.course", id_=course.id) }}">View</a>
</div>
<div>{{ disabled_text_field("Name", course.lms_name) }}</div>
{% endmacro %}
{% macro registration_preview(request, lti_registration) %}
{% if lti_registration %}
<div class="block has-text-right">
Expand Down Expand Up @@ -247,3 +254,11 @@
{"label": "Resource link id", "name": "resource_link_id"},
]) }}
{% endmacro %}
{% macro courses_table(request, courses) %}
{{ object_list_table(request, 'admin.course', courses,
fields=[
{"label": "Name", "name": "lms_name"},
{"label": "Context ID", "name": "lms_name"},
{"label": "Resource link id", "name": "resource_link_id"},
]) }}
{% endmacro %}
20 changes: 20 additions & 0 deletions tests/unit/lms/models/assignment_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime

import pytest

from lms.models import Assignment
from tests import factories


class TestAssignment:
Expand Down Expand Up @@ -30,6 +33,23 @@ def test_get_canvas_mapped_file_id_returns_the_given_file_id_if_no_mapping_exist
):
assert assignment.get_canvas_mapped_file_id("file_id") == "file_id"

def test_course(self, assignment, db_session):
course = factories.Course(created=datetime.datetime(2024, 1, 1))
factories.AssignmentGrouping(
assignment=assignment, grouping=factories.CanvasSection()
)
factories.AssignmentGrouping(
assignment=assignment, grouping=factories.CanvasGroup()
)
factories.AssignmentGrouping(
assignment=assignment,
grouping=factories.Course(created=datetime.datetime(2022, 1, 1)),
)
factories.AssignmentGrouping(assignment=assignment, grouping=course)
db_session.flush()

assert assignment.course == course

@pytest.fixture
def assignment(self, db_session):
assignment = Assignment(
Expand Down

0 comments on commit b0bea13

Please sign in to comment.