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

Dev python #251

Merged
merged 2 commits into from
Dec 23, 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
31 changes: 31 additions & 0 deletions solidui/daos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
from flask_appbuilder.models.filters import BaseFilter
from flask_appbuilder.models.sqla import Model
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_sqlalchemy import Pagination
from sqlalchemy.exc import SQLAlchemyError, StatementError
from sqlalchemy.orm import Session
from sqlalchemy import asc, desc
from solidui.daos.exceptions import (
DAOCreateFailedError,
DAODeleteFailedError,
Expand Down Expand Up @@ -53,6 +55,35 @@ def __init_subclass__(cls) -> None: # pylint: disable=arguments-differ
cls.__orig_bases__[0] # type: ignore # pylint: disable=no-member
)[0]

@classmethod
def query_paginated(
cls,
session: Session = None,
page: int = 1,
per_page: int = 10,
custom_filters=None,
sort_by=None,
sort_order='asc', # Default sort order is ascending
**filters
) -> Pagination:
session = session or db.session
query = session.query(cls.model_cls).filter_by(**filters)

# Apply custom filters
if custom_filters:
query = query.filter(custom_filters)

# Apply sorting
if sort_by:
sort_column = getattr(cls.model_cls, sort_by, None)
if sort_column:
if sort_order == 'desc':
query = query.order_by(desc(sort_column))
else:
query = query.order_by(asc(sort_column))

return query.paginate(page, per_page, False)

@classmethod
def find_by_id(
cls,
Expand Down
14 changes: 1 addition & 13 deletions solidui/views/constants.py → solidui/daos/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,4 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Status
STATUS = "status"
DATA_LIST = "data"
MSG = "msg"
TOTAL_LIST = "totalList"
CURRENT_PAGE = "currentPage"
TOTAL_PAGE = "totalPage"
TOTAL = "total"

DEFAULT_PAGE_SIZE = 10
DEFAULT_PAGE_NO = 1
# limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,4 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from sqlalchemy import Column, Integer, String, DateTime

from .base import Base

class JobElement(Base):

__tablename__ = 'job_element'

id = Column(Integer, primary_key=True)
project_id = Column(Integer)
name = Column(String)
data = Column(String)
data_type = Column(String)
create_time = Column(DateTime)
update_time = Column(DateTime)
# limitations under the License.
10 changes: 5 additions & 5 deletions solidui/daos/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class DAODeleteFailedError(DAOException):
message = "Delete failed"


class DatasourceTypeNotSupportedError(DAOException):
class DAOTypeNotSupportedError(DAOException):
"""
DAO datasource query source type is not supported
query source type is not supported
"""

status = 422
message = "DAO datasource query source type is not supported"
message = "query source type is not supported"


class DatasourceNotFound(DAOException):
class DAONotFound(DAOException):
status = 404
message = "Datasource does not exist"
message = "does not exist"
13 changes: 13 additions & 0 deletions solidui/daos/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
103 changes: 103 additions & 0 deletions solidui/daos/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from datetime import datetime
from typing import Optional
from solidui.daos.base import BaseDAO
from solidui.daos.exceptions import DAONotFound
from solidui.entity.core import Project
from solidui.utils.page_info import PageInfo
from sqlalchemy import or_


class ProjectDAO(BaseDAO[Project]):
model_cls = Project

@classmethod
def create_project(cls, project_name: str, description: str, user_name: str) -> Project:
attributes = {
'project_name': project_name,
'user_name': user_name,
'description': description,
'create_time': datetime.now(),
'update_time': datetime.now(),
'status': 0
}

return super().create(attributes=attributes)

@classmethod
def update_project(cls, project_id: int, name: Optional[str] = None, image: Optional[str] = None,
desc: Optional[str] = None) -> Project:
project = super().find_by_id(project_id)
if not project:
raise DAONotFound(message="Project not found")

attributes = {}
if name:
attributes['project_name'] = name
if image:
attributes['image'] = image
if desc:
attributes['description'] = desc

attributes['update_time'] = datetime.now()
attributes['status'] = 0

return super().update(item=project, attributes=attributes)

@classmethod
def delete_project(cls, project_id: int) -> Project:
project = super().find_by_id(project_id)
if not project:
raise DAONotFound(message="Project not found")

project.status = 1 # Assuming '1' means deleted

return super().update(item=project, attributes={'status': project.status})

@classmethod
def get_project(cls, project_id: int) -> Project:
project = super().find_by_id(project_id)
if not project:
raise DAONotFound(message="Project not found")
return project

@classmethod
def query_project_list_paging(cls, search_name: str, page_no: int, page_size: int) -> PageInfo[Project]:
# Build custom filters
custom_filters = None
if search_name:
custom_filters = or_(
cls.model_cls.project_name.like(f'%{search_name}%'),
cls.model_cls.description.like(f'%{search_name}%')
)

# Using the query_paginated method of BaseDAO
pagination = super().query_paginated(
page=page_no,
per_page=page_size,
custom_filters=custom_filters,
status=0,
sort_by='create_time',
sort_order='desc'
)

# Create PageInfo object
page_info = PageInfo(page_no, page_size)
page_info.set_total(pagination.total)
page_info.set_total_list(pagination.items)

return page_info
1 change: 1 addition & 0 deletions solidui/daos/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from solidui.daos.base import BaseDAO
from solidui.entity.core import User
from sqlalchemy.orm import Session
Expand Down
1 change: 1 addition & 0 deletions solidui/entity/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Project(Base):
update_time = Column(DateTime)
status = Column(Integer, nullable=False, default=0)


class User(Base):
__tablename__ = 'solidui_user'

Expand Down
47 changes: 47 additions & 0 deletions solidui/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SolidUIErrorType(StrEnum):
DELETE_JOB_ERROR = "DELETE_JOB_ERROR"
JOB_PAGE_ALREADY_EXISTS_ERROR = "JOB_PAGE_ALREADY_EXISTS_ERROR"
QUERY_MODEL_TYPE_ERROR = "QUERY_MODEL_TYPE_ERROR"
COOKIE_IS_NULL = "COOKIE_IS_NULL"
FAILED = "FAILED"


Expand Down Expand Up @@ -105,6 +106,7 @@ class SolidUIErrorType(StrEnum):
10077: _("Failed to delete job"),
10078: _("Job page already exists"),
10080: _("Failed to query model types"),
10081: _("Missing cookie"),
1: _("Request failed"),
}

Expand Down Expand Up @@ -149,5 +151,50 @@ class SolidUIErrorType(StrEnum):
SolidUIErrorType.DELETE_JOB_ERROR: [10077],
SolidUIErrorType.JOB_PAGE_ALREADY_EXISTS_ERROR: [10078],
SolidUIErrorType.QUERY_MODEL_TYPE_ERROR: [10080],
SolidUIErrorType.COOKIE_IS_NULL: [10081],
SolidUIErrorType.FAILED: [1],
}

ERROR_TYPE_TO_STATUS_CODE = {
SolidUIErrorType.INTERNAL_SERVER_ERROR: 500,
SolidUIErrorType.USER_NAME_EXIST: 409, # Conflict
SolidUIErrorType.USER_NAME_NULL: 400, # Bad Request
SolidUIErrorType.PASSWORD_NAME_NULL: 400,
SolidUIErrorType.USER_LOGIN_FAILURE: 401, # Unauthorized
SolidUIErrorType.LOGIN_OUT_FAILURE: 401,
SolidUIErrorType.UPDATE_PROJECT_ERROR: 400,
SolidUIErrorType.QUERY_PROJECT_DETAILS_ERROR: 400,
SolidUIErrorType.CREATE_PROJECT_ERROR: 400,
SolidUIErrorType.LOGIN_USER_QUERY_PROJECT_LIST_PAGING_ERROR: 400,
SolidUIErrorType.DELETE_PROJECT_ERROR: 400,
SolidUIErrorType.PROJECT_ALREADY_EXISTS_ERROR: 409,
SolidUIErrorType.PROJECT_NOT_EXISTS_ERROR: 404, # Not Found
SolidUIErrorType.DATASOURCE_NOT_EXISTS_ERROR: 404,
SolidUIErrorType.DELETE_DATASOURCE_ERROR: 400,
SolidUIErrorType.CREATE_DATASOURCE_ERROR: 400,
SolidUIErrorType.UPDATE_DATASOURCE_ERROR: 400,
SolidUIErrorType.QUERY_DATASOURCE_ERROR: 400,
SolidUIErrorType.DATASOURCE_ALREADY_EXISTS_ERROR: 409,
SolidUIErrorType.CREATE_DATASOURCE_TYPE_ERROR: 400,
SolidUIErrorType.QUERY_DATASOURCE_TYPE_ERROR: 400,
SolidUIErrorType.CREATE_DATASOURCE_TYPE_KEY_ERROR: 400,
SolidUIErrorType.DELETE_DATASOURCE_TYPE_KEY_ERROR: 400,
SolidUIErrorType.UPDATE_DATASOURCE_TYPE_KEY_ERROR: 400,
SolidUIErrorType.QUERY_DATASOURCE_TYPE_KEY_ERROR: 400,
SolidUIErrorType.QUERY_METADATA_DB_ERROR: 400,
SolidUIErrorType.QUERY_METADATA_TABLE_ERROR: 400,
SolidUIErrorType.QUERY_METADATA_SQL_ERROR: 400,
SolidUIErrorType.QUERY_METADATA_CONN_ERROR: 400,
SolidUIErrorType.QUERY_JOB_PAGE_ERROR: 400,
SolidUIErrorType.CREATE_JOB_PAGE_ERROR: 400,
SolidUIErrorType.UPDATE_JOB_PAGE_ERROR: 400,
SolidUIErrorType.DELETE_JOB_PAGE_ERROR: 400,
SolidUIErrorType.QUERY_JOB_ERROR: 400,
SolidUIErrorType.CREATE_JOB_ERROR: 400,
SolidUIErrorType.UPDATE_JOB_ERROR: 400,
SolidUIErrorType.DELETE_JOB_ERROR: 400,
SolidUIErrorType.JOB_PAGE_ALREADY_EXISTS_ERROR: 409,
SolidUIErrorType.QUERY_MODEL_TYPE_ERROR: 400,
SolidUIErrorType.COOKIE_IS_NULL: 400,
SolidUIErrorType.FAILED: 400,
}
7 changes: 4 additions & 3 deletions solidui/initialization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ def init_views(self) -> None:

from solidui.example.api import ExampleRestApi
from solidui.views.user.api import LoginRestApi
from solidui.views.project.api import ProjectRestApi

appbuilder.add_api(ExampleRestApi)
appbuilder.add_api(LoginRestApi)
appbuilder.add_api(ProjectRestApi)


# for rule in self.solidui_app.url_map.iter_rules():
# print(rule)
for rule in self.solidui_app.url_map.iter_rules():
print(rule)


def configure_fab(self) -> None:
Expand Down
Loading
Loading