web sttarting
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
# Database configuration package
|
||||
from pydantic.generics import GenericModel
|
||||
from pydantic import BaseModel
|
||||
import re
|
||||
import os
|
||||
import urllib.parse
|
||||
|
||||
class Settings(GenericModel):
|
||||
class Settings(BaseModel):
|
||||
DB_HOST: str = os.getenv("DB_HOST", "postgres")
|
||||
DB_PORT: int = int(os.getenv("DB_PORT", "5432"))
|
||||
DB_USER: str = os.getenv("DB_USER", "postgres")
|
||||
@@ -32,3 +32,15 @@ class Settings(GenericModel):
|
||||
def DATABASE_URL_syncpg(self) -> str:
|
||||
pwd = urllib.parse.quote_plus(str(self.DB_PASS))
|
||||
return f"postgresql+pg8000://{self.DB_USER}:{pwd}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
|
||||
|
||||
@property
|
||||
def PG8000_CONNECT_ARGS(self) -> dict:
|
||||
"""connect_args для pg8000 — отключаем SSL (сервер его не поддерживает)."""
|
||||
return {"ssl_context": None}
|
||||
|
||||
|
||||
def make_engine():
|
||||
"""Создаёт SQLAlchemy engine с правильными параметрами для pg8000."""
|
||||
from sqlalchemy import create_engine
|
||||
s = Settings()
|
||||
return create_engine(s.DATABASE_URL_syncpg, connect_args=s.PG8000_CONNECT_ARGS, future=True)
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
from sqlalchemy import Column, String, ForeignKey
|
||||
from sqlalchemy import Column, String
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
class Group(Base):
|
||||
__tablename__ = "group"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name_group = Column(String(150), nullable=False)
|
||||
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
|
||||
user = relationship("User", backref="groups", passive_deletes=True)
|
||||
name_group = Column(String(150), nullable=False, unique=True)
|
||||
|
||||
__all__ = ["Base", "Group"]
|
||||
|
||||
|
||||
@@ -8,10 +8,7 @@ class Organization(Base):
|
||||
__tablename__ = "organization"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name_organization = Column(String(150), nullable=False)
|
||||
group_id = Column(UUID(as_uuid=True), ForeignKey("group.id", ondelete="CASCADE"), nullable=False)
|
||||
|
||||
group = relationship("Group", backref="organizations", passive_deletes=True)
|
||||
name_organization = Column(String(150), nullable=False, unique=True)
|
||||
|
||||
__all__ = ["Base", "Organization"]
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class Response(Base):
|
||||
__tablename__ = "responses"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
poll_id = Column(UUID(as_uuid=True), ForeignKey("polls.id"), nullable=False)
|
||||
poll_id = Column(UUID(as_uuid=True), ForeignKey("polls.id", ondelete="CASCADE"), nullable=False)
|
||||
user_id = Column(UUID(as_uuid=True), nullable=True)
|
||||
submitted_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||
|
||||
@@ -22,7 +22,7 @@ class Answer(Base):
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
response_id = Column(UUID(as_uuid=True), ForeignKey("responses.id", ondelete="CASCADE"), nullable=False)
|
||||
question_id = Column(UUID(as_uuid=True), ForeignKey("questions.id"), nullable=False)
|
||||
question_id = Column(UUID(as_uuid=True), ForeignKey("questions.id", ondelete="CASCADE"), nullable=False)
|
||||
choice_id = Column(UUID(as_uuid=True), nullable=True)
|
||||
text = Column(Text, nullable=True)
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from sqlalchemy import Column, String
|
||||
from sqlalchemy import Column, String, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import relationship
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
|
||||
@@ -12,5 +13,11 @@ class User(Base):
|
||||
username = Column(String(150), nullable=False, unique=True)
|
||||
hashed_password = Column(String(256), nullable=False)
|
||||
|
||||
group_id = Column(UUID(as_uuid=True), ForeignKey("group.id", ondelete="SET NULL"), nullable=True)
|
||||
organization_id = Column(UUID(as_uuid=True), ForeignKey("organization.id", ondelete="SET NULL"), nullable=True)
|
||||
|
||||
group = relationship("Group", foreign_keys=[group_id])
|
||||
organization = relationship("Organization", foreign_keys=[organization_id])
|
||||
|
||||
__all__ = ["Base", "User"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user