Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix library upload not working due to unsupported content-type in DRF #130

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions backend/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
from .serializers import LibrarySerializer, LibraryUploadSerializer
from .utils import get_available_libraries, get_library, import_library_view


class LibraryViewSet(BaseModelViewSet):
serializer_class = LibrarySerializer
parser_classes = [FileUploadParser]

# solve issue with URN containing dot, see https://stackoverflow.com/questions/27963899/django-rest-framework-using-dot-in-url
lookup_value_regex = r"[\w.:-]+"
Expand Down Expand Up @@ -127,15 +127,11 @@ def import_library(self, request, pk=None):
status=HTTP_422_UNPROCESSABLE_ENTITY,
)


class UploadLibraryView(APIView):
parser_classes = (FileUploadParser,)
serializer_class = LibraryUploadSerializer

def post(self, request):
@action(detail=False, methods=["post"], url_path="upload")
def upload_library(self, request):
if not request.data:
return HttpResponse(
json.dumps({"error": "No file detected !"}), status=HTTP_400_BAD_REQUEST
json.dumps({"error": "noFileDetected"}), status=HTTP_400_BAD_REQUEST
)

try:
Expand Down
3 changes: 2 additions & 1 deletion frontend/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,6 @@
"notApplicable": "Not applicable",
"administrator": "Administrator",
"domainManager": "Domain manager",
"analyst": "Analyst"
"analyst": "Analyst",
"noFileDetected": "Error: no file detected"
}
3 changes: 2 additions & 1 deletion frontend/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,6 @@
"notApplicable": "Non applicable",
"administrator": "Administrateur",
"domainManager": "Gestionnaire de domaine",
"analyst": "Analyste"
"analyst": "Analyste",
"noFileDetected": "Erreur: aucun fichier détecté"
}
1 change: 1 addition & 0 deletions frontend/src/lib/utils/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ export function localItems(languageTag: string): LocalItems {
analyst: m.analyst({ languageTag: languageTag }),
auditor: m.auditor({ languageTag: languageTag }),
domainManager: m.domainManager({ languageTag: languageTag }),
noFileDetected: m.noFileDetected({ languageTag: languageTag }),

};
return LOCAL_ITEMS;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const RiskMatrixSchema = baseNamedObject({
});

export const LibraryUploadSchema = z.object({
file: z.string()
file: z.string().optional()
});

export const RiskAssessmentSchema = baseNamedObject({
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/routes/(app)/libraries/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { z } from 'zod';
import { tableSourceMapper } from '@skeletonlabs/skeleton';
import { listViewFields } from '$lib/utils/table';
import type { Library, urlModel } from '$lib/utils/types';
import { localItems } from '$lib/utils/locales';
import { languageTag } from '$paraglide/runtime';

export const load = (async ({ fetch }) => {
const endpoint = `${BASE_API_URL}/libraries/`;
Expand Down Expand Up @@ -94,7 +96,7 @@ export const actions: Actions = {
if (!req.ok) {
const response = await req.json();
console.error(response);
setFlash({ type: 'error', message: `Error: ${response.error}` }, event);
setFlash({ type: 'error', message: localItems(languageTag())[response.error] }, event);
return fail(400, { form });
}
setFlash({ type: 'success', message: 'Library successfully imported !' }, event);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes/(app)/libraries/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<button
class="btn variant-filled-primary font-semibold w-full"
data-testid="save-button"
type="submit">{m.save()}</button
type="submit">{m.upload()}</button
>
</SuperForm>
{:catch err}
Expand Down
Loading