vault backup: 2025-12-26 02:09:22
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
This commit is contained in:
31
stroma/file/views/tree_api_view.py
Normal file
31
stroma/file/views/tree_api_view.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from django.http import JsonResponse
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from file.models import File
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
def get_file_tree(request):
|
||||
"""Return the hierarchical file tree for the user."""
|
||||
files = File.objects.filter(user=request.quiz_user).select_related('parent').order_by('name')
|
||||
# Create a mapping of id -> item
|
||||
item_map = {}
|
||||
for f in files:
|
||||
item_map[f.id] = {
|
||||
'id': f.id,
|
||||
'name': f.name,
|
||||
'path': f.path,
|
||||
'type': 'folder' if f.mime_type == 'application/x-folder' else 'file',
|
||||
'mime_type': f.mime_type,
|
||||
'children': [],
|
||||
'content': f.text if f.mime_type.startswith('text/') else None
|
||||
}
|
||||
|
||||
root_items = []
|
||||
for f in files:
|
||||
item = item_map[f.id]
|
||||
if f.parent_id:
|
||||
if f.parent_id in item_map:
|
||||
item_map[f.parent_id]['children'].append(item)
|
||||
else:
|
||||
root_items.append(item)
|
||||
|
||||
return JsonResponse(root_items, safe=False)
|
||||
Reference in New Issue
Block a user