Start api app + Add api dl view + Download in store module detail

This commit is contained in:
HugoNeveux 2020-04-28 17:01:17 +02:00
parent 8eedad2a02
commit 45a819788b
13 changed files with 57 additions and 37 deletions

View File

@ -41,6 +41,7 @@ INSTALLED_APPS = [
'store',
'doc',
'front',
'api',
'bootstrap_modal_forms',
'widget_tweaks',
'markdownx',

View File

@ -15,7 +15,7 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path, include
import store, front, doc
import store, front, doc, api
urlpatterns = [
path('admin/', admin.site.urls),
@ -24,4 +24,5 @@ urlpatterns = [
path('store/', include('store.urls'), name='store'),
path('doc/', include('doc.urls'), name='doc'),
path('markdownx/', include('markdownx.urls')),
path('api/', include('api.urls')),
]

0
PDMI/api/__init__.py Normal file
View File

3
PDMI/api/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
PDMI/api/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'

View File

3
PDMI/api/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
PDMI/api/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

21
PDMI/api/urls.py Normal file
View File

@ -0,0 +1,21 @@
"""PDMI URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path
from . import views
urlpatterns = [
path('download/<str:req_mod>/<str:req_ver>/', views.ModuleDownloadView.as_view(), name="api_dl")
]

16
PDMI/api/views.py Normal file
View File

@ -0,0 +1,16 @@
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, JsonResponse
from store.models import Module, Version
from django.shortcuts import get_object_or_404
from io import BytesIO
import zipfile, os
class ModuleDownloadView(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)
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'
return response

View File

@ -1,32 +0,0 @@
# encoding: utf-8
from django.http import HttpResponse
import json
MIMEANY = '*/*'
MIMEJSON = 'application/json'
MIMETEXT = 'text/plain'
def response_mimetype(request):
"""response_mimetype -- Return a proper response mimetype, accordingly to
what the client accepts, as available in the `HTTP_ACCEPT` header.
request -- a HttpRequest instance.
"""
can_json = MIMEJSON in request.META['HTTP_ACCEPT']
can_json |= MIMEANY in request.META['HTTP_ACCEPT']
return MIMEJSON if can_json else MIMETEXT
class JsonResponse(HttpResponse):
"""JSONResponse -- Extends HTTPResponse to handle JSON format response.
This response can be used in any view that should return a json stream of
data.
Usage:
def a_iew(request):
content = {'key': 'value'}
return JSONResponse(content, mimetype=response_mimetype(request))
"""
def __init__(self, obj='', json_opts=None, mimetype=MIMEJSON, *args, **kwargs):
json_opts = json_opts if isinstance(json_opts, dict) else {}
content = json.dumps(obj, **json_opts)
super(JsonResponse, self).__init__(content, mimetype, *args, **kwargs)

View File

@ -40,9 +40,9 @@
</div>
<div class="col bg-grey">
<p>
<button type="button" name="button" class="btn btn-success">
<a href="{% url 'api_dl' req_mod=module.name req_ver=view_version.ver %}" class="btn btn-success">
Download <span class="fas fa-download"></span>
</button>
</a>
</p>
<p>
Module by {{ module.creator }} <span class="fas fa-user border"></span>

View File

@ -9,11 +9,10 @@ from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views import View
from .forms import FileFieldForm
from .response import response_mimetype, JsonResponse
from .models import Module, Version, Dependency
from PDMI import settings
from packaging.specifiers import SpecifierSet
from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse
from django.utils import timezone
from markdownx.utils import markdownify
import os