Skip to content

Commit

Permalink
fix(orm): fix orm and other bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxin688 committed Jul 7, 2024
1 parent b965e86 commit f9a05a3
Show file tree
Hide file tree
Showing 27 changed files with 234 additions and 192 deletions.
9 changes: 1 addition & 8 deletions backend/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from src.core.database.base import Base
from src.features.admin.models import *
from src.features.dcim.models import *
from src.features.ipam.models import *
from src.features.circuit.models import *
from src.features.netconfig.models import *
from src.features.intend.models import *
from src.features.org.models import *
from src.core.database import Base

target_metadata = Base.metadata

Expand Down
15 changes: 10 additions & 5 deletions backend/deploy/collections/metadata/circuit_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@
"id": 1,
"name": {"en_US": "Ponit-to-point", "zh_CN": "点对点专线"},
"slug": "p2p",
"description": "Point to point circuit/点对点专线"
"description": "Point to point circuit/点对点专线",
"connection_type": "LAN"
},
{
"id": 2,
"name": {"en_US": "Internet", "zh_CN": "互联网专线"},
"slug": "internet",
"description": "Internet circuit with dedicated public IP addresses/互联网专线(带公网IP地址)"
"description": "Internet circuit with dedicated public IP addresses/互联网专线(带公网IP地址)",
"connection_type": "WAN"
},
{
"id": 3,
"name": {"en_US": "MPLS", "zh_CN": "MPLS"},
"slug": "mpls",
"description": "MPLS"
"description": "MPLS",
"connection_type": "WAN"
},
{
"id": 4,
"name": {"en_US": "Dark Fiber", "zh_CN": "裸光专线"},
"slug": "dark-fiber",
"description": "大带宽同城裸光专线(通常距离较短的大带宽同城裸光专线)"
"description": "大带宽同城裸光专线(通常距离较短的大带宽同城裸光专线)",
"connection_type": "WAN"
},
{
"id": 5,
"name": {"en_US": "ADSL", "zh_CN": "ADSL线路"},
"slug": "adsl",
"description": "Intenet access with dynamic public IP addresses /ADSL互联网接入线路(无固定的公网地址)"
"description": "Intenet access with dynamic public IP addresses /ADSL互联网接入线路(无固定的公网地址)",
"connection_type": "WAN"
}
]
55 changes: 43 additions & 12 deletions backend/deploy/collections/metadata/device_role.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,72 @@
{
"name": {"en_US": "Core Switch", "zh_CN": "核心交换机"},
"slug": "core-switch",
"description": ""
"description": "",
"abbreviation": "CSW",
"priority": 100
},
{
"name": {"en_US": "Distribution Switch", "zh_CN": "汇聚交换机"},
"slug": "distribution-switch",
"description": ""
"description": "",
"abbreviation": "DSW",
"priority": 200
},
{
"name": {"en_US": "Access Switch", "zh_CN": "接入交换机"},
"slug": "access-switch",
"description": ""
"description": "",
"abbreviation": "ASW",
"priority": 300
},
{
"name": {"en_US": "Leaf Switch", "zh_CN": "leaf交换机"},
"slug": "leaf",
"description": ""
"description": "",
"abbreviation": "LEAF",
"priority": 300
},
{
"name": {"en_US": "Spine Switch", "zh_CN": "Spine交换机"},
"slug": "spine",
"description": ""
},
{
"name": {"en_US": "Server Leaf Switch", "zh_CN": "服务器leaf交换机"},
"slug": "server-leaf",
"description": ""
"description": "",
"abbreviation": "SPINE",
"priority": 100
},
{
"name": {"en_US": "Border Leaf Switch", "zh_CN": "边缘leaf交换机"},
"slug": "border-leaf",
"description": ""
"description": "",
"abbreviation": "BLEAF",
"priority": 200
},
{
"name": {"en_US": "Firewall", "zh_CN": "防火墙"},
"slug": "firewall",
"description": ""
"description": "",
"abbreviation": "FW",
"priority": 50
},
{
"name": {"en_US": "Gateway Router", "zh_CN": "网关路由器"},
"slug": "gateway-router",
"description": "",
"abbreviation": "GR",
"priority": 30
},
{
"name": {"en_US": "Access Point", "zh_CN": "无线AP"},
"slug": "access-point",
"description": "",
"abbreviation": "AP",
"priority": 400

},
{
"name": {"en_US": "WLAN Controller", "zh_CN": "无线控制器"},
"slug": "wlan-ac",
"description": "",
"abbreviation": "WLC",
"priority": 150
}
]
13 changes: 6 additions & 7 deletions backend/deploy/init_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from src.features.admin.models import Group, Role, User
from src.features.admin.security import get_password_hash
from src.features.consts import ReservedRoleSlug
from src.features.dcim.models import DeviceType, Manufacturer
from src.features.intend.models import CircuitType, DeviceRole, IPRole, Platform
from src.features.intend.models import CircuitType, DeviceRole, DeviceType, IPRole, Manufacturer, Platform
from src.features.ipam.models import Block

if TYPE_CHECKING:
Expand Down Expand Up @@ -74,16 +73,16 @@ async def init_platform(session: "AsyncSession") -> None:


async def init_manufacturer(session: "AsyncSession") -> None:
async with await open_file(f"{PROJECT_DIR}/deploy/collections/metadata/vendor.json") as f:
async with await open_file(f"{PROJECT_DIR}/deploy/collections/metadata/manufacturer.json") as f:
contents = await f.read()
vendors = json.loads(contents)
new_vendors = [Manufacturer(**p) for p in vendors]
manufacturers = json.loads(contents)
new_manufacturers = [Manufacturer(**p) for p in manufacturers]
db_objs = (await session.scalars(select(Manufacturer))).all()
if not db_objs:
session.add_all(new_vendors)
session.add_all(new_manufacturers)
else:
slugs = [v.slug for v in db_objs]
for new_v in new_vendors:
for new_v in new_manufacturers:
if new_v.slug not in slugs:
session.add(new_v)
await session.commit()
Expand Down
4 changes: 2 additions & 2 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fixable = ["ALL"]

[tool.ruff.lint.extend-per-file-ignores]
"env.py" = ["INP001", "I001", "ERA001"]
"tests/*.py" = ["S101", "ANN201"]
"tests/*.py" = ["S101", "ANN201", "PLR2004"]
"*exceptions.py" = ["ARG001"]
"models.py" = ["RUF012"]
"schemas.py" = ["ANN201"]
Expand All @@ -96,7 +96,7 @@ fixable = ["ALL"]
"src/features/internal/api.py" = ["ARG001"]
"src/features/admin/schemas.py" = ["N815"] # frontend menu
"alembic/*.py" = ["INP001", "UP007", "PLR0915", "E402", "F403"]
"__init__.py" = ["F403"]
"__init__.py" = ["F403", "F401"]

[tool.ruff.lint.flake8-bugbear]
extend-immutable-calls = [
Expand Down
4 changes: 3 additions & 1 deletion backend/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]: # noqa: ARG001
openapi_url="/api/openapi.json",
)

@app.get("/api/elements", include_in_schema=False, operation_id="1a4987dd-6c38-4502-a879-3fe35050ae38")
@app.get(
"/api/elements", tags=["Docs"], include_in_schema=False, operation_id="1a4987dd-6c38-4502-a879-3fe35050ae38"
)
def get_stoplight_elements() -> HTMLResponse:
return get_stoplight_elements_html(
openapi_url="/api/openapi.json", title=settings.PROJECT_NAME, base_path="/api/elements"
Expand Down
10 changes: 10 additions & 0 deletions backend/src/core/database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from src.core.database.base import Base
from src.features.admin.models import *
from src.features.alert.models import *
from src.features.circuit.models import *
from src.features.dcim.models import *
from src.features.intend.models import *
from src.features.ipam.models import *
from src.features.monitor.models import *
from src.features.netconfig.models import *
from src.features.org.models import *
2 changes: 1 addition & 1 deletion backend/src/core/database/mixins/audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlalchemy.orm import Mapped, Mapper, class_mapper, mapped_column, relationship
from sqlalchemy.orm.attributes import get_history

from src.core.database.base import Base
from src.core.database import Base
from src.core.database.types import DateTimeTZ, int_pk
from src.core.utils.context import orm_diff_ctx, request_id_ctx, user_ctx

Expand Down
28 changes: 10 additions & 18 deletions backend/src/core/errors/exception_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import sys
import traceback
from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
from typing import Any, NewType
Expand Down Expand Up @@ -74,31 +73,24 @@ def __repr__(self) -> str:
return f"Gener Error Occurred: ErrCode: {self.error.error}, Message: {self.error.message}"


def log_exception(exc: type[BaseException] | Exception, logger_trace_info: bool) -> None:
def log_exception(exception: BaseException, include_traceback: bool) -> None:
"""
Logs an exception.
Args:
exc (Type[BaseException] | Exception): The exception to be logged.
logger_trace_info (bool): Indicates whether to include detailed trace information in the log.
exception (BaseException): The exception to be logged.
include_traceback (bool): Indicates whether to include detailed trace information in the log.
Returns:
None
Raises:
N/A
"""
logger = logging.getLogger(__name__)
ex_type, _, ex_traceback = sys.exc_info()
trace_back = traceback.format_list(traceback.extract_tb(ex_traceback)[-1:])[-1]

logger.warning(f"ErrorMessage: {exc!s}")
logger.warning(f"Exception Type {ex_type.__name__}: ")
exception_type = type(exception).__name__
logger.warning(f"ErrorMessage: {exception}")
logger.warning(f"Exception Type: {exception_type}")

if not logger_trace_info:
logger.warning(f"Stack trace: {trace_back}")
else:
logger.exception(f"Stack trace: {trace_back}")
if include_traceback:
traceback_info = "".join(traceback.format_exc().splitlines()[-3:])
logger.warning(f"Traceback: {traceback_info}")


async def token_invalid_handler(request: Request, exc: TokenInvalidError) -> JSONResponse: # noqa: ARG001
Expand Down Expand Up @@ -148,7 +140,7 @@ def gener_error_handler(request: Request, exc: GenerError) -> JSONResponse: # n


def default_exception_handler(request: Request, exc: Exception) -> JSONResponse: # noqa: ARG001
log_exception(exc, logger_trace_info=True)
log_exception(exc, True)
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={
Expand Down
2 changes: 1 addition & 1 deletion backend/src/core/repositories/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sqlalchemy.orm import InstrumentedAttribute, selectinload, undefer
from sqlalchemy.sql.base import ExecutableOption

from src.core.database.base import Base
from src.core.database import Base
from src.core.database.session import async_engine
from src.core.errors.exception_handlers import ExistError, NotFoundError
from src.core.utils.context import locale_ctx
Expand Down
25 changes: 15 additions & 10 deletions backend/src/features/admin/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,22 @@ async def get_permissions(self) -> list[schemas.Permission]:

@router.post("/permissions", operation_id="e0fe80d5-cbe0-4c2c-9eff-57e80ecba522", summary="Permissions: Sync All")
async def sync_db_permission(self, request: Request) -> dict:
routes = request.app.routes
operation_ids = [route.operation_id for route in routes]
router_mappings = {
router.operation_id: {
"name": router.name,
"path": router.path,
"methods": router.methods,
"description": router.description,
routes = request.app.router.__dict__["routes"]
operation_ids = []
router_mappings = {}
for route in routes:
if hasattr(route, "tags") and not hasattr(route, "operation_id"):
msg = f"Missing operation_id for route: {route.path}"
raise ValueError(msg)
if not hasattr(route, "operation_id") and not hasattr(route, "tags"):
continue
operation_ids.append(route.operation_id)
router_mappings[route.operation_id] = {
"name": route.name,
"url": route.path,
"method": next(iter(route.methods)),
"tag": route.tags[0],
}
for router in routes
}
permissions = await self.service.get_multi_by_ids(self.session, operation_ids)
removed = {str(p.id) for p in permissions} - set(operation_ids)
added = set(operation_ids) - {str(p.id) for p in permissions}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/features/admin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlalchemy.orm import Mapped, backref, column_property, mapped_column, relationship
from sqlalchemy.orm.collections import attribute_mapped_collection

from src.core.database.base import Base
from src.core.database import Base
from src.core.database.mixins import AuditTimeMixin
from src.core.database.types import bool_false, bool_true, int_pk, uuid_pk

Expand Down
3 changes: 2 additions & 1 deletion backend/src/features/admin/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from uuid import UUID

from pydantic_extra_types.phone_numbers import PhoneNumber

Expand All @@ -16,7 +17,7 @@ class AccessToken(BaseModel):


class Permission(BaseModel):
id: int
id: UUID
name: str
url: str
method: str
Expand Down
Loading

0 comments on commit f9a05a3

Please sign in to comment.