Skip to content

Commit

Permalink
FWF-3749: [Feature] Sonar fix and api docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
auslin-aot committed Jan 20, 2025
1 parent 4194590 commit 725f739
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 103 deletions.
5 changes: 2 additions & 3 deletions forms-flow-api/src/formsflow_api/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from flask_sqlalchemy.query import Query
from formsflow_api_utils.utils import (
DRAFT_APPLICATION_STATUS,
FILTER_MAPS,
validate_sort_order_and_order_by,
)
Expand Down Expand Up @@ -457,7 +456,7 @@ def find_all_by_form_id_count(cls, form_id: str):
FormProcessMapper, cls.form_process_mapper_id == FormProcessMapper.id
).filter(
cls.latest_form_id == form_id,
cls.application_status != DRAFT_APPLICATION_STATUS,
cls.is_draft.is_(False),
)
return FormProcessMapper.tenant_authorization(query=query).count()

Expand Down Expand Up @@ -705,7 +704,7 @@ def get_total_application_corresponding_to_mapper_id(
FormProcessMapper.id == Application.form_process_mapper_id,
)
.filter(FormProcessMapper.id == form_process_mapper_id)
.filter(Application.application_status != DRAFT_APPLICATION_STATUS)
.filter(Application.is_draft.is_(False))
.one_or_none()
)
# returns a list of one element with count of applications
Expand Down
5 changes: 2 additions & 3 deletions forms-flow-api/src/formsflow_api/models/draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import uuid

from formsflow_api_utils.utils import (
DRAFT_APPLICATION_STATUS,
FILTER_MAPS,
validate_sort_order_and_order_by,
)
Expand Down Expand Up @@ -206,7 +205,7 @@ def get_draft_count(cls, **kwargs):
query = query.filter(Application.created_by == user_id)
query = query.filter(
and_(
Application.application_status == DRAFT_APPLICATION_STATUS,
Application.is_draft.is_(True),
Draft.status == str(DraftStatus.ACTIVE.value),
)
)
Expand Down Expand Up @@ -234,7 +233,7 @@ def get_draft_by_parent_form_id(cls, parent_form_id: str) -> Draft:
Application.form_process_mapper_id.in_(
[id for id, in get_all_mapper_id]
),
Application.application_status == DRAFT_APPLICATION_STATUS,
Application.is_draft.is_(True),
)
)
return FormProcessMapper.tenant_authorization(result).all()
109 changes: 65 additions & 44 deletions forms-flow-api/src/formsflow_api/resources/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ApplicationsResource(Resource):
"description": "Filter resources by application name.",
"type": "string",
},
"id": {
"Id": {
"in": "query",
"description": "Filter resources by id.",
"type": "int",
Expand All @@ -145,6 +145,46 @@ class ApplicationsResource(Resource):
"description": "Filter resources by modified to.",
"type": "string",
},
"createdBy": {
"in": "query",
"description": "Filter resources by created by.",
"type": "string",
},
"createdFrom": {
"in": "query",
"description": "Filter resources by created from.",
"type": "string",
},
"createdTo": {
"in": "query",
"description": "Filter resources by created to.",
"type": "string",
},
"applicationStatus": {
"in": "query",
"description": "Filter resources by application status.",
"type": "string",
},
"parentFormId": {
"in": "query",
"description": "Filter resources by parent form id.",
"type": "string",
},
"createdUserSubmissions": {
"in": "query",
"description": "Return user created submissions.",
"type": "bool",
},
"includeDrafts": {
"in": "query",
"description": "Return submissions and drafts/Specific to client permission.",
"type": "bool",
},
"onlyDrafts": {
"in": "query",
"description": "Return only drafts/Specific to client permission.",
"type": "bool",
},
}
)
@API.response(200, "OK:- Successful request.", model=application_list_model)
Expand All @@ -159,62 +199,43 @@ class ApplicationsResource(Resource):
def get(): # pylint:disable=too-many-locals
"""Get applications."""
dict_data = ApplicationListRequestSchema().load(request.args) or {}
page_no = dict_data.get("page_no")
limit = dict_data.get("limit")
order_by = dict_data.get("order_by", "id")
application_id = dict_data.get("application_id")
application_name = dict_data.get("application_name")
application_status = dict_data.get("application_status")
created_by = dict_data.get("created_by")
created_from_date = dict_data.get("created_from_date")
created_to_date = dict_data.get("created_to_date")
modified_from_date = dict_data.get("modified_from_date")
modified_to_date = dict_data.get("modified_to_date")
sort_order = dict_data.get("sort_order", "desc")
# Common parameters
common_filters = {
"page_no": dict_data.get("page_no"),
"limit": dict_data.get("limit"),
"order_by": dict_data.get("order_by", "id"),
"application_id": dict_data.get("application_id"),
"application_name": dict_data.get("application_name"),
"application_status": dict_data.get("application_status"),
"created_by": dict_data.get("created_by"),
"created_from": dict_data.get("created_from_date"),
"created_to": dict_data.get("created_to_date"),
"modified_from": dict_data.get("modified_from_date"),
"modified_to": dict_data.get("modified_to_date"),
"sort_order": dict_data.get("sort_order", "desc"),
"parent_form_id": dict_data.get("parent_form_id"),
}

# Flags
include_drafts = dict_data.get("include_drafts", False)
only_drafts = dict_data.get("only_drafts", False)
created_user_submissions = dict_data.get("created_user_submissions", False)
parent_form_id = dict_data.get("parent_form_id")
include_drafts = dict_data.get("include_drafts")
only_drafts = dict_data.get("only_drafts")

if auth.has_role([VIEW_TASKS, MANAGE_TASKS]) and not created_user_submissions:
(
application_schema_dump,
application_count,
draft_count,
) = ApplicationService.get_auth_applications_and_count(
created_from=created_from_date,
created_to=created_to_date,
modified_from=modified_from_date,
modified_to=modified_to_date,
order_by=order_by,
sort_order=sort_order,
created_by=created_by,
application_id=application_id,
application_name=application_name,
application_status=application_status,
page_no=page_no,
limit=limit,
parent_form_id=parent_form_id,
filters=common_filters
)
else:
(
application_schema_dump,
application_count,
draft_count,
) = ApplicationService.get_all_applications_by_user(
page_no=page_no,
limit=limit,
order_by=order_by,
sort_order=sort_order,
created_from=created_from_date,
created_to=created_to_date,
modified_from=modified_from_date,
modified_to=modified_to_date,
created_by=created_by,
application_id=application_id,
application_name=application_name,
application_status=application_status,
parent_form_id=parent_form_id,
filters=common_filters,
include_drafts=include_drafts,
only_drafts=only_drafts,
)
Expand All @@ -224,8 +245,8 @@ def get(): # pylint:disable=too-many-locals
"applications": application_schema_dump,
"totalCount": application_count,
"draftCount": draft_count,
"limit": limit,
"pageNo": page_no,
"limit": common_filters["limit"],
"pageNo": common_filters["page_no"],
}
),
HTTPStatus.OK,
Expand Down
80 changes: 27 additions & 53 deletions forms-flow-api/src/formsflow_api/services/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,29 @@ def _application_access(token: str) -> bool:
resource_list,
)

@staticmethod
def extract_common_filters(filters: dict) -> dict:
"""Extract common filter parameters from the filters dictionary."""
return {
"application_id": filters.get("application_id"),
"application_name": filters.get("application_name"),
"application_status": filters.get("application_status"),
"created_by": filters.get("created_by"),
"page_no": filters.get("page_no"),
"limit": filters.get("limit"),
"order_by": filters.get("order_by"),
"sort_order": filters.get("sort_order"),
"created_from": filters.get("created_from"),
"created_to": filters.get("created_to"),
"modified_from": filters.get("modified_from"),
"modified_to": filters.get("modified_to"),
"parent_form_id": filters.get("parent_form_id"),
}

@staticmethod
@user_context
def get_auth_applications_and_count( # pylint: disable=too-many-arguments,too-many-locals,too-many-positional-arguments
page_no: int,
limit: int,
order_by: str,
created_from: datetime,
created_to: datetime,
modified_from: datetime,
modified_to: datetime,
application_id: int,
application_name: str,
application_status: str,
created_by: str,
sort_order: str,
parent_form_id: str,
filters: dict,
**kwargs,
):
"""Get applications only from authorized groups."""
Expand All @@ -213,25 +220,15 @@ def get_auth_applications_and_count( # pylint: disable=too-many-arguments,too-m
)
for form in forms:
form_ids.append(form.resource_id)

common_filters = ApplicationService.extract_common_filters(filters)
(
applications,
get_all_applications_count,
) = Application.find_applications_by_auth_formids_user(
application_id=application_id,
application_name=application_name,
application_status=application_status,
created_by=created_by,
page_no=page_no,
limit=limit,
order_by=order_by,
modified_from=modified_from,
modified_to=modified_to,
sort_order=sort_order,
created_from=created_from,
created_to=created_to,
**common_filters,
form_ids=form_ids,
user_name=user_name,
parent_form_id=parent_form_id,
)
draft_count = Draft.get_draft_count()
return (
Expand Down Expand Up @@ -270,41 +267,18 @@ def get_auth_by_application_id(application_id: int, **kwargs):
@staticmethod
@user_context
def get_all_applications_by_user( # pylint: disable=too-many-arguments,too-many-locals,too-many-positional-arguments
page_no: int,
limit: int,
order_by: str,
sort_order: str,
created_from: datetime,
created_to: datetime,
modified_from: datetime,
modified_to: datetime,
created_by: str,
application_status: str,
application_name: str,
application_id: int,
parent_form_id: str,
include_drafts: str,
filters: dict,
include_drafts: bool,
only_drafts: bool,
**kwargs,
):
"""Get all applications based on user."""
user: UserContext = kwargs["user"]
user_id: str = user.user_name
common_filters = ApplicationService.extract_common_filters(filters)
applications, get_all_applications_count = Application.find_all_by_user(
user_id=user_id,
page_no=page_no,
limit=limit,
order_by=order_by,
sort_order=sort_order,
application_id=application_id,
application_name=application_name,
application_status=application_status,
created_by=created_by,
modified_from=modified_from,
modified_to=modified_to,
created_from=created_from,
created_to=created_to,
parent_form_id=parent_form_id,
**common_filters,
include_drafts=include_drafts,
only_drafts=only_drafts,
)
Expand Down

0 comments on commit 725f739

Please sign in to comment.