From 0faa732c9a3e1ffced2b26bee682f513b0e5f0ae Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 28 Oct 2020 19:16:34 +0100 Subject: Use flask-sqlalchemy instead of using sqlalchemy directly This makes database access a bit easier and also greatly simplifies some upcoming changes. --- fbin/file_storage/base.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'fbin/file_storage') diff --git a/fbin/file_storage/base.py b/fbin/file_storage/base.py index e2ca1a6..abdf580 100644 --- a/fbin/file_storage/base.py +++ b/fbin/file_storage/base.py @@ -1,6 +1,6 @@ import datetime -from .. import db +from ..db import db, File from .exceptions import * class BaseStorage: @@ -11,19 +11,18 @@ class BaseStorage: user = file.user_id is not None size_limit = self.app.config.get('USER_FILE_SIZE_LIMIT' if user else 'ANONYMOUS_FILE_SIZE_LIMIT') if size_limit is not None and file.size > size_limit: - raise FileSizeError('The file size is too large (max {})'.format(db.File.pretty_size(size_limit))) + raise FileSizeError('The file size is too large (max {})'.format(File.pretty_size(size_limit))) def add_file(self, file_hash, filename, size, user=None, ip=None, verify=True): '''Adds the file to the database. Call from store_file after the file is successfully stored.''' - with db.session_scope() as sess: - f = db.File(file_hash, filename, size, datetime.datetime.utcnow(), user.id if user else None, ip) - # Raises on invalid files - self.verify_file(f) - sess.add(f) - sess.commit() - sess.refresh(f) + f = File(file_hash, filename, size, datetime.datetime.utcnow(), user.id if user else None, ip) + # Raises on invalid files + self.verify_file(f) + db.session.add(f) + db.session.commit() + db.session.refresh(f) return f def store_file(self, uploaded_file, file_hash, filename, user, ip): -- cgit v1.2.3