All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
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)
|