Fix find bookmarks algorithm
This commit is contained in:
parent
861db768cd
commit
cbdff6a782
|
|
@ -22,7 +22,7 @@ logger = get_logger(__name__)
|
|||
|
||||
content_dict = {
|
||||
'assignment': 'Auftrag',
|
||||
'basic_knowledge': 'dsafasdfasdf',
|
||||
'basic_knowledge': '',
|
||||
'survey': 'Übung',
|
||||
'image_block': 'Bild',
|
||||
'link_block': 'Link',
|
||||
|
|
@ -37,30 +37,49 @@ content_dict = {
|
|||
'cms_document_block': 'Dokument',
|
||||
}
|
||||
|
||||
from collections import deque
|
||||
|
||||
def find_content_bfs(data, target_id):
|
||||
"""
|
||||
breadth-first search to find an object by its ID in a nested dictionary structure
|
||||
"""
|
||||
# Initialize a queue with the initial data list
|
||||
queue = deque(data)
|
||||
|
||||
# Traverse through the data using BFS
|
||||
while queue:
|
||||
current = queue.popleft()
|
||||
|
||||
# Check if the current dictionary has the target_id
|
||||
if current.get("id", "") == target_id:
|
||||
return current
|
||||
|
||||
# If 'value' in current and it is a list, extend the queue with the items in 'value'
|
||||
if isinstance(current.get("value"), list):
|
||||
queue.extend(current["value"])
|
||||
|
||||
return None # If no matching ID is found
|
||||
|
||||
|
||||
def find_content(content_list, bookmark):
|
||||
found = (content for content in content_list if uuid.UUID(content['id']) == bookmark.uuid)
|
||||
content = next(found, None)
|
||||
if content is None:
|
||||
for c in content_list:
|
||||
if c.get('id') == bookmark.uuid:
|
||||
return c.get('value').get('text', '')
|
||||
return c.get('value').get('text', '')
|
||||
content = find_content_bfs(content_list, str(bookmark.uuid))
|
||||
if not content:
|
||||
logger.warn(f"Content not found: {bookmark.uuid}")
|
||||
return ''
|
||||
if content['type'] in ['text_block', 'subtitle', 'solution']:
|
||||
|
||||
if content['type'] in ['text_block', 'subtitle', 'solution', 'section_title']:
|
||||
return content['value'].get('text', '')
|
||||
|
||||
if content['type'] in ['basic_knowledge']:
|
||||
if type(content['value']) is dict:
|
||||
description = content['value'].get('description', '')
|
||||
if not description:
|
||||
try:
|
||||
return BasicKnowledge.objects.get(pk=content['value']['basic_knowledge']).title
|
||||
except BasicKnowledge.DoesNotExist:
|
||||
return ''
|
||||
else:
|
||||
return description
|
||||
else:
|
||||
logger.warn(f"Content value is not a dict: {content['value']}")
|
||||
description = content['value'].get('description', '')
|
||||
if not description:
|
||||
try:
|
||||
return BasicKnowledge.objects.get(pk=content['value']['basic_knowledge']).title
|
||||
except BasicKnowledge.DoesNotExist:
|
||||
return ''
|
||||
return description
|
||||
if content_dict.get(content['type'], '') == "":
|
||||
logger.warn(f"Content type not found: {content['type']}")
|
||||
return content_dict.get(content['type'], '')
|
||||
|
||||
class NoteNode(DjangoObjectType):
|
||||
|
|
|
|||
|
|
@ -0,0 +1,87 @@
|
|||
import json
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from books.factories import InstrumentFactory, ContentBlockFactory, ModuleFactory
|
||||
from notes.models import InstrumentBookmark
|
||||
from notes.schema import find_content
|
||||
from users.models import User
|
||||
from users.services import create_users
|
||||
|
||||
|
||||
class TestGetBookmarkContent(TestCase):
|
||||
def setUp(self):
|
||||
create_users()
|
||||
self.student1 = User.objects.get(username='student1')
|
||||
self.instrument = InstrumentFactory(title="My Instrument", slug='instrument-slug')
|
||||
|
||||
def test_content_from_content_list_item(self):
|
||||
content_list = [
|
||||
{
|
||||
"id": "45c5d9e7-233b-4ec7-a539-4136f2f969a4",
|
||||
"type": "content_list_item",
|
||||
"value": [
|
||||
{
|
||||
"id": "4eb6a9ec-576a-4bec-8eb7-798ac21e88cd",
|
||||
"type": "text_block",
|
||||
"value": {"text": "<p>Schauen Sie sich die</p>"}
|
||||
},
|
||||
{
|
||||
"id": "718bee9a-5f92-4e3d-b7f8-aa63b8f2497e",
|
||||
"type": "link_block",
|
||||
"value": {"url": "https://skilM2_A2_Studie.pdf", "text": "Studie anzeigen"}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "6a72b706-5ad7-4c8b-9fa1-e89909a2a5e6",
|
||||
"type": "content_list_item",
|
||||
"value": [
|
||||
{
|
||||
"id": "b7cd58c7-3131-4e3c-a51a-cd0026be03f8",
|
||||
"type": "assignment",
|
||||
"value": {"assignment_id": 204}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
bookmark = InstrumentBookmark.objects.create(
|
||||
instrument=self.instrument,
|
||||
uuid='4eb6a9ec-576a-4bec-8eb7-798ac21e88cd',
|
||||
user=self.student1
|
||||
)
|
||||
content = find_content(content_list, bookmark)
|
||||
|
||||
self.assertEqual("<p>Schauen Sie sich die</p>", content)
|
||||
|
||||
def test_content_from_instrument(self):
|
||||
self.instrument = InstrumentFactory(title="My Instrument", slug='instrument-slug-sdfe')
|
||||
|
||||
content_list = [{'id': '6dd39d83-e442-4f3c-8d3f-e7e8026b0c4b',
|
||||
'type': 'basic_knowledge',
|
||||
'value': {'description': '', 'basic_knowledge': self.instrument.id}}
|
||||
]
|
||||
|
||||
bookmark = InstrumentBookmark.objects.create(
|
||||
instrument=self.instrument,
|
||||
uuid='6dd39d83-e442-4f3c-8d3f-e7e8026b0c4b',
|
||||
user=self.student1
|
||||
)
|
||||
content = find_content(content_list, bookmark)
|
||||
self.assertEqual("My Instrument", content)
|
||||
|
||||
def test_content_from_thinglink_block(self):
|
||||
self.instrument = InstrumentFactory(title="My Instrument", slug='instrument-slug-sdfe')
|
||||
|
||||
content_list = [{'id': 'f3f80b35-9098-4eeb-a149-3dc0318208cb',
|
||||
'type': 'thinglink_block',
|
||||
'value': {'id': '1251174983242088451'}}]
|
||||
|
||||
bookmark = InstrumentBookmark.objects.create(
|
||||
instrument=self.instrument,
|
||||
uuid='f3f80b35-9098-4eeb-a149-3dc0318208cb',
|
||||
user=self.student1
|
||||
)
|
||||
content = find_content(content_list, bookmark)
|
||||
self.assertEqual("Infografik", content)
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
import json
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from books.factories import InstrumentFactory, ContentBlockFactory, ModuleFactory
|
||||
from notes.models import InstrumentBookmark
|
||||
from notes.schema import find_content, find_content_bfs
|
||||
from users.models import User
|
||||
from users.services import create_users
|
||||
|
||||
|
||||
class TestFindContentBfs(TestCase):
|
||||
def setUp(self):
|
||||
self.data = [
|
||||
{
|
||||
"id": "45c5d9e7-233b-4ec7-a539-4136f2f969a4",
|
||||
"type": "content_list_item",
|
||||
"value": [
|
||||
{
|
||||
"id": "4eb6a9ec-576a-4bec-8eb7-798ac21e88cd",
|
||||
"type": "text_block",
|
||||
"value": {"text": "<p>Schauen Sie sich die</p>"}
|
||||
},
|
||||
{
|
||||
"id": "718bee9a-5f92-4e3d-b7f8-aa63b8f2497e",
|
||||
"type": "link_block",
|
||||
"value": {"url": "https://skilM2_A2_Studie.pdf", "text": "Studie anzeigen"}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "6a72b706-5ad7-4c8b-9fa1-e89909a2a5e6",
|
||||
"type": "content_list_item",
|
||||
"value": [
|
||||
{
|
||||
"id": "b7cd58c7-3131-4e3c-a51a-cd0026be03f8",
|
||||
"type": "assignment",
|
||||
"value": {"assignment_id": 204}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
def test_content_from_content_list_item_sub_item(self):
|
||||
result = find_content_bfs(self.data, "4eb6a9ec-576a-4bec-8eb7-798ac21e88cd")
|
||||
|
||||
self.assertEqual( {
|
||||
"id": "4eb6a9ec-576a-4bec-8eb7-798ac21e88cd",
|
||||
"type": "text_block",
|
||||
"value": {"text": "<p>Schauen Sie sich die</p>"}
|
||||
}, result)
|
||||
|
||||
def test_content_from_content_list_item(self):
|
||||
result = find_content_bfs(self.data, "45c5d9e7-233b-4ec7-a539-4136f2f969a4")
|
||||
self.assertEqual( {
|
||||
"id": "45c5d9e7-233b-4ec7-a539-4136f2f969a4",
|
||||
"type": "content_list_item",
|
||||
"value": [
|
||||
{
|
||||
"id": "4eb6a9ec-576a-4bec-8eb7-798ac21e88cd",
|
||||
"type": "text_block",
|
||||
"value": {"text": "<p>Schauen Sie sich die</p>"}
|
||||
},
|
||||
{
|
||||
"id": "718bee9a-5f92-4e3d-b7f8-aa63b8f2497e",
|
||||
"type": "link_block",
|
||||
"value": {"url": "https://skilM2_A2_Studie.pdf", "text": "Studie anzeigen"}
|
||||
}
|
||||
]
|
||||
}, result)
|
||||
Loading…
Reference in New Issue