Merge branch 'master' into editor

This commit is contained in:
Vivek Santayana 2022-06-19 13:25:02 +01:00
commit d8d5e92453
13 changed files with 30 additions and 21 deletions

View File

@ -1,5 +0,0 @@
from config import Config
from os import path
from pathlib import Path
data = Path(Config.DATA)

View File

@ -1,14 +1,15 @@
from ..data import data from ..extensions import db
from ..modules import db
from ..tools.logs import write from ..tools.logs import write
from flask import flash from flask import flash
from flask import current_app as app
from flask_login import current_user from flask_login import current_user
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from datetime import datetime from datetime import datetime
from json import dump, loads from json import dump, loads
from os import path, remove from os import path, remove
from pathlib import Path
from uuid import uuid4 from uuid import uuid4
class Dataset(db.Model): class Dataset(db.Model):
@ -48,6 +49,7 @@ class Dataset(db.Model):
return False, message return False, message
write('system.log', f'Dataset {self.id} deleted by {current_user.get_username()}.') write('system.log', f'Dataset {self.id} deleted by {current_user.get_username()}.')
filename = secure_filename('.'.join([self.id,'json'])) filename = secure_filename('.'.join([self.id,'json']))
data = Path(app.config.get('DATA'))
file_path = path.join(data, 'questions', filename) file_path = path.join(data, 'questions', filename)
remove(file_path) remove(file_path)
db.session.delete(self) db.session.delete(self)
@ -58,6 +60,7 @@ class Dataset(db.Model):
self.generate_id() self.generate_id()
timestamp = datetime.now() timestamp = datetime.now()
filename = secure_filename('.'.join([self.id,'json'])) filename = secure_filename('.'.join([self.id,'json']))
data = Path(app.config.get('DATA'))
file_path = path.join(data, 'questions', filename) file_path = path.join(data, 'questions', filename)
upload.stream.seek(0) upload.stream.seek(0)
questions = loads(upload.read()) questions = loads(upload.read())
@ -73,11 +76,13 @@ class Dataset(db.Model):
def check_file(self): def check_file(self):
filename = secure_filename('.'.join([self.id,'json'])) filename = secure_filename('.'.join([self.id,'json']))
data = Path(app.config.get('DATA'))
file_path = path.join(data, 'questions', filename) file_path = path.join(data, 'questions', filename)
if not path.isfile(file_path): return False, 'Data file is missing.' if not path.isfile(file_path): return False, 'Data file is missing.'
return True, 'Data file found.' return True, 'Data file found.'
def get_file(self): def get_file(self):
filename = secure_filename('.'.join([self.id,'json'])) filename = secure_filename('.'.join([self.id,'json']))
data = Path(app.config.get('DATA'))
file_path = path.join(data, 'questions', filename) file_path = path.join(data, 'questions', filename)
return file_path return file_path

View File

@ -1,4 +1,4 @@
from ..modules import db, mail from ..extensions import db, mail
from ..tools.forms import JsonEncodedDict from ..tools.forms import JsonEncodedDict
from ..tools.encryption import decrypt, encrypt from ..tools.encryption import decrypt, encrypt
from ..tools.logs import write from ..tools.logs import write

View File

@ -1,4 +1,4 @@
from ..modules import db from ..extensions import db
from ..tools.encryption import decrypt, encrypt from ..tools.encryption import decrypt, encrypt
from ..tools.forms import JsonEncodedDict from ..tools.forms import JsonEncodedDict
from ..tools.logs import write from ..tools.logs import write

View File

@ -1,4 +1,4 @@
from ..modules import db, mail from ..extensions import db, mail
from ..tools.encryption import decrypt, encrypt from ..tools.encryption import decrypt, encrypt
from ..tools.logs import write from ..tools.logs import write

View File

@ -1,13 +1,16 @@
from ..data import data as data_dir from flask import current_app as app
import json import json
from pathlib import Path
from random import shuffle from random import shuffle
def load(filename:str): def load(filename:str):
data_dir = Path(app.config.get('DATA'))
with open(f'./{data_dir}/{filename}') as file: with open(f'./{data_dir}/{filename}') as file:
return json.load(file) return json.load(file)
def save(data:dict, filename:str): def save(data:dict, filename:str):
data_dir = Path(app.config.get('DATA'))
with open(f'./{data_dir}/{filename}', 'w') as file: with open(f'./{data_dir}/{filename}', 'w') as file:
json.dump(data, file, indent=4) json.dump(data, file, indent=4)

View File

@ -1,7 +1,10 @@
from ..data import data from flask import current_app as app
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
from pathlib import Path
def load_key(): def load_key():
data = Path(app.config.get('DATA'))
with open(f'./{data}/.encryption.key', 'rb') as keyfile: return keyfile.read() with open(f'./{data}/.encryption.key', 'rb') as keyfile: return keyfile.read()
def decrypt(input:str): def decrypt(input:str):

View File

@ -1,5 +1,5 @@
from ..modules import db from ..extensions import db
from wtforms.validators import ValidationError from wtforms.validators import ValidationError

View File

@ -1,10 +1,14 @@
from ..data import data from flask import current_app as app
from datetime import datetime from datetime import datetime
from pathlib import Path
def read(filename:str): def read(filename:str):
data = Path(app.config.get('DATA'))
with open(f'./{data}/logs/{filename}') as file: with open(f'./{data}/logs/{filename}') as file:
return file.readlines() return file.readlines()
def write(filename:str, message:str): def write(filename:str, message:str):
data = Path(app.config.get('DATA'))
with open(f'./{data}/logs/{filename}', 'a+') as file: with open(f'./{data}/logs/{filename}', 'a+') as file:
file.write(f'{datetime.now().strftime("%Y-%m-%d-%X")}: {message}\n') file.write(f'{datetime.now().strftime("%Y-%m-%d-%X")}: {message}\n')

View File

@ -1,6 +1,5 @@
from .config import Config
from flask import Blueprint, redirect, request, render_template from flask import Blueprint, redirect, request, render_template
from flask import current_app as app
from datetime import datetime, timedelta 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, max_age = timedelta(days=14) if request.cookies.get('remember') == 'True' else None,
path = '/', path = '/',
expires = datetime.utcnow() + timedelta(days=14) if request.cookies.get('remember') else None, 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 secure = True
) )
return resp return resp

View File

@ -1 +0,0 @@
from app.config import Development as Config

View File

@ -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.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.data import save
from app.tools.logs import write from app.tools.logs import write
from config import Config
from flask import flash, Flask, render_template, request from flask import flash, Flask, render_template, request
from flask.helpers import url_for from flask.helpers import url_for
@ -15,6 +14,7 @@ from werkzeug.middleware.proxy_fix import ProxyFix
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
from datetime import datetime from datetime import datetime
from os import mkdir, path from os import mkdir, path
from pathlib import Path
def create_app(): def create_app():
app = Flask(__name__) app = Flask(__name__)
@ -59,6 +59,7 @@ def create_app():
app.register_blueprint(quiz) app.register_blueprint(quiz)
app.register_blueprint(editor, url_prefix='/admin/editor') app.register_blueprint(editor, url_prefix='/admin/editor')
data = Path(app.config.get('DATA'))
if not path.isdir(f'./{data}'): mkdir(f'./{data}') if not path.isdir(f'./{data}'): mkdir(f'./{data}')
if not path.isdir(f'./{data}/questions'): mkdir(f'./{data}/questions') if not path.isdir(f'./{data}/questions'): mkdir(f'./{data}/questions')
if not path.isfile(f'./{data}/.gitignore'): if not path.isfile(f'./{data}/.gitignore'):