Add API toml download view

This commit is contained in:
HugoNeveux 2020-04-30 19:37:23 +02:00
parent e79980554a
commit 68b344904a
3 changed files with 23 additions and 8 deletions

View File

@ -17,5 +17,6 @@ from django.urls import path
from . import views
urlpatterns = [
path('download/<str:req_mod>/<str:req_ver>/', views.ModuleDownloadView.as_view(), name="api_zip_dl")
path('download/<str:req_mod>/<str:req_ver>/', views.ModuleDownloadView.as_view(), name="api_zip_dl"),
path('toml_download/<str:req_mod>/<str:req_ver>/', views.TomlDownloadView.as_view(), name="api_toml_dl")
]

View File

@ -7,15 +7,27 @@ from io import BytesIO
import zipfile, os
class ModuleDownloadView(View):
"""
Module download view : returns file response with zip archive based on
module name & version
"""
def get(self, request, req_mod, req_ver):
module = get_object_or_404(Module, name=req_mod)
version = get_object_or_404(Version, module=module, ver=req_ver)
zf = open(version.file.path, 'rb')
response = HttpResponse(zf, content_type="application/force-download")
response['Content-Disposition'] = f'attachment; filename={module.name}-{version.ver}.zip'
with open(version.file.path, 'rb') as zf:
response = HttpResponse(zf, content_type="application/force-download")
response['Content-Disposition'] = f'attachment; filename={module.name}-{version.ver}.zip'
return response
class TomlDownloadView(View):
"""
Toml download view : returns file response with module toml file based on
module name & version
"""
def get(self, request, req_mod, req_ver):
module = get_object_or_404(module, name=req_mod)
module = get_object_or_404(Module, name=req_mod)
version = get_object_or_404(Version, module=module, ver=req_ver)
with open(version.toml.path, 'rb') as f:
response = HttpResponse(f, content_type="application/toml")
response['Content-Disposition'] = f'attachment; filename={module.name}-{version.ver}.toml'
return response

View File

@ -108,12 +108,12 @@ class UploadView(LoginRequiredMixin, FormView):
readme = f.read()
else:
readme = "No readme provided."
shutil.rmtree(extract_path) # Clean temp files
os.remove(zip_path)
for required_field in REQUIRED_FIELDS:
if not required_field in module_info.keys():
return JsonResponse({'error': f'Field {required_field}\
is missing in infos.toml.'}, status=500)
shutil.rmtree(extract_path)
os.remove(zip_path)
if Module.objects.filter(name=module_info['name'].lower(),
creator=request.user):
# If module exists
@ -125,7 +125,7 @@ class UploadView(LoginRequiredMixin, FormView):
desc=module_info['description'],
creator=request.user)
if Version.objects.filter(module=module, ver=module_info['version']).exists():
# If version already exists, edit existing version
# If version already exists, update existing version
version = Version.objects.get(
module=module, ver=module_info['version'])
version.file = file
@ -134,6 +134,7 @@ class UploadView(LoginRequiredMixin, FormView):
version.readme = readme
version.toml = toml_file
else:
# Else, create new version object with all required fields
version = Version(module=module, ver=module_info['version'],
bot_ver=module_info['bot_version'],
metamodule=module_info['metamodule'],
@ -142,6 +143,7 @@ class UploadView(LoginRequiredMixin, FormView):
version.save()
toml_file.close() # Colsing toml file
for dependency in module_info['dependencies']:
# Update dependencies model for current module
if not Dependency.objects.filter(version=version, dep_module=dependency,
dep_version=module_info['dependencies'][dependency]).exists():
dep = Dependency(version=version, dep_module=dependency,