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

View File

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

View File

@ -333,8 +333,10 @@ class Upload(db.Model):
completed = db.Column(db.Boolean, nullable=False, default=False)
chunks = db.relationship(
"Chunk", backref=db.backref("upload"), order_by="Chunk.chunk_id",
cascade="all, delete"
"Chunk",
backref=db.backref("upload"),
order_by="Chunk.chunk_id",
cascade="all, delete",
)
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()
if self.upload is None:
self.upload = Upload(upload_id, total_chunks, share.share_id)
self.upload_id = upload_id
self.upload.recv_chunks = 0
db.session.add(self.upload)
self.upload_id = upload_id
self.create_date = datetime.datetime.now()
self.index = index
self.filename = f"{share.share_id}_{self.upload_id}_{self.index}"

View File

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

View File

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