mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-05 06:41:13 +02:00
Base API and tables for RBAC support.
This commit is contained in:
1 parent
eb0f8c6174
commit
6d4da98b8e
15 files changed
+1196
-42
No files matched your search
@@ -19,7 +19,7 @@ import uuid
|
||||
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from sqlalchemy import Column, DateTime, func, inspect
|
||||
from sqlalchemy.types import TypeDecorator, CHAR
|
||||
from sqlalchemy.types import TypeDecorator, CHAR, VARCHAR
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.ext.declarative import as_declarative
|
||||
|
||||
@@ -72,6 +72,37 @@ class GUID(TypeDecorator):
|
||||
return value
|
||||
|
||||
|
||||
class ListException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ListType(TypeDecorator):
|
||||
"""
|
||||
Save/restore a Python list to/from a database column.
|
||||
"""
|
||||
|
||||
impl = VARCHAR
|
||||
cache_ok = True
|
||||
|
||||
def __init__(self, separator=',', *args, **kwargs):
|
||||
|
||||
self._separator = separator
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
if value is not None:
|
||||
if any(self._separator in str(item) for item in value):
|
||||
raise ListException(f"List values cannot contain '{self._separator}'"
|
||||
f"Please use a different separator.")
|
||||
return self._separator.join(map(str, value))
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
if value is None:
|
||||
return []
|
||||
else:
|
||||
return list(map(str, value.split(self._separator)))
|
||||
|
||||
|
||||
class BaseTable(Base):
|
||||
|
||||
__abstract__ = True
|
||||
@@ -79,6 +110,8 @@ class BaseTable(Base):
|
||||
created_at = Column(DateTime, server_default=func.current_timestamp())
|
||||
updated_at = Column(DateTime, server_default=func.current_timestamp(), onupdate=func.current_timestamp())
|
||||
|
||||
__mapper_args__ = {"eager_defaults": True}
|
||||
|
||||
|
||||
def generate_uuid():
|
||||
return str(uuid.uuid4())
|
||||
Reference in new issue
Block a user