youtube-channel-manager/YtManagerApp/views/old_views.py

104 lines
2.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
2018-10-13 20:01:45 +00:00
from YtManagerApp.models import SubscriptionFolder, Subscription
from YtManagerApp.management.management import FolderManager, SubscriptionManager
2018-10-05 19:53:27 +00:00
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",
2018-10-10 22:43:50 +00:00
"text": sub.name,
"icon": sub.icon_default
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()
def ajax_edit_subscription(request: HttpRequest):
if request.method == 'POST':
sid = request.POST['id']
name = request.POST['name']
url = request.POST['url']
parent_id = request.POST['parent']
SubscriptionManager.create_or_edit(sid, url, name, parent_id)
return HttpResponse()
def ajax_delete_subscription(request: HttpRequest, sid):
SubscriptionManager.delete(sid)
return HttpResponse()
2018-10-10 22:43:50 +00:00
def ajax_list_videos(request: HttpRequest):
if request.method == 'POST':
type = request.POST['type']
id = request.POST['id']
context = {}
if type == 'sub':
context['videos'] = SubscriptionManager.list_videos(int(id))
else:
context['videos'] = FolderManager.list_videos(int(id))
2018-10-13 20:01:45 +00:00
return render(request, 'YtManagerApp/index_videos.html', context)
2018-10-10 22:43:50 +00:00
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)
2018-10-10 22:43:50 +00:00