From 4c5b4ae70f2f41ef9db01d9ce039dca89dea07ca Mon Sep 17 00:00:00 2001 From: Marcin Armacki Date: Mon, 14 Dec 2020 18:24:29 +0100 Subject: [PATCH] Fixed file endpoint --- .../prototype/filehandler/fileModule.py | 45 +++++++++---------- backend/webapp/prototype/filehandler/views.py | 4 +- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/backend/webapp/prototype/filehandler/fileModule.py b/backend/webapp/prototype/filehandler/fileModule.py index d6cc8c5..ff5aaa8 100644 --- a/backend/webapp/prototype/filehandler/fileModule.py +++ b/backend/webapp/prototype/filehandler/fileModule.py @@ -4,30 +4,27 @@ from django.contrib.auth.models import User def addDataToDatabase(data, file_id): - try: - out = {} - para_id = [] - messages = [] - forum = Forum(forum_id = data['id'], name = data['name'], document_id = file_id) - forum.save() - for discussion_ in data['discussions']: - discussion = Discussion(discussion_id = discussion_['id'], title = discussion_['title'], first_post = discussion_['first_post'], forum_id = forum.pk) - discussion.save() - for post_ in discussion_['posts']: - post = Post(post_id = post_['id'], parent = post_['parent'], author = post_['author'], discussion_id = discussion.pk) - post.save() - for paragraph_ in post_['message']: - paragraph = Paragraph(message = paragraph_, label = '', post_id = post.pk) - paragraph.save() - para_id.append(paragraph.pk) - messages.append(paragraph_) - out['para_id'] = para_id - out['messages'] = messages - result = DatabaseAddSuccess - except: - result = UnknownError - finally: - return result + out = {} + para_id = [] + messages = [] + print("ok") + forum = Forum(forum_id = data['id'], name = data['name'], document_id = file_id) + forum.save() + for discussion_ in data['discussions']: + discussion = Discussion(discussion_id = discussion_['id'], title = discussion_['title'], first_post = discussion_['first_post'], forum_id = forum.pk) + discussion.save() + for post_ in discussion_['posts']: + post = Post(post_id = post_['id'], parent = post_['parent'], author = post_['author'], discussion_id = discussion.pk) + post.save() + for paragraph_ in post_['message']: + paragraph = Paragraph(message = paragraph_, label = '', post_id = post.pk) + paragraph.save() + para_id.append(paragraph.pk) + messages.append(paragraph_) + out['para_id'] = para_id + out['messages'] = messages + result = DatabaseAddSuccess + return result def listDiscussionsFromFile(id, user_id): out = {} diff --git a/backend/webapp/prototype/filehandler/views.py b/backend/webapp/prototype/filehandler/views.py index 11b72aa..96c4137 100644 --- a/backend/webapp/prototype/filehandler/views.py +++ b/backend/webapp/prototype/filehandler/views.py @@ -155,14 +155,14 @@ def file(request): if result["statusCode"] != 200: raise KnownError user = User.objects.get(pk = user_id) - form = DocumentForm({"user_id": user}, request.FILES) + form = DocumentForm({"user_id": user.pk}, request.FILES) if not form.is_valid(): raise InvalidForm data = parseData(request.FILES["file"]) if data == False: raise CouldNotParse file_id = (form.save()).pk - if request.POST["title"] is not None: + if "title" in request.POST: result = updateFileTitle(file_id, request.POST["title"], user_id) result = addDataToDatabase(data, file_id) if result["statusCode"] != 200: