OpenDroneMap-WebODM/app/plugins/views.py

69 wiersze
2.7 KiB
Python
Czysty Zwykły widok Historia

import os
2018-07-30 15:55:46 +00:00
from app.api.tasks import TaskNestedView as TaskView
2020-01-17 18:19:03 +00:00
from app.api.workers import CheckTask as CheckTask
from app.api.workers import GetTaskResult as GetTaskResult
from django.http import HttpResponse, Http404
2021-07-01 19:30:45 +00:00
from .functions import get_plugin_by_name, get_active_plugins
from django.conf.urls import url
from django.views.static import serve
2020-04-02 18:29:27 +00:00
from urllib.parse import urlparse
def try_resolve_url(request, url):
2020-04-02 18:29:27 +00:00
o = urlparse(request.get_full_path())
res = url.resolve(o.path)
if res:
return res
else:
return (None, None, None)
def app_view_handler(request, plugin_name=None):
plugin = get_plugin_by_name(plugin_name) # TODO: this pings the server, which might be bad for performance with very large amount of files
if plugin is None:
raise Http404("Plugin not found")
# Try mountpoints first
for mount_point in plugin.app_mount_points():
view, args, kwargs = try_resolve_url(request, url(r'^/plugins/{}/{}'.format(plugin_name, mount_point.url),
mount_point.view,
*mount_point.args,
**mount_point.kwargs))
if view:
return view(request, *args, **kwargs)
# Try public assets
2020-04-02 18:29:27 +00:00
if os.path.exists(plugin.get_path("public")) and plugin.serve_public_assets(request):
view, args, kwargs = try_resolve_url(request, url('^/plugins/{}/(.*)'.format(plugin_name),
serve,
{'document_root': plugin.get_path("public")}))
if view:
return view(request, *args, **kwargs)
raise Http404("No valid routes")
def api_view_handler(request, plugin_name=None):
plugin = get_plugin_by_name(plugin_name) # TODO: this pings the server, which might be bad for performance with very large amount of files
if plugin is None:
raise Http404("Plugin not found")
for mount_point in plugin.api_mount_points():
view, args, kwargs = try_resolve_url(request, url(r'^/api/plugins/{}/{}'.format(plugin_name, mount_point.url),
mount_point.view,
*mount_point.args,
**mount_point.kwargs))
if view:
return view(request, *args, **kwargs)
2021-07-01 19:30:45 +00:00
raise Http404("No valid routes")
def root_url_patterns():
result = []
for p in get_active_plugins():
for mount_point in p.root_mount_points():
result.append(url(mount_point.url, mount_point.view, *mount_point.args, **mount_point.kwargs))
return result