From b070f35d5e3bd3a1957521527a79c4b8e42b84a8 Mon Sep 17 00:00:00 2001 From: HugoNeveux Date: Thu, 30 Apr 2020 19:21:01 +0200 Subject: [PATCH] Add toml file to saved files on upload --- PDMI/api/urls.py | 2 +- PDMI/api/views.py | 5 +++++ PDMI/store/migrations/0010_version_toml.py | 19 ++++++++++++++++ PDMI/store/models.py | 9 ++++++++ PDMI/store/templates/store/module_detail.html | 2 +- PDMI/store/views.py | 22 +++++++++++++------ 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 PDMI/store/migrations/0010_version_toml.py diff --git a/PDMI/api/urls.py b/PDMI/api/urls.py index 4d10656..3f0bf7b 100644 --- a/PDMI/api/urls.py +++ b/PDMI/api/urls.py @@ -17,5 +17,5 @@ from django.urls import path from . import views urlpatterns = [ - path('download///', views.ModuleDownloadView.as_view(), name="api_dl") + path('download///', views.ModuleDownloadView.as_view(), name="api_zip_dl") ] diff --git a/PDMI/api/views.py b/PDMI/api/views.py index de59124..00f9b32 100644 --- a/PDMI/api/views.py +++ b/PDMI/api/views.py @@ -14,3 +14,8 @@ class ModuleDownloadView(View): response = HttpResponse(zf, content_type="application/force-download") response['Content-Disposition'] = f'attachment; filename={module.name}-{version.ver}.zip' return response + +class TomlDownloadView(View): + 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) diff --git a/PDMI/store/migrations/0010_version_toml.py b/PDMI/store/migrations/0010_version_toml.py new file mode 100644 index 0000000..c2c6ee7 --- /dev/null +++ b/PDMI/store/migrations/0010_version_toml.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0 on 2020-04-29 14:15 + +from django.db import migrations, models +import store.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('store', '0009_version_readme'), + ] + + operations = [ + migrations.AddField( + model_name='version', + name='toml', + field=models.FileField(null=True, upload_to=store.models.toml_upload_path), + ), + ] diff --git a/PDMI/store/models.py b/PDMI/store/models.py index e633c85..008ca2a 100644 --- a/PDMI/store/models.py +++ b/PDMI/store/models.py @@ -16,6 +16,14 @@ def upload_path(instance, filename): os.remove(os.path.join(settings.MEDIA_ROOT, path)) return path +def toml_upload_path(instance, filename): + path = os.path.join( + 'modules', instance.module.name.lower(), instance.ver, instance.module.name+'.toml') + if os.path.isfile(os.path.join(settings.MEDIA_ROOT, path)): + # Delete file if already exists + os.remove(os.path.join(settings.MEDIA_ROOT, path)) + return path + class Module(models.Model): name = models.CharField(max_length=255, unique=True) @@ -35,6 +43,7 @@ class Version(models.Model): metamodule = models.BooleanField(default=False) file = models.FileField(upload_to=upload_path) readme = MarkdownxField(default="No readme provided.") + toml = models.FileField(upload_to=toml_upload_path, null=True) def __str__(self): return self.ver diff --git a/PDMI/store/templates/store/module_detail.html b/PDMI/store/templates/store/module_detail.html index 9c44323..dd023a3 100644 --- a/PDMI/store/templates/store/module_detail.html +++ b/PDMI/store/templates/store/module_detail.html @@ -40,7 +40,7 @@

- + Download

diff --git a/PDMI/store/views.py b/PDMI/store/views.py index 66578eb..83377eb 100644 --- a/PDMI/store/views.py +++ b/PDMI/store/views.py @@ -2,18 +2,18 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import render, get_object_or_404 from django.urls import reverse_lazy from django.utils import timezone -from bootstrap_modal_forms.generic import BSModalCreateView, BSModalLoginView -from .forms import CustomUserCreationForm, CustomAuthenticationForm from django.views.generic.edit import FormView from django.views.generic.list import ListView from django.views.generic.detail import DetailView from django.views import View -from .forms import FileFieldForm +from django.http import HttpResponse, JsonResponse +from django.utils import timezone +from django.core.files import File +from bootstrap_modal_forms.generic import BSModalCreateView, BSModalLoginView +from .forms import CustomUserCreationForm, CustomAuthenticationForm, FileFieldForm from .models import Module, Version, Dependency from PDMI import settings from packaging.specifiers import SpecifierSet -from django.http import HttpResponse, JsonResponse -from django.utils import timezone from markdownx.utils import markdownify import os import zipfile @@ -23,6 +23,7 @@ import magic REQUIRED_FIELDS = ['name', 'description', 'version', 'bot_version'] + class SignUpView(BSModalCreateView): form_class = CustomUserCreationForm template_name = 'store/signup_modal.html' @@ -36,6 +37,7 @@ class CustomLoginView(BSModalLoginView): success_message = 'Success: You were successfully logged in.' extra_content = dict(success_url=reverse_lazy('index')) + class ModuleListView(ListView): model = Module paginate_by = 100 @@ -45,6 +47,7 @@ class ModuleListView(ListView): context['now'] = timezone.now() return context + class ModuleDetailView(View): def get(self, request, pk, req_ver="latest"): module = get_object_or_404(Module, id=pk) @@ -53,7 +56,8 @@ class ModuleDetailView(View): for version in versions: deps.append(Dependency.objects.filter(version=version)) if req_ver == "latest": - view_version = Version.objects.filter(module=module).order_by('-id')[0] + view_version = Version.objects.filter( + module=module).order_by('-id')[0] else: view_version = Version.objects.get(module=module, ver=req_ver) return render(request, 'store/module_detail.html', { @@ -88,9 +92,11 @@ class UploadView(LoginRequiredMixin, FormView): with zipfile.ZipFile(zip_path, 'r') as zip: # Unzip archive zip.extractall(extract_path) + with open(os.path.join(extract_path, 'infos.toml'), 'r') as f: # Reading and parsing toml file module_info = toml.loads(f.read()) + toml_file = File(open(os.path.join(extract_path, 'infos.toml'), 'r')) if os.path.isfile(os.path.join(extract_path, 'README.md')): with open(os.path.join(extract_path, 'README.md'), 'r') as f: # Reading README.md file @@ -121,13 +127,15 @@ class UploadView(LoginRequiredMixin, FormView): version.metamodule = module_info['metamodule'] version.bot_ver = module_info['bot_version'] version.readme = readme + version.toml = toml_file else: version = Version(module=module, ver=module_info['version'], bot_ver=module_info['bot_version'], metamodule=module_info['metamodule'], - file=file, readme=readme) + file=file, readme=readme, toml=toml_file) module.save() version.save() + toml_file.close() for dependency in module_info['dependencies']: if not Dependency.objects.filter(version=version, dep_module=dependency, dep_version=module_info['dependencies'][dependency]).exists():