-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathmodels.py
More file actions
620 lines (552 loc) · 23.4 KB
/
models.py
File metadata and controls
620 lines (552 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
"""
Models for contentstore
"""
import logging
from datetime import datetime, timezone
from itertools import chain
from config_models.models import ConfigurationModel
from django.db import models
from django.db.models import Case, Exists, ExpressionWrapper, OuterRef, Q, QuerySet, Value, When
from django.db.models.fields import BooleanField, IntegerField, TextField
from django.db.models.functions import Coalesce
from django.db.models.lookups import GreaterThan
from django.utils.translation import gettext_lazy as _
from opaque_keys.edx.django.models import ContainerKeyField, CourseKeyField, UsageKeyField
from opaque_keys.edx.keys import CourseKey, UsageKey
from opaque_keys.edx.locator import LibraryContainerLocator
from openedx_learning.api.authoring import get_published_version
from openedx_learning.api.authoring_models import Component, Container
from openedx_learning.lib.fields import (
immutable_uuid_field,
key_field,
manual_date_time_field,
)
logger = logging.getLogger(__name__)
class VideoUploadConfig(ConfigurationModel):
"""
Configuration for the video upload feature.
.. no_pii:
"""
profile_whitelist = TextField(
blank=True,
help_text="A comma-separated list of names of profiles to include in video encoding downloads."
)
@classmethod
def get_profile_whitelist(cls):
"""Get the list of profiles to include in the encoding download"""
return [profile for profile in cls.current().profile_whitelist.split(",") if profile]
class BackfillCourseTabsConfig(ConfigurationModel):
"""
Manages configuration for a run of the backfill_course_tabs management command.
.. no_pii:
"""
class Meta:
verbose_name = 'Arguments for backfill_course_tabs'
verbose_name_plural = 'Arguments for backfill_course_tabs'
start_index = IntegerField(
help_text='Index of first course to start backfilling (in an alphabetically sorted list of courses)',
default=0,
)
count = IntegerField(
help_text='How many courses to backfill in this run (or zero for all courses)',
default=0,
)
class CleanStaleCertificateAvailabilityDatesConfig(ConfigurationModel):
"""
Manages configuration for a run of the `clean_stale_certificate_availability_dates` management command.
.. no_pii:
"""
class Meta:
app_label = "contentstore"
verbose_name = "Arguments for 'clean_stale_certificate_availability_dates'"
verbose_name_plural = "Arguments for 'clean_stale_certificate_availability_dates'"
arguments = TextField(
blank=True,
help_text=(
"A space seperated collection of arguments to be used when running the "
"`clean_stale_certificate_available_dates` management command.' See the management command for options."
)
)
class EntityLinkBase(models.Model):
"""
Abstract base class that defines fields and functions for storing link between two publishable entities
or links between publishable entity and a course xblock.
"""
uuid = immutable_uuid_field()
# Search by library/upstream context key
upstream_context_key = key_field(
help_text=_("Upstream context key i.e., learning_package/library key"),
db_index=True,
)
# A downstream entity can only link to single upstream entity
# whereas an entity can be upstream for multiple downstream entities.
downstream_usage_key = UsageKeyField(max_length=255, unique=True)
# Search by course/downstream key
downstream_context_key = CourseKeyField(max_length=255, db_index=True)
# This is present if the creation of this link is a consequence of
# importing a container that has one or more levels of children.
# This represents the parent (container) in the top level
# at the moment of the import.
top_level_parent = models.ForeignKey("ContainerLink", on_delete=models.SET_NULL, null=True, blank=True)
version_synced = models.IntegerField()
version_declined = models.IntegerField(null=True, blank=True)
downstream_customized = models.JSONField(
default=list,
help_text=(
'Names of the fields which have values set on the upstream block yet have been explicitly'
' overridden on this downstream block'
),
)
created = manual_date_time_field()
updated = manual_date_time_field()
@property
def upstream_context_title(self) -> str:
"""
Returns upstream context title.
"""
raise NotImplementedError
@property
def published_at(self) -> str | None:
"""
Returns the published date of the entity
"""
raise NotImplementedError
class Meta:
abstract = True
@classmethod
def get_by_downstream_usage_key(cls, downstream_usage_key: UsageKey):
return cls.objects.get(downstream_usage_key=downstream_usage_key)
class ComponentLink(EntityLinkBase):
"""
This represents link between any two publishable entities or link between publishable entity and a course
XBlock. It helps in tracking relationship between XBlocks imported from libraries and used in different courses.
"""
upstream_block = models.ForeignKey(
Component,
on_delete=models.SET_NULL,
related_name="links",
null=True,
blank=True,
)
upstream_usage_key = UsageKeyField(
max_length=255,
help_text=_(
"Upstream block usage key, this value cannot be null"
" and useful to track upstream library blocks that do not exist yet"
)
)
class Meta:
verbose_name = _("Component Link")
verbose_name_plural = _("Component Links")
def __str__(self):
return f"ComponentLink<{self.upstream_usage_key}->{self.downstream_usage_key}>"
@property
def upstream_version_num(self) -> int | None:
"""
Returns upstream block version number if available.
"""
published_version = get_published_version(self.upstream_block.publishable_entity.id)
return published_version.version_num if published_version else None
@property
def upstream_context_title(self) -> str:
"""
Returns upstream context title.
"""
return self.upstream_block.publishable_entity.learning_package.title
@property
def published_at(self) -> str | None:
"""
Returns the published date of the component
"""
if self.upstream_block.publishable_entity.published is None:
raise AttributeError(_("The component must be published to access `published_at`"))
return self.upstream_block.publishable_entity.published.publish_log_record.publish_log.published_at
@classmethod
def filter_links(
cls,
*,
use_top_level_parents=False,
**link_filter,
) -> QuerySet["EntityLinkBase"] | list["EntityLinkBase"]:
"""
Get all links along with sync flag, upstream context title and version, with optional filtering.
`use_top_level_parents` is an special filter, replace any result with the top-level parent if exists.
Example: We have linkA and linkB with top-level parent as linkC, and linkD without top-level parent.
After all other filters:
Case 1: `use_top_level_parents` is False, the result is [linkA, linkB, linkC, linkD]
Case 2: `use_top_level_parents` is True, the result is [linkC, linkD]
"""
RELATED_FIELDS = [
"upstream_block__publishable_entity__published__version",
"upstream_block__publishable_entity__learning_package",
"upstream_block__publishable_entity__published__publish_log_record__publish_log",
]
ready_to_sync = link_filter.pop('ready_to_sync', None)
result = cls.objects.filter(**link_filter).select_related(*RELATED_FIELDS).annotate(
ready_to_sync=(
GreaterThan(
Coalesce("upstream_block__publishable_entity__published__version__version_num", 0),
Coalesce("version_synced", 0)
) & GreaterThan(
Coalesce("upstream_block__publishable_entity__published__version__version_num", 0),
Coalesce("version_declined", 0)
)
),
# This is alwys False, the components doens't have children
ready_to_sync_from_children=Value(False, output_field=BooleanField())
)
if ready_to_sync is not None:
result = result.filter(ready_to_sync=ready_to_sync)
# Handle top-level parents logic
if use_top_level_parents:
# Get objects without top_level_parent
objects_without_top_level = result.filter(top_level_parent__isnull=True)
# Get the top-level parent keys
top_level_keys = result.filter(top_level_parent__isnull=False).values_list(
'top_level_parent', flat=True,
)
# Get the top-level parents
# Any top-level parent is a container
top_level_objects = ContainerLink.filter_links(**{
"id__in": top_level_keys
})
# Returns a list of `EntityLinkBase` as can be a combination of `ComponentLink``
# and `ContainerLink``
return list(chain(top_level_objects, objects_without_top_level))
return result
@classmethod
def update_or_create(
cls,
upstream_block: Component | None,
/,
upstream_usage_key: UsageKey,
upstream_context_key: str,
downstream_usage_key: UsageKey,
downstream_context_key: CourseKey,
version_synced: int,
top_level_parent_usage_key: UsageKey | None = None,
version_declined: int | None = None,
downstream_customized: list[str] | None = None,
created: datetime | None = None,
) -> "ComponentLink":
"""
Update or create entity link. This will only update `updated` field if something has changed.
"""
if not created:
created = datetime.now(tz=timezone.utc)
top_level_parent = None
if top_level_parent_usage_key is not None:
try:
top_level_parent = ContainerLink.get_by_downstream_usage_key(
top_level_parent_usage_key,
)
except ContainerLink.DoesNotExist:
logger.info(f"Unable to find the link for the container with the link: {top_level_parent_usage_key}")
new_values = {
'upstream_usage_key': upstream_usage_key,
'upstream_context_key': upstream_context_key,
'downstream_usage_key': downstream_usage_key,
'downstream_context_key': downstream_context_key,
'version_synced': version_synced,
'version_declined': version_declined,
'top_level_parent': top_level_parent,
'downstream_customized': downstream_customized,
}
if upstream_block:
new_values['upstream_block'] = upstream_block
try:
link = cls.objects.get(downstream_usage_key=downstream_usage_key)
has_changes = False
for key, new_value in new_values.items():
prev_value = getattr(link, key)
if prev_value != new_value:
has_changes = True
setattr(link, key, new_value)
if has_changes:
link.updated = created
link.save()
except cls.DoesNotExist:
link = cls(**new_values)
link.created = created
link.updated = created
link.save()
return link
class ContainerLink(EntityLinkBase):
"""
This represents link between any two publishable entities or link between publishable entity and a course
xblock. It helps in tracking relationship between xblocks imported from libraries and used in different courses.
"""
upstream_container = models.ForeignKey(
Container,
on_delete=models.SET_NULL,
related_name="links",
null=True,
blank=True,
)
upstream_container_key = ContainerKeyField(
max_length=255,
help_text=_(
"Upstream block key (e.g. lct:...), this value cannot be null "
"and is useful to track upstream library blocks that do not exist yet "
"or were deleted."
)
)
class Meta:
verbose_name = _("Container Link")
verbose_name_plural = _("Container Links")
def __str__(self):
return f"ContainerLink<{self.upstream_container_key}->{self.downstream_usage_key}>"
@property
def upstream_version_num(self) -> int | None:
"""
Returns upstream container version number if available.
"""
published_version = get_published_version(self.upstream_container.publishable_entity.id)
return published_version.version_num if published_version else None
@property
def upstream_context_title(self) -> str:
"""
Returns upstream context title.
"""
return self.upstream_container.publishable_entity.learning_package.title
@property
def published_at(self) -> str | None:
"""
Returns the published date of the container
"""
if self.upstream_container.publishable_entity.published is None:
raise AttributeError(_("The container must be published to access `published_at`"))
return self.upstream_container.publishable_entity.published.publish_log_record.publish_log.published_at
@classmethod
def filter_links(
cls,
*,
use_top_level_parents=False,
**link_filter,
) -> QuerySet["EntityLinkBase"]:
"""
Get all links along with sync flag, upstream context title and version, with optional filtering.
`use_top_level_parents` is an special filter, replace any result with the top-level parent if exists.
Example: We have linkA and linkB with top-level parent as linkC and linkD without top-level parent.
After all other filters:
Case 1: `use_top_level_parents` is False, the result is [linkA, linkB, linkC, linkD]
Case 2: `use_top_level_parents` is True, the result is [linkC, linkD]
"""
RELATED_FIELDS = [
"upstream_container__publishable_entity__published__version",
"upstream_container__publishable_entity__learning_package",
"upstream_container__publishable_entity__published__publish_log_record__publish_log",
]
ready_to_sync = link_filter.pop('ready_to_sync', None)
result = cls._annotate_query_with_ready_to_sync(
cls.objects.filter(**link_filter).select_related(*RELATED_FIELDS),
)
if ready_to_sync is not None:
result = result.filter(Q(ready_to_sync=ready_to_sync) | Q(ready_to_sync_from_children=ready_to_sync))
# Handle top-level parents logic
if use_top_level_parents:
# Get objects without top_level_parent
objects_without_top_level = result.filter(top_level_parent__isnull=True)
# Get the top-level parent keys
top_level_keys = result.filter(top_level_parent__isnull=False).values_list(
'top_level_parent', flat=True,
)
# Get the top-level parents
# Any top-level parent is a container
top_level_objects = cls._annotate_query_with_ready_to_sync(cls.objects.filter(
id__in=top_level_keys,
).select_related(*RELATED_FIELDS))
result = top_level_objects.union(objects_without_top_level)
return result
@classmethod
def _annotate_query_with_ready_to_sync(cls, query_set: QuerySet["EntityLinkBase"]) -> QuerySet["EntityLinkBase"]:
"""
Adds ready to sync related values to the query set:
* `ready_to_sync`: When the container is ready to sync.
* `ready_to_sync_from_children`: When any children is ready to sync.
"""
# SubQuery to verify if some container children (associated with top-level parent)
# needs sync.
subq_container = cls.objects.filter(
top_level_parent=OuterRef('pk')
).annotate(
child_ready=Case(
When(
GreaterThan(
Coalesce("upstream_container__publishable_entity__published__version__version_num", 0),
Coalesce("version_synced", 0)
) & GreaterThan(
Coalesce("upstream_container__publishable_entity__published__version__version_num", 0),
Coalesce("version_declined", 0)
),
then=1
),
# If upstream block was deleted, set ready_to_sync = True
When(
Q(upstream_container__publishable_entity__published__version__version_num__isnull=True),
then=1
),
default=0,
output_field=models.IntegerField()
)
).filter(child_ready=1)
# SubQuery to verify if some component children (assisiated with top-level parent)
# needs sync.
subq_components = ComponentLink.objects.filter(
top_level_parent=OuterRef('pk')
).annotate(
child_ready=Case(
When(
GreaterThan(
Coalesce("upstream_block__publishable_entity__published__version__version_num", 0),
Coalesce("version_synced", 0)
) & GreaterThan(
Coalesce("upstream_block__publishable_entity__published__version__version_num", 0),
Coalesce("version_declined", 0)
),
then=1
),
# If upstream block was deleted, set ready_to_sync = True
When(
Q(upstream_block__publishable_entity__published__version__version_num__isnull=True),
then=1
),
default=0,
output_field=models.IntegerField()
)
).filter(child_ready=1)
# TODO: is there a way to run `subq_container` or `subq_components` depending on the container type?
return query_set.annotate(
ready_to_sync=(
GreaterThan(
Coalesce("upstream_container__publishable_entity__published__version__version_num", 0),
Coalesce("version_synced", 0)
) & GreaterThan(
Coalesce("upstream_container__publishable_entity__published__version__version_num", 0),
Coalesce("version_declined", 0)
)
),
ready_to_sync_from_children=ExpressionWrapper(
Exists(subq_container) | Exists(subq_components),
output_field=BooleanField(),
),
)
@classmethod
def update_or_create(
cls,
upstream_container_id: int | None,
/,
upstream_container_key: LibraryContainerLocator,
upstream_context_key: str,
downstream_usage_key: UsageKey,
downstream_context_key: CourseKey,
version_synced: int,
top_level_parent_usage_key: UsageKey | None = None,
version_declined: int | None = None,
downstream_customized: list[str] | None = None,
created: datetime | None = None,
) -> "ContainerLink":
"""
Update or create entity link. This will only update `updated` field if something has changed.
"""
if not created:
created = datetime.now(tz=timezone.utc)
top_level_parent = None
if top_level_parent_usage_key is not None:
try:
top_level_parent = ContainerLink.get_by_downstream_usage_key(
top_level_parent_usage_key,
)
except ContainerLink.DoesNotExist:
logger.info(f"Unable to find the link for the container with the link: {top_level_parent_usage_key}")
new_values = {
'upstream_container_key': upstream_container_key,
'upstream_context_key': upstream_context_key,
'downstream_usage_key': downstream_usage_key,
'downstream_context_key': downstream_context_key,
'version_synced': version_synced,
'version_declined': version_declined,
'top_level_parent': top_level_parent,
'downstream_customized': downstream_customized,
}
if upstream_container_id:
new_values['upstream_container_id'] = upstream_container_id
try:
link = cls.objects.get(downstream_usage_key=downstream_usage_key)
has_changes = False
for key, new_value in new_values.items():
prev_value = getattr(link, key)
if prev_value != new_value:
has_changes = True
setattr(link, key, new_value)
if has_changes:
link.updated = created
link.save()
except cls.DoesNotExist:
link = cls(**new_values)
link.created = created
link.updated = created
link.save()
return link
class LearningContextLinksStatusChoices(models.TextChoices):
"""
Enumerates the states that a LearningContextLinksStatus can be in.
"""
PENDING = "pending", _("Pending")
PROCESSING = "processing", _("Processing")
FAILED = "failed", _("Failed")
COMPLETED = "completed", _("Completed")
class LearningContextLinksStatus(models.Model):
"""
This table stores current processing status of upstream-downstream links in ComponentLink table for a
course or a learning context.
"""
context_key = CourseKeyField(
max_length=255,
# Single entry for a learning context or course
unique=True,
help_text=_("Linking status for course context key"),
)
status = models.CharField(
max_length=20,
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:
verbose_name = _("Learning Context Links status")
verbose_name_plural = _("Learning Context Links status")
def __str__(self):
return f"{self.status}|{self.context_key}"
@classmethod
def get_or_create(cls, context_key: str, created: datetime | None = None) -> "LearningContextLinksStatus":
"""
Get or create course link status row from LearningContextLinksStatus table for given course key.
Args:
context_key: Learning context or Course key
Returns:
LearningContextLinksStatus object
"""
if not created:
created = datetime.now(tz=timezone.utc)
status, _ = cls.objects.get_or_create(
context_key=context_key,
defaults={
'status': LearningContextLinksStatusChoices.PENDING,
'created': created,
'updated': created,
},
)
return status
def update_status(
self,
status: LearningContextLinksStatusChoices,
updated: datetime | None = None
) -> None:
"""
Updates entity links processing status of given learning context.
"""
self.status = status
self.updated = updated or datetime.now(tz=timezone.utc)
self.save()