models.py: logic error

This commit is contained in:
dogeystamp 2023-05-08 18:54:40 -04:00
parent bb6e222255
commit e2ec325540
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
5 changed files with 56 additions and 62 deletions

View File

@ -11,50 +11,62 @@ import sqlalchemy_utils
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = '70ab3c81827a' revision = "70ab3c81827a"
down_revision = '4cd7cdbc2d1f' down_revision = "4cd7cdbc2d1f"
branch_labels = None branch_labels = None
depends_on = None depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.create_table('uploads', op.create_table(
sa.Column('upload_id', sa.String(), nullable=False), "uploads",
sa.Column('share_id', sqlalchemy_utils.types.uuid.UUIDType(), nullable=True), sa.Column("upload_id", sa.String(), nullable=False),
sa.Column('create_date', sa.DateTime(), nullable=False), sa.Column("share_id", sqlalchemy_utils.types.uuid.UUIDType(), nullable=True),
sa.Column('total_chunks', sa.Integer(), nullable=False), sa.Column("create_date", sa.DateTime(), nullable=False),
sa.Column('recv_chunks', sa.Integer(), nullable=False), sa.Column("total_chunks", sa.Integer(), nullable=False),
sa.Column('completed', sa.Boolean(), nullable=False), sa.Column("recv_chunks", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['share_id'], ['shares.share_id'], ), sa.Column("completed", sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('upload_id') sa.ForeignKeyConstraint(
["share_id"],
["shares.share_id"],
),
sa.PrimaryKeyConstraint("upload_id"),
) )
op.create_table('chunks', op.create_table(
sa.Column('chunk_id', sa.Integer(), autoincrement=True, nullable=False), "chunks",
sa.Column('create_date', sa.DateTime(), nullable=False), sa.Column("chunk_id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column('index', sa.Integer(), nullable=False), sa.Column("create_date", sa.DateTime(), nullable=False),
sa.Column('upload_id', sa.String(), nullable=True), sa.Column("index", sa.Integer(), nullable=False),
sa.Column('filename', sa.String(), nullable=False), sa.Column("upload_id", sa.String(), nullable=True),
sa.ForeignKeyConstraint(['upload_id'], ['uploads.upload_id'], ), sa.Column("filename", sa.String(), nullable=False),
sa.PrimaryKeyConstraint('chunk_id') sa.ForeignKeyConstraint(
["upload_id"],
["uploads.upload_id"],
),
sa.PrimaryKeyConstraint("chunk_id"),
) )
with op.batch_alter_table('shares', schema=None) as batch_op: with op.batch_alter_table("shares", schema=None) as batch_op:
batch_op.alter_column('share_id', batch_op.alter_column(
existing_type=sa.NUMERIC(precision=16), "share_id",
type_=sqlalchemy_utils.types.uuid.UUIDType(), existing_type=sa.NUMERIC(precision=16),
existing_nullable=False) type_=sqlalchemy_utils.types.uuid.UUIDType(),
existing_nullable=False,
)
# ### end Alembic commands ### # ### end Alembic commands ###
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('shares', schema=None) as batch_op: with op.batch_alter_table("shares", schema=None) as batch_op:
batch_op.alter_column('share_id', batch_op.alter_column(
existing_type=sqlalchemy_utils.types.uuid.UUIDType(), "share_id",
type_=sa.NUMERIC(precision=16), existing_type=sqlalchemy_utils.types.uuid.UUIDType(),
existing_nullable=False) type_=sa.NUMERIC(precision=16),
existing_nullable=False,
)
op.drop_table('chunks') op.drop_table("chunks")
op.drop_table('uploads') op.drop_table("uploads")
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@ -90,19 +90,13 @@ class FileContentAPI(MethodView):
except KeyError as err: except KeyError as err:
return ( return (
jsonify( jsonify(
dict( dict(status="fail", message=f"Missing data for chunking; {err}")
status="fail", message=f"Missing data for chunking; {err}"
)
), ),
400, 400,
) )
except ValueError as err: except ValueError as err:
return ( return (
jsonify( jsonify(dict(status="fail", message=f"{err}")),
dict(
status="fail", message=f"{err}"
)
),
400, 400,
) )

View File

@ -333,8 +333,10 @@ class Upload(db.Model):
completed = db.Column(db.Boolean, nullable=False, default=False) completed = db.Column(db.Boolean, nullable=False, default=False)
chunks = db.relationship( chunks = db.relationship(
"Chunk", backref=db.backref("upload"), order_by="Chunk.chunk_id", "Chunk",
cascade="all, delete" backref=db.backref("upload"),
order_by="Chunk.chunk_id",
cascade="all, delete",
) )
def __init__(self, upload_id, total_chunks, share_id): def __init__(self, upload_id, total_chunks, share_id):
@ -406,10 +408,11 @@ class Chunk(db.Model):
self.upload = Upload.query.filter_by(upload_id=upload_id).first() self.upload = Upload.query.filter_by(upload_id=upload_id).first()
if self.upload is None: if self.upload is None:
self.upload = Upload(upload_id, total_chunks, share.share_id) self.upload = Upload(upload_id, total_chunks, share.share_id)
self.upload_id = upload_id
self.upload.recv_chunks = 0 self.upload.recv_chunks = 0
db.session.add(self.upload) db.session.add(self.upload)
self.upload_id = upload_id
self.create_date = datetime.datetime.now() self.create_date = datetime.datetime.now()
self.index = index self.index = index
self.filename = f"{share.share_id}_{self.upload_id}_{self.index}" self.filename = f"{share.share_id}_{self.upload_id}_{self.index}"

View File

@ -224,6 +224,7 @@ def upload(client):
method : function method : function
Method like client.post or client.put to use. Method like client.post or client.put to use.
""" """
def upload(url, data, headers={}, chunk_size=int(2e6), method=client.post): def upload(url, data, headers={}, chunk_size=int(2e6), method=client.post):
data_size = len(data.getbuffer()) data_size = len(data.getbuffer())
@ -257,6 +258,4 @@ def upload(client):
return resp return resp
return upload return upload

View File

@ -150,10 +150,7 @@ def test_files_invalid(client, auth, rand, upload):
data = resp.get_json() data = resp.get_json()
url = data.get("url") url = data.get("url")
upload_data = rand.randbytes(4000) upload_data = rand.randbytes(4000)
resp = upload( resp = upload(url + "/content", BytesIO(upload_data))
url + "/content",
BytesIO(upload_data)
)
assert resp.status_code == 201 assert resp.status_code == 201
# disable all permissions # disable all permissions
@ -165,26 +162,15 @@ def test_files_invalid(client, auth, rand, upload):
assert resp.status_code == 200 assert resp.status_code == 200
# test initializing a share without perms # test initializing a share without perms
resp = upload( resp = upload(url + "/content", BytesIO(upload_data))
url + "/content",
BytesIO(upload_data)
)
assert resp.status_code == 401 assert resp.status_code == 401
# test reading a share without perms # test reading a share without perms
resp = client.get(url + "/content") resp = client.get(url + "/content")
# test modifying an uninitialized share without perms # test modifying an uninitialized share without perms
resp = upload( resp = upload(uninit_url + "/content", BytesIO(upload_data), method=client.put)
uninit_url + "/content",
BytesIO(upload_data),
method=client.put
)
assert resp.status_code == 401 assert resp.status_code == 401
# test modifying a share without perms # test modifying a share without perms
resp = upload( resp = upload(url + "/content", BytesIO(upload_data), method=client.put)
url + "/content",
BytesIO(upload_data),
method=client.put
)
assert resp.status_code == 401 assert resp.status_code == 401
# test deleting a share without perms # test deleting a share without perms