From f34b82309bb85e1b09dcbfe220d7ab3fc3be3157 Mon Sep 17 00:00:00 2001 From: Marcin Armacki Date: Sun, 13 Dec 2020 14:05:35 +0100 Subject: [PATCH] Updated delete methods, file title update added --- .../prototype/filehandler/annotationModule.py | 23 +++---- .../prototype/filehandler/fileModule.py | 7 ++ backend/webapp/prototype/filehandler/views.py | 69 ++++++++++++------- backend/webapp/prototype/urls.py | 5 +- 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/backend/webapp/prototype/filehandler/annotationModule.py b/backend/webapp/prototype/filehandler/annotationModule.py index a19d50c..cbd747f 100644 --- a/backend/webapp/prototype/filehandler/annotationModule.py +++ b/backend/webapp/prototype/filehandler/annotationModule.py @@ -38,12 +38,11 @@ def updateDimensionName(id, new_name, user_id): result = {"message": "Dimension name changed successfully", "statusCode": 200} return result -def deleteDimensions(ids, user_id): +def deleteDimensions(dimension_id, user_id): user = User.objects.get(pk = user_id) - for id in ids: - dimension = Dimension.objects.get(pk = id, user_id = user) - dimension.delete() - result = {"message": "Dimensions removed successfully", "statusCode": 200} + dimension = Dimension.objects.get(pk = dimension_id, user_id = user) + dimension.delete() + result = {"message": "Dimension removed successfully", "statusCode": 200} return result def addDimensionToFile(id, file_id, user_id): @@ -67,11 +66,10 @@ def getDimensionsFromFile(file_id, user_id): result = {"data": dimensions, "statusCode": 200} return result -def removeDimensionsFromFile(file_id, dimension_ids, user_id): +def removeDimensionsFromFile(file_id, dimension_id, user_id): user = User.objects.get(pk = user_id) file = Document.objects.get(pk = file_id, user_id = user) - for id in dimension_ids: - dimension = DimensionToFiles.objects.get(file_id = file, dimension_id = id).delete() + dimension = DimensionToFiles.objects.get(file_id = file, dimension_id = dimension_id).delete() result = {"message": "Dimensions successfuly removed from specified file", "statusCode": 200} return result @@ -94,12 +92,11 @@ def addNewCategories(id, categories, user_id): result = {"message": "Categories added successfully", "statusCode": 200} return result -def deleteCategories(id, categories, user_id): +def deleteCategories(dimension_id, category_id, user_id): user = User.objects.get(pk = user_id) - dimension = Dimension.objects.get(pk = id, user_id = user) - for category_id in categories: - cat = Category.objects.get(pk = category_id, dimension_id = dimension) - cat.delete() + dimension = Dimension.objects.get(pk = dimension_id, user_id = user) + cat = Category.objects.get(pk = category_id, dimension_id = dimension) + cat.delete() result = {"message": "Categories have been successfuly removed", "statusCode": 200} return result diff --git a/backend/webapp/prototype/filehandler/fileModule.py b/backend/webapp/prototype/filehandler/fileModule.py index 40e5051..d6cc8c5 100644 --- a/backend/webapp/prototype/filehandler/fileModule.py +++ b/backend/webapp/prototype/filehandler/fileModule.py @@ -46,3 +46,10 @@ def listDiscussionsFromFile(id, user_id): out['discussions'] = discussions_ result = {"data": out, "statusCode": 200} return result + +def updateFileTitle(file_id, title, user_id): + file = Document.objects.get(pk = file_id, user_id = user_id) + file.title = title + file.save() + result = {"result": "Title updated successfully", "statusCode": 200} + return result diff --git a/backend/webapp/prototype/filehandler/views.py b/backend/webapp/prototype/filehandler/views.py index d36bb87..11b72aa 100644 --- a/backend/webapp/prototype/filehandler/views.py +++ b/backend/webapp/prototype/filehandler/views.py @@ -15,7 +15,7 @@ from prototype.filehandler.xmlParser import parseData from prototype.filehandler.functions import addToDatabase, listDiscussionsFromFile, listParagraphsFromDiscussion, createLabels, listPostsFromDiscussion, updateLabelsByParagraphId from prototype.filehandler.userModule import registerNewUser, loginUser, logoutUser, checkUserToken -from prototype.filehandler.fileModule import addDataToDatabase, listDiscussionsFromFile +from prototype.filehandler.fileModule import addDataToDatabase, listDiscussionsFromFile, updateFileTitle from prototype.filehandler.annotationModule import addNewDimension, getDimensionList, getCategoriesFromDimension, updateDimensionName, updateCategories, addNewCategories, deleteCategories, deleteDimensions, addDimensionToFile, getDimensionsFromFile, removeDimensionsFromFile, addLabelsToParagraphs, removeLabelsFromParagraphs from prototype.filehandler.exceptions import InvalidForm, CouldNotParse, InvalidMethod, KnownError @@ -162,6 +162,8 @@ def file(request): if data == False: raise CouldNotParse file_id = (form.save()).pk + if request.POST["title"] is not None: + result = updateFileTitle(file_id, request.POST["title"], user_id) result = addDataToDatabase(data, file_id) if result["statusCode"] != 200: raise KnownError @@ -199,6 +201,17 @@ def file(request): return JsonResponse({"files": data}, safe=False, status=200) return JsonResponse(result, status = result["statusCode"]) +@csrf_exempt +def fileTitle(request, file_id): + if request.method == 'PATCH': + result, user_id = checkUserToken(request) + if result["statusCode"] != 200: + print("niedobrze") + data = json.loads(request.body.decode("utf-8")) + if "title" in data: + result = updateFileTitle(file_id, data["title"], user_id) + return JsonResponse(result, status = result["statusCode"]) + @csrf_exempt def dimension(request): if request.method == 'GET': @@ -229,25 +242,27 @@ def dimension(request): result = updateCategories(data["id"], data["categories"], user_id) if result["statusCode"] != 200: print("niedobrze3") + if "new_categories" in data: + result = addNewCategories(data["id"], data["new_categories"], user_id) return JsonResponse(result, status = result["statusCode"]) +@csrf_exempt +def getDeleteDimension(request, id = 0): + if request.method == 'GET': + result, user_id = checkUserToken(request) + if result["statusCode"] != 200: + print("niedobrze") + if id != 0: + result = getCategoriesFromDimension(id, user_id) + return JsonResponse(result, status = result["statusCode"], safe=False, json_dumps_params={'ensure_ascii': False}) + if request.method == 'DELETE': result, user_id = checkUserToken(request) if result["statusCode"] != 200: print("niedobrze") - data = json.loads(request.body.decode("utf-8")) - if "id" in data: - result = deleteDimensions(data["id"], user_id) + if id != 0: + result = deleteDimensions(id, user_id) return JsonResponse(result, status = result["statusCode"]) - -def getDimension(request, id): - if request.method == 'GET': - result, user_id = checkUserToken(request) - if result["statusCode"] != 200: - print("niedobrze") - result = getCategoriesFromDimension(id, user_id) - return JsonResponse(result, status = result["statusCode"], safe=False, json_dumps_params={'ensure_ascii': False}) - @csrf_exempt def dimensionToFile(request): if request.method == 'POST': @@ -259,15 +274,6 @@ def dimensionToFile(request): result = addDimensionToFile(data["dimension_id"], data["file_id"], user_id) return JsonResponse(result, status = result["statusCode"]) - if request.method == 'DELETE': - result, user_id = checkUserToken(request) - if result["statusCode"] != 200: - print("niedobrze") - data = json.loads(request.body.decode("utf-8")) - if "dimension_ids" in data and "file_id" in data: - result = removeDimensionsFromFile(data["file_id"], data["dimension_ids"], user_id) - return JsonResponse(result, status = result["statusCode"]) - def dimensionsFromFile(request, id): if request.method == 'GET': result, user_id = checkUserToken(request) @@ -275,7 +281,17 @@ def dimensionsFromFile(request, id): print("niedobrze") result = getDimensionsFromFile(id, user_id) return JsonResponse(result, status = result["statusCode"]) - + +@csrf_exempt +def deleteDimensionsFromFile(request, file_id = 0, dimension_id = 0): + if request.method == 'DELETE': + result, user_id = checkUserToken(request) + if result["statusCode"] != 200: + print("niedobrze") + if file_id != 0 and dimension_id != 0: + result = removeDimensionsFromFile(file_id, dimension_id, user_id) + return JsonResponse(result, status = result["statusCode"]) + @csrf_exempt def category(request): if request.method == 'POST': @@ -286,13 +302,14 @@ def category(request): result = addNewCategories(data["id"], data["categories"], user_id) return JsonResponse(result, status = result["statusCode"]) +@csrf_exempt +def deleteCategory(request, dimension_id = 0, category_id = 0): if request.method == 'DELETE': result, user_id = checkUserToken(request) if result["statusCode"] != 200: print("niedobrze") - data = json.loads(request.body.decode("utf-8")) - if "categories" in data and "id" in data: - result = deleteCategories(data["id"], data["categories"], user_id) + if dimension_id != 0 and category_id != 0 : + result = deleteCategories(dimension_id, category_id, user_id) return JsonResponse(result, status = result["statusCode"]) @csrf_exempt diff --git a/backend/webapp/prototype/urls.py b/backend/webapp/prototype/urls.py index f48a986..1804296 100644 --- a/backend/webapp/prototype/urls.py +++ b/backend/webapp/prototype/urls.py @@ -32,11 +32,14 @@ urlpatterns = [ path('login/', views.login), path('testToken/', views.testToken), path('file/', views.file), + path('fileTitle/', views.fileTitle), path('dimension/', views.dimension), - path('dimension/', views.getDimension), + path('dimension/', views.getDeleteDimension), path('file/dimension/', views.dimensionToFile), path('file/dimension/', views.dimensionsFromFile), + path('file/dimension//', views.deleteDimensionsFromFile), path('category/', views.category), + path('category//', views.deleteCategory), path('label/', views.label) ]