Skip to content

Commit

Permalink
🐛(rdfa) fix errors on Google Search Console
Browse files Browse the repository at this point in the history
Google Search Console requires more information for the RDFa information.
Fixed errors:
- Missing field 'hasCourseInstance'
- Either 'courseWorkload' or 'courseSchedule' should be specified
  (in 'hasCourseInstance')
Fix course enrollment_count shouldn't include the hidden runs.
  • Loading branch information
igobranco committed Jul 2, 2024
1 parent ce11b21 commit 38a9939
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unrealeased]

### Fixed

- Fix RDFa errors on Google Search Console
- Fix course enrollment count shouldn't include the hidden runs.

## [2.28.1]

### Fixed
Expand Down
12 changes: 8 additions & 4 deletions src/richie/apps/courses/models/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,14 @@ def course_runs_enrollment_count(self):
node = self.extended_object.node
current_and_descendant_nodes = node.__class__.get_tree(parent=node)

return CourseRun.objects.filter(
direct_course__extended_object__node__in=current_and_descendant_nodes,
direct_course__extended_object__publisher_is_draft=is_draft,
).aggregate(sum=Sum("enrollment_count"))["sum"]
return (
CourseRun.objects.filter(
direct_course__extended_object__node__in=current_and_descendant_nodes,
direct_course__extended_object__publisher_is_draft=is_draft,
)
.exclude(catalog_visibility=CourseRunCatalogVisibility.HIDDEN)
.aggregate(sum=Sum("enrollment_count"))["sum"]
)

@cached_property
def languages_display(self):
Expand Down
33 changes: 17 additions & 16 deletions src/richie/apps/courses/templates/courses/cms/course_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -368,22 +368,23 @@ <h2 class="course-detail__title">{% blocktrans context "course_detail__title" %}
{% block runs %}
<div class="course-detail__aside">
{% render_model_add course "" "" "get_admin_url_to_add_run" %}
{% with runs_dict=current_page.course.course_runs_dict %}
{% with open_visible_runs=runs_dict.0|add:runs_dict.1|add:runs_dict.2|visible_on_course_page:request.toolbar.edit_mode_active %}
<!--
As course runs are rendered through React, we use meta tags to
setup course runs RFDa properties.
-->
{% for run in open_visible_runs %}
<span rel="hasCourseInstance" typeof="CourseInstance">
<meta property="name" content="{{ run.title }}" />
<meta property="inLanguage" content="{{ run.get_languages_display }}" />
<meta property="courseMode" content="online" />
<meta property="startDate" content="{{ run.start|date:'Y-m-d' }}" />
<meta property="endDate" content="{{ run.end|date:'Y-m-d' }}" />
</span>
{% endfor %}
{% endwith %}
{% with runs=current_page.course.course_runs|visible_on_course_page:request.toolbar.edit_mode_active %}
<!--
As course runs are rendered through React, we use meta tags to
setup course runs RFDa properties.
-->
{% for run in runs %}
{% if run.is_visible_on_course_page %}
<span rel="hasCourseInstance" typeof="CourseInstance">
<meta property="name" content='{{ run.title|default:current_page.get_title }}' />
<meta property="inLanguage" content="{{ run.get_languages_display }}" />
<meta property="courseMode" content="online" />
<meta property="startDate" content="{{ run.start|date:'Y-m-d' }}" />
<meta property="endDate" content="{{ run.end|date:'Y-m-d' }}" />
<meta property="courseWorkload" content="{{ current_page.course.pt_effort }}" />
</span>
{% endif %}
{% endfor %}
{% endwith %}
</div>
{% endblock runs %}
Expand Down
20 changes: 20 additions & 0 deletions tests/apps/courses/test_models_course.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from richie.apps.courses import defaults, factories
from richie.apps.courses.cms_plugins import CoursePlugin
from richie.apps.courses.models import Course, CourseRun, CourseRunTranslation, Program
from richie.apps.courses.models.course import CourseRunCatalogVisibility

# pylint: disable=too-many-public-methods

Expand Down Expand Up @@ -941,6 +942,25 @@ def test_models_course_course_runs_enrollment_count(self):
enrollment_count_sum = course.public_extension.course_runs_enrollment_count
self.assertEqual(enrollment_count_sum, 5)

def test_models_course_course_runs_enrollment_count_hidden_run(self):
"""
The `course_runs_enrollment_count` property should not include the hidden course runs.
"""
course = factories.CourseFactory()

# Create random course runs for this course
factories.CourseRunFactory(enrollment_count=30, direct_course=course)
factories.CourseRunFactory(enrollment_count=20, direct_course=course)
factories.CourseRunFactory(
enrollment_count=10,
direct_course=course,
catalog_visibility=CourseRunCatalogVisibility.HIDDEN,
)
course.extended_object.publish("en")

enrollment_count_sum = course.public_extension.course_runs_enrollment_count
self.assertEqual(enrollment_count_sum, 50)

def test_models_course_course_runs_dict(self):
"""The `course_runs_dict` property should be computed on once per request."""
course = factories.CourseFactory()
Expand Down
15 changes: 15 additions & 0 deletions tests/apps/courses/test_templates_course_detail_rdfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
OrganizationFactory,
PersonFactory,
)
from richie.apps.courses.models import CourseRunCatalogVisibility
from richie.plugins.nesteditem.defaults import ACCORDION

# pylint: disable=too-many-lines,too-many-locals,too-many-statements
Expand Down Expand Up @@ -213,6 +214,17 @@ def test_templates_course_detail_rdfa(self):
languages=["de"],
enrollment_count=3000,
)
CourseRunFactory(
title="A hidden course run",
direct_course=course,
start=datetime(2010, 6, 1, tzinfo=timezone.utc),
end=datetime(2050, 7, 10, tzinfo=timezone.utc),
enrollment_start=datetime(2010, 6, 13, tzinfo=timezone.utc),
enrollment_end=datetime(2050, 6, 20, tzinfo=timezone.utc),
languages=["pt"],
enrollment_count=100,
catalog_visibility=CourseRunCatalogVisibility.HIDDEN,
)

contributor1.extended_object.publish("en")
course.extended_object.publish("en")
Expand Down Expand Up @@ -552,6 +564,9 @@ def test_templates_course_detail_rdfa(self):
self.assertTrue(
(course_run_subject, SDO.courseMode, Literal("online")) in graph
)
self.assertTrue(
(course_run_subject, SDO.courseWorkload, Literal("PT3H")) in graph
)

for title in ["Run 0", "Run 1"]:
(course_run_subject,) = graph.subjects(SDO.name, Literal(title))
Expand Down

0 comments on commit 38a9939

Please sign in to comment.