2020-12-02 08:09:08 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
2021-03-28 00:45:08 +00:00
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
|
|
from sqlalchemy import Column, DateTime, func, inspect
|
2020-12-02 08:09:08 +00:00
|
|
|
from sqlalchemy.types import TypeDecorator, CHAR
|
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
2021-03-28 00:45:08 +00:00
|
|
|
from sqlalchemy.ext.declarative import as_declarative
|
2020-12-07 06:22:36 +00:00
|
|
|
|
|
|
|
|
2021-03-28 00:45:08 +00:00
|
|
|
@as_declarative()
|
|
|
|
class Base:
|
|
|
|
|
2021-03-28 10:47:29 +00:00
|
|
|
def asdict(self):
|
2021-03-28 00:45:08 +00:00
|
|
|
|
|
|
|
return {c.key: getattr(self, c.key)
|
|
|
|
for c in inspect(self).mapper.column_attrs}
|
|
|
|
|
2021-03-28 10:47:29 +00:00
|
|
|
def asjson(self):
|
2021-03-28 00:45:08 +00:00
|
|
|
|
2021-03-28 10:47:29 +00:00
|
|
|
return jsonable_encoder(self.asdict())
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GUID(TypeDecorator):
|
2021-03-28 00:45:08 +00:00
|
|
|
"""
|
|
|
|
Platform-independent GUID type.
|
2020-12-02 08:09:08 +00:00
|
|
|
Uses PostgreSQL's UUID type, otherwise uses
|
|
|
|
CHAR(32), storing as stringified hex values.
|
|
|
|
"""
|
2021-03-28 00:45:08 +00:00
|
|
|
|
2020-12-02 08:09:08 +00:00
|
|
|
impl = CHAR
|
|
|
|
|
|
|
|
def load_dialect_impl(self, dialect):
|
|
|
|
if dialect.name == 'postgresql':
|
|
|
|
return dialect.type_descriptor(UUID())
|
|
|
|
else:
|
|
|
|
return dialect.type_descriptor(CHAR(32))
|
|
|
|
|
|
|
|
def process_bind_param(self, value, dialect):
|
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
elif dialect.name == 'postgresql':
|
|
|
|
return str(value)
|
|
|
|
else:
|
|
|
|
if not isinstance(value, uuid.UUID):
|
|
|
|
return "%.32x" % uuid.UUID(value).int
|
|
|
|
else:
|
|
|
|
# hexstring
|
|
|
|
return "%.32x" % value.int
|
|
|
|
|
|
|
|
def process_result_value(self, value, dialect):
|
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
if not isinstance(value, uuid.UUID):
|
|
|
|
value = uuid.UUID(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTable(Base):
|
|
|
|
|
|
|
|
__abstract__ = True
|
|
|
|
|
|
|
|
created_at = Column(DateTime, default=func.current_timestamp())
|
|
|
|
updated_at = Column(DateTime,
|
|
|
|
default=func.current_timestamp(),
|
|
|
|
onupdate=func.current_timestamp())
|
|
|
|
|
|
|
|
|
2020-12-07 06:22:36 +00:00
|
|
|
def generate_uuid():
|
|
|
|
return str(uuid.uuid4())
|