From ac02f4dee134d17ace0b181c0dc0297d9dfb4920 Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Sun, 19 Jun 2022 13:22:05 +0100 Subject: [PATCH 1/2] Changed structure of referencing data --- ref-test/app/data.py | 5 ----- ref-test/app/models/dataset.py | 9 +++++++-- ref-test/app/tools/data.py | 5 ++++- ref-test/app/tools/encryption.py | 5 ++++- ref-test/app/tools/logs.py | 6 +++++- ref-test/app/views.py | 5 ++--- ref-test/config.py | 1 - ref-test/main.py | 5 +++-- 8 files changed, 25 insertions(+), 16 deletions(-) delete mode 100644 ref-test/app/data.py delete mode 100644 ref-test/config.py diff --git a/ref-test/app/data.py b/ref-test/app/data.py deleted file mode 100644 index 0fb38c4..0000000 --- a/ref-test/app/data.py +++ /dev/null @@ -1,5 +0,0 @@ -from config import Config -from os import path -from pathlib import Path - -data = Path(Config.DATA) diff --git a/ref-test/app/models/dataset.py b/ref-test/app/models/dataset.py index 0d1ccb7..d021e0f 100644 --- a/ref-test/app/models/dataset.py +++ b/ref-test/app/models/dataset.py @@ -1,14 +1,15 @@ -from ..data import data -from ..modules import db +from ..extensions import db from ..tools.logs import write from flask import flash +from flask import current_app as app from flask_login import current_user from werkzeug.utils import secure_filename from datetime import datetime from json import dump, loads from os import path, remove +from pathlib import Path from uuid import uuid4 class Dataset(db.Model): @@ -48,6 +49,7 @@ class Dataset(db.Model): return False, message write('system.log', f'Dataset {self.id} deleted by {current_user.get_username()}.') filename = secure_filename('.'.join([self.id,'json'])) + data = Path(app.config.get('DATA')) file_path = path.join(data, 'questions', filename) remove(file_path) db.session.delete(self) @@ -58,6 +60,7 @@ class Dataset(db.Model): self.generate_id() timestamp = datetime.now() filename = secure_filename('.'.join([self.id,'json'])) + data = Path(app.config.get('DATA')) file_path = path.join(data, 'questions', filename) upload.stream.seek(0) questions = loads(upload.read()) @@ -73,11 +76,13 @@ class Dataset(db.Model): def check_file(self): filename = secure_filename('.'.join([self.id,'json'])) + data = Path(app.config.get('DATA')) file_path = path.join(data, 'questions', filename) if not path.isfile(file_path): return False, 'Data file is missing.' return True, 'Data file found.' def get_file(self): filename = secure_filename('.'.join([self.id,'json'])) + data = Path(app.config.get('DATA')) file_path = path.join(data, 'questions', filename) return file_path \ No newline at end of file diff --git a/ref-test/app/tools/data.py b/ref-test/app/tools/data.py index 8702741..778c05a 100644 --- a/ref-test/app/tools/data.py +++ b/ref-test/app/tools/data.py @@ -1,13 +1,16 @@ -from ..data import data as data_dir +from flask import current_app as app import json +from pathlib import Path from random import shuffle def load(filename:str): + data_dir = Path(app.config.get('DATA')) with open(f'./{data_dir}/{filename}') as file: return json.load(file) def save(data:dict, filename:str): + data_dir = Path(app.config.get('DATA')) with open(f'./{data_dir}/{filename}', 'w') as file: json.dump(data, file, indent=4) diff --git a/ref-test/app/tools/encryption.py b/ref-test/app/tools/encryption.py index 1bc3362..7631bb8 100644 --- a/ref-test/app/tools/encryption.py +++ b/ref-test/app/tools/encryption.py @@ -1,7 +1,10 @@ -from ..data import data +from flask import current_app as app + from cryptography.fernet import Fernet +from pathlib import Path def load_key(): + data = Path(app.config.get('DATA')) with open(f'./{data}/.encryption.key', 'rb') as keyfile: return keyfile.read() def decrypt(input:str): diff --git a/ref-test/app/tools/logs.py b/ref-test/app/tools/logs.py index 16e7888..6be840e 100644 --- a/ref-test/app/tools/logs.py +++ b/ref-test/app/tools/logs.py @@ -1,10 +1,14 @@ -from ..data import data +from flask import current_app as app + from datetime import datetime +from pathlib import Path def read(filename:str): + data = Path(app.config.get('DATA')) with open(f'./{data}/logs/{filename}') as file: return file.readlines() def write(filename:str, message:str): + data = Path(app.config.get('DATA')) with open(f'./{data}/logs/{filename}', 'a+') as file: file.write(f'{datetime.now().strftime("%Y-%m-%d-%X")}: {message}\n') \ No newline at end of file diff --git a/ref-test/app/views.py b/ref-test/app/views.py index e3b425d..4627ebe 100644 --- a/ref-test/app/views.py +++ b/ref-test/app/views.py @@ -1,6 +1,5 @@ -from .config import Config - from flask import Blueprint, redirect, request, render_template +from flask import current_app as app from datetime import datetime, timedelta @@ -24,7 +23,7 @@ def _cookie_consent(): max_age = timedelta(days=14) if request.cookies.get('remember') == 'True' else None, path = '/', expires = datetime.utcnow() + timedelta(days=14) if request.cookies.get('remember') else None, - domain = f'{Config.SERVER_NAME}', + domain = f'{app.config.get("SERVER_NAME")}', secure = True ) return resp \ No newline at end of file diff --git a/ref-test/config.py b/ref-test/config.py deleted file mode 100644 index c21f59c..0000000 --- a/ref-test/config.py +++ /dev/null @@ -1 +0,0 @@ -from app.config import Production as Config \ No newline at end of file diff --git a/ref-test/main.py b/ref-test/main.py index 97a4d09..08ab07f 100644 --- a/ref-test/main.py +++ b/ref-test/main.py @@ -1,9 +1,8 @@ -from app.data import data +from app.config import Development as Config from app.models import Entry, Dataset, Test, User from app.modules import bootstrap, csrf, db, login_manager, mail from app.tools.data import save from app.tools.logs import write -from config import Config from flask import flash, Flask, render_template, request from flask.helpers import url_for @@ -15,6 +14,7 @@ from werkzeug.middleware.proxy_fix import ProxyFix from cryptography.fernet import Fernet from datetime import datetime from os import mkdir, path +from pathlib import Path def create_app(): app = Flask(__name__) @@ -57,6 +57,7 @@ def create_app(): app.register_blueprint(views) app.register_blueprint(quiz) + data = Path(app.config.get('DATA')) if not path.isdir(f'./{data}'): mkdir(f'./{data}') if not path.isdir(f'./{data}/questions'): mkdir(f'./{data}/questions') if not path.isfile(f'./{data}/.gitignore'): From 12207d115929e7e945fe2fc68dd5edc20a39967e Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Sun, 19 Jun 2022 13:22:24 +0100 Subject: [PATCH 2/2] Changed modules to extensions --- ref-test/app/{modules.py => extensions.py} | 0 ref-test/app/models/entry.py | 2 +- ref-test/app/models/test.py | 2 +- ref-test/app/models/user.py | 2 +- ref-test/app/tools/forms.py | 2 +- ref-test/main.py | 2 +- 6 files changed, 5 insertions(+), 5 deletions(-) rename ref-test/app/{modules.py => extensions.py} (100%) diff --git a/ref-test/app/modules.py b/ref-test/app/extensions.py similarity index 100% rename from ref-test/app/modules.py rename to ref-test/app/extensions.py diff --git a/ref-test/app/models/entry.py b/ref-test/app/models/entry.py index c1adfb6..4e528e4 100644 --- a/ref-test/app/models/entry.py +++ b/ref-test/app/models/entry.py @@ -1,4 +1,4 @@ -from ..modules import db, mail +from ..extensions import db, mail from ..tools.forms import JsonEncodedDict from ..tools.encryption import decrypt, encrypt from ..tools.logs import write diff --git a/ref-test/app/models/test.py b/ref-test/app/models/test.py index 6e3a072..95e2e78 100644 --- a/ref-test/app/models/test.py +++ b/ref-test/app/models/test.py @@ -1,4 +1,4 @@ -from ..modules import db +from ..extensions import db from ..tools.encryption import decrypt, encrypt from ..tools.forms import JsonEncodedDict from ..tools.logs import write diff --git a/ref-test/app/models/user.py b/ref-test/app/models/user.py index a1f22c6..a1cec1d 100644 --- a/ref-test/app/models/user.py +++ b/ref-test/app/models/user.py @@ -1,4 +1,4 @@ -from ..modules import db, mail +from ..extensions import db, mail from ..tools.encryption import decrypt, encrypt from ..tools.logs import write diff --git a/ref-test/app/tools/forms.py b/ref-test/app/tools/forms.py index ab9daf4..cef280d 100644 --- a/ref-test/app/tools/forms.py +++ b/ref-test/app/tools/forms.py @@ -1,5 +1,5 @@ -from ..modules import db +from ..extensions import db from wtforms.validators import ValidationError diff --git a/ref-test/main.py b/ref-test/main.py index 08ab07f..e66e134 100644 --- a/ref-test/main.py +++ b/ref-test/main.py @@ -1,6 +1,6 @@ from app.config import Development as Config from app.models import Entry, Dataset, Test, User -from app.modules import bootstrap, csrf, db, login_manager, mail +from app.extensions import bootstrap, csrf, db, login_manager, mail from app.tools.data import save from app.tools.logs import write