Added exception handling

This commit is contained in:
Marcin Armacki 2020-06-30 19:16:28 +02:00
parent bc098077cf
commit 6c75e6a911
2 changed files with 50 additions and 29 deletions

View File

@ -10,6 +10,7 @@ def count_punct(text):
return round(count/(len(text) - text.count(" ")), 3)*100
def createLabels(data):
try:
id_to_labels = load('prototype/filehandler/labels.pkl')
df = DataFrame(data['messages'], columns = ['body_text'])
@ -31,11 +32,16 @@ def createLabels(data):
Paragraph.objects.filter(pk = id).update(label = label)
return(True)
except:
return(False)
def updateLabelsByParagraphId(data):
try:
for paragraph_ in data:
Paragraph.objects.filter(pk = paragraph_['id']).update(label = paragraph_['label'], user_updated = True)
return(True)
except:
return(False)
def addToDatabase(data, file_id):
out = {}

View File

@ -20,10 +20,13 @@ def model_form_upload(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
try:
data = parseData(request.FILES['file'])
except:
return HttpResponse('Error: Couldn\'t parse data', status=406)
file_id = (form.save()).pk
if not (createLabels(addToDatabase(data, file_id))):
return HttpResponse('Błąd przy dodawaniu informacji do bazy danych/tworzeniu etykiet', status=406)
return HttpResponse('Error: Couln\'t get labels or contact database', status=406)
output = listDiscussionsFromFile(file_id)
return JsonResponse(output, safe=False)
else:
@ -37,22 +40,34 @@ def model_form_upload(request):
@csrf_exempt
def discussions(request, id):
if request.method == 'GET':
try:
output = listParagraphsFromDiscussion(id)
return JsonResponse(output, safe=False)
except:
return HttpResponse('Error: Couldn\'t list paragraphs', status=406)
elif request.method == 'PATCH':
try:
received_data = json.loads(request.body.decode("utf-8"))
except:
return HttpResponse('Error: Couldn\'t receive data', status=406)
if updateLabelsByParagraphId(received_data):
try:
output = listParagraphsFromDiscussion(id)
return JsonResponse(output, safe=False)
except:
return HttpResponse('Error: Couldn\'t list paragraphs', status=406)
else:
return HttpResponse('Błąd przy dodwaniu informacji do bazy danych', status=406)
return HttpResponse('Error: Couldn\'t update database', status=406)
else:
return HttpResponse('Nieobsługiwana metoda HTTP', status=406)
def visualize(request, id):
if request.method == 'GET':
try:
output = listPostsFromDiscussion(id)
return JsonResponse(output, safe=False)
except:
return HttpResponse('Error: Couldn\'t list posts', status=406)
else:
return HttpResponse('Nieobsługiwana metoda HTTP', status=406)
return HttpResponse('Error: Unacceptable HTTP method', status=406)