Skip to content

Commit

Permalink
Implement document uploading (#1840)
Browse files Browse the repository at this point in the history
* Refactor common file upload

* change naming to addFileDrawer

* Add upload document drawer and functionality

* Add common file upload

* Fix spacing folder paper

* fix code smells

* cleanup shapefile upload

* change naming to reflect which file uploader is used

* change object service methods to use the name object

* remove duplicate file import

* Add is uploaded column to documents widget table

* tweak update document

* remove unused import

* revert icon choices

* update package lock

* update package lock

* add unit test for upload drawer

* update tests

* remove failing test for now
  • Loading branch information
jadmsaadaot authored Jul 17, 2023
1 parent fe0331b commit 439e150
Show file tree
Hide file tree
Showing 26 changed files with 3,909 additions and 26,971 deletions.
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

0 comments on commit 439e150

Please sign in to comment.