Skip to content

Commit

Permalink
refactor: rename table and update upstream_context_key field
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Jan 17, 2025
1 parent e5cd584 commit d287508
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 63 deletions.
8 changes: 4 additions & 4 deletions openedx_learning/apps/authoring/linking/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from openedx_learning.lib.admin_utils import ReadOnlyModelAdmin

from .models import CourseLinksStatus, PublishableEntityLink
from .models import LearningContextLinksStatus, PublishableEntityLink


@admin.register(PublishableEntityLink)
Expand Down Expand Up @@ -45,10 +45,10 @@ class PublishableEntityLinkAdmin(ReadOnlyModelAdmin):
]


@admin.register(CourseLinksStatus)
class CourseLinksStatusAdmin(admin.ModelAdmin):
@admin.register(LearningContextLinksStatus)
class LearningContextLinksStatusAdmin(admin.ModelAdmin):
"""
CourseLinksStatus admin.
LearningContextLinksStatus admin.
"""
fields = (
"context_key",
Expand Down
54 changes: 30 additions & 24 deletions openedx_learning/apps/authoring/linking/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Please look at the models.py file for more information about the kinds of data
are stored in this app.
"""

from __future__ import annotations

from datetime import datetime, timezone
Expand All @@ -12,35 +13,37 @@
from django.db.models import QuerySet

from ..components.models import Component
from .models import CourseLinksStatus, CourseLinksStatusChoices, PublishableEntityLink
from .models import LearningContextLinksStatus, LearningContextLinksStatusChoices, PublishableEntityLink

__all__ = [
"delete_entity_link",
"get_entity_links",
"get_or_create_course_link_status",
"update_or_create_entity_link",
'delete_entity_link',
'get_entity_links',
'get_or_create_learning_context_link_status',
'update_or_create_entity_link',
]


def get_or_create_course_link_status(context_key: str, created: datetime | None = None) -> CourseLinksStatus:
def get_or_create_learning_context_link_status(
context_key: str, created: datetime | None = None
) -> LearningContextLinksStatus:
"""
Get or create course link status row from CourseLinksStatus table for given course key.
Get or create course link status row from LearningContextLinksStatus table for given course key.
Args:
context_key: Course key
context_key: Learning context or Course key
Returns:
CourseLinksStatus object
LearningContextLinksStatus object
"""
if not created:
created = datetime.now(tz=timezone.utc)
status, _ = CourseLinksStatus.objects.get_or_create(
status, _ = LearningContextLinksStatus.objects.get_or_create(
context_key=context_key,
defaults={
"status": CourseLinksStatusChoices.PENDING,
"created": created,
"updated": created,
}
'status': LearningContextLinksStatusChoices.PENDING,
'created': created,
'updated': created,
},
)
return status

Expand All @@ -56,6 +59,7 @@ def update_or_create_entity_link(
upstream_block: Component | None,
/,
upstream_usage_key: str,
upstream_context_key: str,
downstream_usage_key: str,
downstream_context_key: str,
downstream_context_title: str,
Expand All @@ -69,18 +73,20 @@ def update_or_create_entity_link(
if not created:
created = datetime.now(tz=timezone.utc)
new_values = {
"upstream_usage_key": upstream_usage_key,
"downstream_usage_key": downstream_usage_key,
"downstream_context_key": downstream_context_key,
"downstream_context_title": downstream_context_title,
"version_synced": version_synced,
"version_declined": version_declined,
'upstream_usage_key': upstream_usage_key,
'upstream_context_key': upstream_context_key,
'downstream_usage_key': downstream_usage_key,
'downstream_context_key': downstream_context_key,
'downstream_context_title': downstream_context_title,
'version_synced': version_synced,
'version_declined': version_declined,
}
if upstream_block:
new_values.update({
"upstream_block": upstream_block.publishable_entity,
"upstream_context_key": upstream_block.learning_package.key,
})
new_values.update(
{
'upstream_block': upstream_block.publishable_entity,
}
)
try:
link = PublishableEntityLink.objects.get(downstream_usage_key=downstream_usage_key)
has_changes = False
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.17 on 2025-01-07 12:17
# Generated by Django 4.2.15 on 2025-01-17 14:23

import uuid

Expand All @@ -18,7 +18,7 @@ class Migration(migrations.Migration):

operations = [
migrations.CreateModel(
name='CourseLinksStatus',
name='LearningContextLinksStatus',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
(
Expand All @@ -38,16 +38,16 @@ class Migration(migrations.Migration):
('failed', 'Failed'),
('completed', 'Completed'),
],
help_text='Status of links in given course.',
help_text='Status of links in given learning context/course.',
max_length=20,
),
),
('created', models.DateTimeField(validators=[openedx_learning.lib.validators.validate_utc_datetime])),
('updated', models.DateTimeField(validators=[openedx_learning.lib.validators.validate_utc_datetime])),
],
options={
'verbose_name': 'Course Links status',
'verbose_name_plural': 'Course Links status',
'verbose_name': 'Learning Context Links status',
'verbose_name_plural': 'Learning Context Links status',
},
),
migrations.CreateModel(
Expand All @@ -69,11 +69,9 @@ class Migration(migrations.Migration):
(
'upstream_context_key',
openedx_learning.lib.fields.MultiCollationCharField(
blank=True,
db_collations={'mysql': 'utf8mb4_bin', 'sqlite': 'BINARY'},
help_text='Upstream context key i.e., learning_package/library key',
max_length=500,
null=True,
),
),
(
Expand Down Expand Up @@ -117,7 +115,7 @@ class Migration(migrations.Migration):
},
),
migrations.AddConstraint(
model_name='courselinksstatus',
model_name='learningcontextlinksstatus',
constraint=models.UniqueConstraint(fields=('context_key',), name='oel_link_ent_status_ctx_key'),
),
migrations.AddIndex(
Expand Down
32 changes: 14 additions & 18 deletions openedx_learning/apps/authoring/linking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from openedx_learning.lib.fields import (
case_insensitive_char_field,
case_sensitive_char_field,
immutable_uuid_field,
key_field,
manual_date_time_field,
Expand All @@ -16,8 +15,8 @@

__all__ = [
"PublishableEntityLink",
"CourseLinksStatus",
"CourseLinksStatusChoices"
"LearningContextLinksStatus",
"LearningContextLinksStatusChoices"
]


Expand All @@ -40,13 +39,10 @@ class PublishableEntityLink(models.Model):
" and useful to track upstream library blocks that do not exist yet"
)
)
upstream_context_key = case_sensitive_char_field(
max_length=500,
upstream_context_key = key_field(
help_text=_(
"Upstream context key i.e., learning_package/library key"
),
null=True,
blank=True,
)
downstream_usage_key = key_field()
downstream_context_key = key_field()
Expand Down Expand Up @@ -87,46 +83,46 @@ class Meta:
name="oel_link_ent_idx_up_ctx_key"
),
]
verbose_name = "Publishable Entity Link"
verbose_name_plural = "Publishable Entity Links"
verbose_name = _("Publishable Entity Link")
verbose_name_plural = _("Publishable Entity Links")


class CourseLinksStatusChoices(models.TextChoices):
class LearningContextLinksStatusChoices(models.TextChoices):
"""
Enumerates the states that a CourseLinksStatus can be in.
Enumerates the states that a LearningContextLinksStatus can be in.
"""
PENDING = "pending", _("Pending")
PROCESSING = "processing", _("Processing")
FAILED = "failed", _("Failed")
COMPLETED = "completed", _("Completed")


class CourseLinksStatus(models.Model):
class LearningContextLinksStatus(models.Model):
"""
This table stores current processing status of upstream-downstream links in PublishableEntityLink table for a
course.
course or a learning context.
"""
context_key = key_field(
help_text=_("Linking status for downstream/course context key"),
)
status = models.CharField(
max_length=20,
choices=CourseLinksStatusChoices.choices,
help_text=_("Status of links in given course."),
choices=LearningContextLinksStatusChoices.choices,
help_text=_("Status of links in given learning context/course."),
)
created = manual_date_time_field()
updated = manual_date_time_field()

class Meta:
constraints = [
# Single entry for a course
# Single entry for a learning context or course
models.UniqueConstraint(
fields=["context_key"],
name="oel_link_ent_status_ctx_key",
)
]
verbose_name = "Course Links status"
verbose_name_plural = "Course Links status"
verbose_name = _("Learning Context Links status")
verbose_name_plural = _("Learning Context Links status")

def __str__(self):
return f"{self.status}|{self.context_key}"
20 changes: 11 additions & 9 deletions tests/openedx_learning/apps/authoring/linking/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from openedx_learning.apps.authoring.components import api as components_api
from openedx_learning.apps.authoring.components.models import Component, ComponentType
from openedx_learning.apps.authoring.linking import api as linking_api
from openedx_learning.apps.authoring.linking.models import CourseLinksStatus, PublishableEntityLink
from openedx_learning.apps.authoring.linking.models import LearningContextLinksStatus, PublishableEntityLink
from openedx_learning.apps.authoring.publishing import api as publishing_api
from openedx_learning.apps.authoring.publishing.models import LearningPackage
from openedx_learning.lib.test_utils import TestCase
Expand Down Expand Up @@ -40,18 +40,18 @@ def setUpTestData(cls) -> None:
created_by=None,
)

def test_get_or_create_course_link_status(self) -> None:
def test_get_or_create_learning_context_link_status(self) -> None:
"""
Test get_or_create_course_link_status api.
Test get_or_create_learning_context_link_status api.
"""
context_key = "test_context_key"
assert not CourseLinksStatus.objects.filter(context_key=context_key).exists()
linking_api.get_or_create_course_link_status(context_key)
assert CourseLinksStatus.objects.filter(context_key=context_key).exists()
assert CourseLinksStatus.objects.filter(context_key=context_key).count() == 1
assert not LearningContextLinksStatus.objects.filter(context_key=context_key).exists()
linking_api.get_or_create_learning_context_link_status(context_key)
assert LearningContextLinksStatus.objects.filter(context_key=context_key).exists()
assert LearningContextLinksStatus.objects.filter(context_key=context_key).count() == 1
# Should not create a new object
linking_api.get_or_create_course_link_status(context_key)
assert CourseLinksStatus.objects.filter(context_key=context_key).count() == 1
linking_api.get_or_create_learning_context_link_status(context_key)
assert LearningContextLinksStatus.objects.filter(context_key=context_key).count() == 1

def test_update_or_create_entity_link(self) -> None:
"""
Expand All @@ -61,6 +61,7 @@ def test_update_or_create_entity_link(self) -> None:
assert not PublishableEntityLink.objects.filter(downstream_usage_key=downstream_usage_key).exists()
entity_args = {
"upstream_usage_key": "test_upstream_usage_key",
"upstream_context_key": "test_upstream_context_key",
"downstream_usage_key": downstream_usage_key,
"downstream_context_key": "test_downstream_context_key",
"downstream_context_title": "test_downstream_context_key",
Expand Down Expand Up @@ -88,6 +89,7 @@ def test_delete_entity_link(self) -> None:
downstream_usage_key = "test_downstream_usage_key"
entity_args = {
"upstream_usage_key": "test_upstream_usage_key",
"upstream_context_key": "test_upstream_context_key",
"downstream_usage_key": downstream_usage_key,
"downstream_context_key": "test_downstream_context_key",
"downstream_context_title": "test_downstream_context_key",
Expand Down

0 comments on commit d287508

Please sign in to comment.