Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement document uploading #1840

Merged
merged 20 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions met-api/migrations/versions/df842dc6d0b7_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
""" Add is_uploaded column to widget_documents table

Revision ID: df842dc6d0b7
Revises: 47fc88fe0477
Create Date: 2023-07-14 13:03:24.113767

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'df842dc6d0b7'
down_revision = '47fc88fe0477'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('widget_documents', sa.Column('is_uploaded', sa.Boolean(), nullable=False, server_default=sa.false()))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('widget_documents', 'is_uploaded')
# ### end Alembic commands ###
7 changes: 4 additions & 3 deletions met-api/src/met_api/models/widget_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WidgetDocuments(BaseModel): # pylint: disable=too-few-public-methods
# defines the sorting within the specific widget.Not the overall sorting.
sort_index = db.Column(db.Integer, nullable=True, default=1)
widget_id = db.Column(db.Integer, ForeignKey('widget.id', ondelete='CASCADE'), nullable=True)
is_uploaded = db.Column(db.Boolean, nullable=True, default=False)

@classmethod
def get_all_by_widget_id(cls, widget_id) -> List[WidgetDocuments]:
Expand All @@ -39,12 +40,12 @@ def edit_widget_document(cls, widget_id, document_id, widget_document_data: dict
"""Update document."""
widget_document_query = db.session.query(WidgetDocuments) \
.filter(WidgetDocuments.widget_id == widget_id, WidgetDocuments.id == document_id)
widget_documents: WidgetDocuments = widget_document_query.first()
if not widget_documents:
widget_document: WidgetDocuments = widget_document_query.first()
if not widget_document:
return None
widget_document_query.update(widget_document_data)
db.session.commit()
return widget_documents
return widget_document

@classmethod
def remove_widget_document(cls, widget_id, document_id) -> List[WidgetDocuments]:
Expand Down
2 changes: 1 addition & 1 deletion met-api/src/met_api/schemas/widget_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class Meta: # pylint: disable=too-few-public-methods
"""Exclude unknown fields in the deserialized output."""

model = WidgetDocumentModel
fields = ('id', 'title', 'type', 'parent_document_id', 'url', 'sort_index')
fields = ('id', 'title', 'type', 'parent_document_id', 'url', 'sort_index', 'is_uploaded')
11 changes: 9 additions & 2 deletions met-api/src/met_api/services/widget_documents_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def _fetch_props(doc):
'sort_index': doc.sort_index,
'url': doc.url,
'parent_document_id': doc.parent_document_id,
'is_uploaded': doc.is_uploaded,
}
# remove null
return dict(props.items())
Expand All @@ -71,6 +72,7 @@ def create_document(widget_id, doc_details):
def _create_document_from_dict(doc_details, parent_id, widget_id):
doc: WidgetDocumentsModel = WidgetDocumentsModel()
doc.type = doc_details.get('type')
doc.is_uploaded = doc_details.get('is_uploaded')
doc.title = doc_details.get('title')
doc.parent_document_id = parent_id
doc.url = doc_details.get('url')
Expand Down Expand Up @@ -104,11 +106,16 @@ def _validate_parent_type(parent_id):
@staticmethod
def edit_document(widget_id, document_id, data: dict):
"""Update document from a document widget."""
updated_document = WidgetDocumentsModel.edit_widget_document(widget_id, document_id, data)
if not updated_document:
document = WidgetDocumentsModel.find_by_id(document_id)
if not document:
raise BusinessException(
error='Document to update was not found.',
status_code=HTTPStatus.BAD_REQUEST)
update_data = {
**data,
'url': data.get('url', document.url) if not document.is_uploaded else document.url,
}
updated_document = WidgetDocumentsModel.edit_widget_document(widget_id, document_id, update_data)
return updated_document

@staticmethod
Expand Down
Loading
Loading