mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-28 19:28:07 +00:00
bde706d19a
Change location of the database.
103 lines
3.0 KiB
Python
103 lines
3.0 KiB
Python
#!/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
|
|
|
|
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime, func
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.types import TypeDecorator, CHAR
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
|
|
from sqlalchemy.orm import declarative_base
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
class GUID(TypeDecorator):
|
|
"""Platform-independent GUID type.
|
|
Uses PostgreSQL's UUID type, otherwise uses
|
|
CHAR(32), storing as stringified hex values.
|
|
"""
|
|
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())
|
|
|
|
|
|
def generate_uuid():
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
class User(BaseTable):
|
|
|
|
__tablename__ = "users"
|
|
|
|
user_id = Column(GUID, primary_key=True, default=generate_uuid)
|
|
username = Column(String, unique=True, index=True)
|
|
email = Column(String, unique=True, index=True)
|
|
full_name = Column(String)
|
|
hashed_password = Column(String)
|
|
is_active = Column(Boolean, default=True)
|
|
is_superuser = Column(Boolean, default=False)
|
|
|
|
|
|
# items = relationship("Item", back_populates="owner")
|
|
#
|
|
#
|
|
# class Item(Base):
|
|
# __tablename__ = "items"
|
|
#
|
|
# id = Column(Integer, primary_key=True, index=True)
|
|
# title = Column(String, index=True)
|
|
# description = Column(String, index=True)
|
|
# owner_id = Column(Integer, ForeignKey("users.id"))
|
|
#
|
|
# owner = relationship("User", back_populates="items")
|