ytsm/YtManagerApp/views.py

72 lines
1.9 KiB
Python
Raw Normal View History

2018-10-04 11:36:11 +00:00
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, JsonResponse
from .models import SubscriptionFolder, Subscription
2018-10-05 19:53:27 +00:00
from .management import FolderManager
2018-10-04 11:36:11 +00:00
def get_children_recurse(parent_id):
children = []
for folder in SubscriptionFolder.objects.filter(parent_id=parent_id).order_by('name'):
children.append({
2018-10-05 19:53:27 +00:00
"id": "folder" + str(folder.id),
"text": folder.name,
"type": "folder",
"state": {"opened": True},
"children": get_children_recurse(folder.id)
2018-10-04 11:36:11 +00:00
})
for sub in Subscription.objects.filter(parent_folder_id=parent_id).order_by('name'):
children.append({
2018-10-05 19:53:27 +00:00
"id": "sub" + str(sub.id),
"type": "sub",
"text": sub.name
2018-10-04 11:36:11 +00:00
})
return children
2018-10-05 19:53:27 +00:00
def get_folders(parent_id, path=""):
2018-10-04 11:36:11 +00:00
folders = []
2018-10-05 19:53:27 +00:00
prefix = path + "/"
if len(path) == 0:
prefix = ""
2018-10-04 11:36:11 +00:00
for folder in SubscriptionFolder.objects.filter(parent_id=parent_id).order_by('name'):
2018-10-05 19:53:27 +00:00
folder_path = prefix + folder.name
2018-10-04 11:36:11 +00:00
folders.append({
2018-10-05 19:53:27 +00:00
"id": folder.id,
"text": folder_path
2018-10-04 11:36:11 +00:00
})
folders.extend(get_folders(folder.id, folder_path))
return folders
def ajax_get_children(request: HttpRequest):
return JsonResponse(get_children_recurse(None), safe=False)
2018-10-05 19:53:27 +00:00
2018-10-04 11:36:11 +00:00
def ajax_get_folders(request: HttpRequest):
return JsonResponse(get_folders(None), safe=False)
2018-10-05 19:53:27 +00:00
def ajax_edit_folder(request: HttpRequest):
if request.method == 'POST':
fid = request.POST['id']
name = request.POST['name']
parent_id = request.POST['parent']
FolderManager.create_or_edit(fid, name, parent_id)
return HttpResponse()
def ajax_delete_folder(request: HttpRequest, fid):
FolderManager.delete(fid)
return HttpResponse()
2018-10-04 11:36:11 +00:00
def index(request: HttpRequest):
context = {}
2018-10-05 19:53:27 +00:00
return render(request, 'YtManagerApp/index.html', context)