MCPcopy
hub / github.com/WebODM/WebODM / plugin_upload

Method plugin_upload

app/admin.py:297–350  ·  view source on GitHub ↗
(self, request, *args, **kwargs)

Source from the content-addressed store, hash-verified

295 return HttpResponseRedirect(reverse('admin:app_plugin_changelist'))
296
297 def plugin_upload(self, request, *args, **kwargs):
298 file = request.FILES.get('file')
299 if file is not None:
300 # Save to tmp dir
301 tmp_zip_path = tempfile.mktemp('plugin.zip', dir=settings.MEDIA_TMP)
302 tmp_extract_path = tempfile.mkdtemp('plugin', dir=settings.MEDIA_TMP)
303
304 try:
305 with open(tmp_zip_path, 'wb+') as fd:
306 if isinstance(file, InMemoryUploadedFile):
307 for chunk in file.chunks():
308 fd.write(chunk)
309 else:
310 with open(file.temporary_file_path(), 'rb') as f:
311 shutil.copyfileobj(f, fd)
312
313 # Extract
314 with zipfile.ZipFile(tmp_zip_path, "r") as zip_h:
315 zip_h.extractall(tmp_extract_path)
316
317 # Validate
318 folders = os.listdir(tmp_extract_path)
319 if len(folders) != 1:
320 raise ValueError("The plugin has more than 1 root directory (it should have only one)")
321
322 plugin_name = folders[0]
323 plugin_path = os.path.join(tmp_extract_path, plugin_name)
324 if not valid_plugin(plugin_path):
325 raise ValueError(
326 "This doesn't look like a plugin. Are plugin.py and manifest.json in the proper place?")
327
328 if os.path.exists(get_plugins_persistent_path(plugin_name)):
329 raise ValueError(
330 "A plugin with the name {} already exist. Please remove it before uploading one with the same name.".format(
331 plugin_name))
332
333 # Move
334 shutil.move(plugin_path, get_plugins_persistent_path())
335
336 # Initialize
337 clear_plugins_cache()
338 init_plugins()
339
340 messages.info(request, _("Plugin added successfully"))
341 except Exception as e:
342 messages.warning(request, _("Cannot load plugin: %(message)s") % {'message': str(e)})
343 if os.path.exists(tmp_zip_path):
344 os.remove(tmp_zip_path)
345 if os.path.exists(tmp_extract_path):
346 shutil.rmtree(tmp_extract_path)
347 else:
348 messages.error(request, _("You need to upload a zip file"))
349
350 return HttpResponseRedirect(reverse('admin:app_plugin_changelist'))
351
352 def plugin_actions(self, obj):
353 plugin = get_plugin_by_name(obj.name, only_active=False)

Callers

nothing calls this directly

Calls 11

valid_pluginFunction · 0.90
clear_plugins_cacheFunction · 0.90
init_pluginsFunction · 0.90
writeMethod · 0.80
temporary_file_pathMethod · 0.80
existsMethod · 0.80
_Function · 0.50
getMethod · 0.45
formatMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected