From 9a02048199dad314b7f8388d1b32b135d583fea2 Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Sun, 5 Mar 2023 00:26:39 +0000 Subject: [PATCH] Added get_file method to datasets --- ref-test/app/admin/views.py | 5 +---- ref-test/app/models/dataset.py | 8 +++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ref-test/app/admin/views.py b/ref-test/app/admin/views.py index 45aeb1b..a901d82 100644 --- a/ref-test/app/admin/views.py +++ b/ref-test/app/admin/views.py @@ -435,10 +435,7 @@ def _view_entry(id:str=None): flash('Invalid entry ID.', 'error') return redirect(url_for('admin._view_entries')) test = entry.test - dataset = test.dataset - dataset_path = dataset.get_file() - with open(dataset_path, 'r') as _dataset: - data = loads(_dataset.read()) + data = test.dataset.get_data() correct = get_correct_answers(dataset=data) answers = answer_options(dataset=data) return render_template('/admin/result-detail.html', entry = entry, correct = correct, answers = answers) diff --git a/ref-test/app/models/dataset.py b/ref-test/app/models/dataset.py index 88dc05d..0455301 100644 --- a/ref-test/app/models/dataset.py +++ b/ref-test/app/models/dataset.py @@ -8,7 +8,7 @@ from flask_login import current_user from werkzeug.utils import secure_filename from datetime import datetime -from json import dump +from json import dump, loads from os import path, remove from pathlib import Path from uuid import uuid4 @@ -116,6 +116,12 @@ class Dataset(db.Model): file_path = path.join(data, 'questions', filename) return file_path + def get_data(self): + dataset_path = self.get_file() + with open(dataset_path, 'r') as _dataset: + data = loads(_dataset.read()) + return data + def update(self, data:list=None, default:bool=False): self.date = datetime.now() if default: self.make_default()