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