Add toml file to saved files on upload
This commit is contained in:
parent
45a819788b
commit
b070f35d5e
@ -17,5 +17,5 @@ from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('download/<str:req_mod>/<str:req_ver>/', views.ModuleDownloadView.as_view(), name="api_dl")
|
||||
path('download/<str:req_mod>/<str:req_ver>/', views.ModuleDownloadView.as_view(), name="api_zip_dl")
|
||||
]
|
||||
|
@ -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)
|
||||
|
19
PDMI/store/migrations/0010_version_toml.py
Normal file
19
PDMI/store/migrations/0010_version_toml.py
Normal file
@ -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),
|
||||
),
|
||||
]
|
@ -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
|
||||
|
@ -40,7 +40,7 @@
|
||||
</div>
|
||||
<div class="col bg-grey">
|
||||
<p>
|
||||
<a href="{% url 'api_dl' req_mod=module.name req_ver=view_version.ver %}" class="btn btn-success">
|
||||
<a href="{% url 'api_zip_dl' req_mod=module.name req_ver=view_version.ver %}" class="btn btn-success">
|
||||
Download <span class="fas fa-download"></span>
|
||||
</a>
|
||||
</p>
|
||||
|
@ -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():
|
||||
|
Loading…
Reference in New Issue
Block a user