-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4604 from akolson/embed-content-logic
Extract content urls
- Loading branch information
Showing
10 changed files
with
843 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 3.2.24 on 2024-08-05 21:23 | ||
import uuid | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations | ||
from django.db import models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [ | ||
('kolibri_public', '0003_alter_file_preset'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='RecommendationsCache', | ||
fields=[ | ||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, | ||
serialize=False)), | ||
('request_hash', models.CharField(max_length=32, null=True)), | ||
('rank', models.FloatField(default=0.0, null=True)), | ||
('override_threshold', models.BooleanField(default=False)), | ||
('timestamp', models.DateTimeField(auto_now_add=True)), | ||
('contentnode', models.ForeignKey(blank=True, null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name='recommendations', | ||
to='kolibri_public.contentnode')), | ||
], | ||
), | ||
migrations.AddIndex( | ||
model_name='recommendationscache', | ||
index=models.Index(fields=['request_hash'], name='request_hash_idx'), | ||
), | ||
migrations.AddIndex( | ||
model_name='recommendationscache', | ||
index=models.Index(fields=['contentnode'], name='contentnode_idx'), | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='recommendationscache', | ||
unique_together={('request_hash', 'contentnode')}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
# from django.db import models | ||
import uuid | ||
|
||
# Create your models here. | ||
from django.db import models | ||
from kolibri_public.models import ContentNode | ||
|
||
|
||
REQUEST_HASH_INDEX_NAME = "request_hash_idx" | ||
CONTENTNODE_INDEX_NAME = "contentnode_idx" | ||
|
||
|
||
class RecommendationsCache(models.Model): | ||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) | ||
request_hash = models.CharField(max_length=32, null=True) | ||
contentnode = models.ForeignKey( | ||
ContentNode, | ||
null=True, | ||
blank=True, | ||
related_name='recommendations', | ||
on_delete=models.CASCADE, | ||
) | ||
rank = models.FloatField(default=0.0, null=True) | ||
override_threshold = models.BooleanField(default=False) | ||
timestamp = models.DateTimeField(auto_now_add=True) | ||
|
||
class Meta: | ||
unique_together = ('request_hash', 'contentnode') | ||
indexes = [ | ||
models.Index(fields=['request_hash'], name=REQUEST_HASH_INDEX_NAME), | ||
models.Index(fields=['contentnode'], name=CONTENTNODE_INDEX_NAME), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
contentcuration/automation/tests/test_recommendations_cache_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import uuid | ||
|
||
from automation.models import RecommendationsCache | ||
from django.db import IntegrityError | ||
from kolibri_public.models import ContentNode | ||
|
||
from contentcuration.tests.base import StudioTestCase | ||
|
||
|
||
class TestRecommendationsCache(StudioTestCase): | ||
|
||
def setUp(self): | ||
self.content_node = ContentNode.objects.create( | ||
id=uuid.uuid4(), | ||
title='Test Content Node', | ||
content_id=uuid.uuid4(), | ||
channel_id=uuid.uuid4(), | ||
) | ||
self.cache = RecommendationsCache.objects.create( | ||
request_hash='test_hash', | ||
contentnode=self.content_node, | ||
rank=1.0, | ||
override_threshold=False | ||
) | ||
|
||
def test_cache_creation(self): | ||
self.assertIsInstance(self.cache, RecommendationsCache) | ||
self.assertEqual(self.cache.request_hash, 'test_hash') | ||
self.assertEqual(self.cache.contentnode, self.content_node) | ||
self.assertEqual(self.cache.rank, 1.0) | ||
self.assertFalse(self.cache.override_threshold) | ||
|
||
def test_cache_retrieval(self): | ||
retrieved_cache = RecommendationsCache.objects.get(request_hash='test_hash') | ||
self.assertEqual(retrieved_cache, self.cache) | ||
|
||
def test_cache_uniqueness(self): | ||
with self.assertRaises(IntegrityError): | ||
RecommendationsCache.objects.create( | ||
request_hash='test_hash', | ||
contentnode=self.content_node, | ||
rank=2.0, | ||
override_threshold=True | ||
) | ||
|
||
def test_bulk_create_ignore_conflicts_true(self): | ||
initial_count = RecommendationsCache.objects.count() | ||
try: | ||
RecommendationsCache.objects.bulk_create( | ||
[self.cache, self.cache], | ||
ignore_conflicts=True | ||
) | ||
except IntegrityError: | ||
self.fail("bulk_create raised IntegrityError unexpectedly!") | ||
|
||
final_count = RecommendationsCache.objects.count() | ||
self.assertEqual(initial_count, final_count) | ||
|
||
def test_bulk_create_ignore_conflicts_false(self): | ||
with self.assertRaises(IntegrityError): | ||
RecommendationsCache.objects.bulk_create( | ||
[self.cache, self.cache], | ||
ignore_conflicts=False | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.