|
| 1 | +""" |
| 2 | +Models for the course to library import app. |
| 3 | +""" |
| 4 | + |
| 5 | +import uuid as uuid_tools |
| 6 | + |
| 7 | +from django.contrib.auth import get_user_model |
| 8 | +from django.db import models |
| 9 | +from django.utils.translation import gettext_lazy as _ |
| 10 | + |
| 11 | +from model_utils.models import TimeStampedModel |
| 12 | +from opaque_keys.edx.django.models import ( |
| 13 | + LearningContextKeyField, |
| 14 | + UsageKeyField, |
| 15 | +) |
| 16 | +from openedx_learning.api.authoring_models import LearningPackage, PublishableEntity |
| 17 | + |
| 18 | +from .data import ImportStatus |
| 19 | + |
| 20 | +User = get_user_model() |
| 21 | + |
| 22 | + |
| 23 | +class Import(TimeStampedModel): |
| 24 | + """ |
| 25 | + Represents the action of a user importing a modulestore-based course or legacy |
| 26 | + library into a learning-core based learning package (today, that is always a content library). |
| 27 | + """ |
| 28 | + |
| 29 | + uuid = models.UUIDField(default=uuid_tools.uuid4, editable=False, unique=True) |
| 30 | + status = models.CharField( |
| 31 | + max_length=100, |
| 32 | + choices=ImportStatus.choices, |
| 33 | + default=ImportStatus.NOT_STARTED, |
| 34 | + db_index=True |
| 35 | + ) |
| 36 | + user = models.ForeignKey(User, on_delete=models.CASCADE) |
| 37 | + |
| 38 | + # Note: For now, this will always be a course key. In the future, it may be a legacy library key. |
| 39 | + source_key = LearningContextKeyField(help_text=_('The modulestore course'), max_length=255, db_index=True) |
| 40 | + target_change = models.ForeignKey(to='oel_publishing.DraftChangeLog', on_delete=models.SET_NULL, null=True) |
| 41 | + |
| 42 | + class Meta: |
| 43 | + verbose_name = _('Import from modulestore') |
| 44 | + verbose_name_plural = _('Imports from modulestore') |
| 45 | + |
| 46 | + def __str__(self): |
| 47 | + return f'{self.source_key} → {self.target_change}' |
| 48 | + |
| 49 | + def set_status(self, status: ImportStatus): |
| 50 | + """ |
| 51 | + Set import status. |
| 52 | + """ |
| 53 | + self.status = status |
| 54 | + self.save() |
| 55 | + if status in [ImportStatus.IMPORTED, ImportStatus.CANCELED]: |
| 56 | + self.clean_related_staged_content() |
| 57 | + |
| 58 | + def clean_related_staged_content(self) -> None: |
| 59 | + """ |
| 60 | + Clean related staged content. |
| 61 | + """ |
| 62 | + for staged_content_for_import in self.staged_content_for_import.all(): |
| 63 | + staged_content_for_import.staged_content.delete() |
| 64 | + |
| 65 | + |
| 66 | +class PublishableEntityMapping(TimeStampedModel): |
| 67 | + """ |
| 68 | + Represents a mapping between a source usage key and a target publishable entity. |
| 69 | + """ |
| 70 | + |
| 71 | + source_usage_key = UsageKeyField( |
| 72 | + max_length=255, |
| 73 | + help_text=_('Original usage key/ID of the thing that has been imported.'), |
| 74 | + ) |
| 75 | + target_package = models.ForeignKey(LearningPackage, on_delete=models.CASCADE) |
| 76 | + target_entity = models.ForeignKey(PublishableEntity, on_delete=models.CASCADE) |
| 77 | + |
| 78 | + class Meta: |
| 79 | + unique_together = ('source_usage_key', 'target_package') |
| 80 | + |
| 81 | + def __str__(self): |
| 82 | + return f'{self.source_usage_key} → {self.target_entity}' |
| 83 | + |
| 84 | + |
| 85 | +class PublishableEntityImport(TimeStampedModel): |
| 86 | + """ |
| 87 | + Represents a publishableentity version that has been imported into a learning package (e.g. content library) |
| 88 | +
|
| 89 | + This is a many-to-many relationship between a container version and a course to library import. |
| 90 | + """ |
| 91 | + |
| 92 | + import_event = models.ForeignKey(Import, on_delete=models.CASCADE) |
| 93 | + resulting_mapping = models.ForeignKey(PublishableEntityMapping, on_delete=models.SET_NULL, null=True, blank=True) |
| 94 | + resulting_change = models.OneToOneField( |
| 95 | + to='oel_publishing.DraftChangeLogRecord', |
| 96 | + # a changelog record can be pruned, which would set this to NULL, but not delete the |
| 97 | + # entire import record |
| 98 | + null=True, |
| 99 | + on_delete=models.SET_NULL, |
| 100 | + ) |
| 101 | + |
| 102 | + class Meta: |
| 103 | + unique_together = ( |
| 104 | + ('import_event', 'resulting_mapping'), |
| 105 | + ) |
| 106 | + |
| 107 | + def __str__(self): |
| 108 | + return f'{self.import_event} → {self.resulting_mapping}' |
| 109 | + |
| 110 | + |
| 111 | +class StagedContentForImport(TimeStampedModel): |
| 112 | + """ |
| 113 | + Represents m2m relationship between an import and staged content created for that import. |
| 114 | + """ |
| 115 | + |
| 116 | + import_event = models.ForeignKey( |
| 117 | + Import, |
| 118 | + on_delete=models.CASCADE, |
| 119 | + related_name='staged_content_for_import', |
| 120 | + ) |
| 121 | + staged_content = models.OneToOneField( |
| 122 | + to='content_staging.StagedContent', |
| 123 | + on_delete=models.CASCADE, |
| 124 | + related_name='staged_content_for_import', |
| 125 | + ) |
| 126 | + # Since StagedContent stores all the keys of the saved blocks, this field was added to optimize search. |
| 127 | + source_usage_key = UsageKeyField( |
| 128 | + max_length=255, |
| 129 | + help_text=_( |
| 130 | + 'The original Usage key of the highest-level component that was saved in StagedContent.' |
| 131 | + ), |
| 132 | + ) |
| 133 | + |
| 134 | + class Meta: |
| 135 | + unique_together = ( |
| 136 | + ('import_event', 'staged_content'), |
| 137 | + ) |
| 138 | + |
| 139 | + def __str__(self): |
| 140 | + return f'{self.import_event} → {self.staged_content}' |
0 commit comments